|
| 1 | +/* |
| 2 | + * Copyright 2024 LiveKit, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.livekit.android.example.screenshareaudio |
| 18 | + |
| 19 | +import android.media.projection.MediaProjectionManager |
| 20 | +import android.os.Bundle |
| 21 | +import androidx.activity.ComponentActivity |
| 22 | +import androidx.activity.compose.setContent |
| 23 | +import androidx.activity.result.contract.ActivityResultContracts |
| 24 | +import androidx.activity.viewModels |
| 25 | +import androidx.compose.foundation.layout.fillMaxSize |
| 26 | +import androidx.compose.material3.Button |
| 27 | +import androidx.compose.material3.MaterialTheme |
| 28 | +import androidx.compose.material3.Surface |
| 29 | +import androidx.compose.material3.Text |
| 30 | +import androidx.compose.runtime.getValue |
| 31 | +import androidx.compose.runtime.mutableStateOf |
| 32 | +import androidx.compose.runtime.remember |
| 33 | +import androidx.compose.runtime.setValue |
| 34 | +import androidx.compose.ui.Modifier |
| 35 | +import io.livekit.android.LiveKit |
| 36 | +import io.livekit.android.example.screenshareaudio.ui.theme.LivekitandroidTheme |
| 37 | +import io.livekit.android.util.LoggingLevel |
| 38 | + |
| 39 | +class MainActivity : ComponentActivity() { |
| 40 | + |
| 41 | + private val viewModel: MainViewModel by viewModels<MainViewModel>() |
| 42 | + private val screenCaptureIntentLauncher = |
| 43 | + registerForActivityResult( |
| 44 | + ActivityResultContracts.StartActivityForResult(), |
| 45 | + ) { result -> |
| 46 | + val resultCode = result.resultCode |
| 47 | + val data = result.data |
| 48 | + if (resultCode != RESULT_OK || data == null) { |
| 49 | + return@registerForActivityResult |
| 50 | + } |
| 51 | + viewModel.startScreenCapture(data) |
| 52 | + } |
| 53 | + |
| 54 | + private fun requestMediaProjection() { |
| 55 | + val mediaProjectionManager = |
| 56 | + getSystemService(MEDIA_PROJECTION_SERVICE) as MediaProjectionManager |
| 57 | + screenCaptureIntentLauncher.launch(mediaProjectionManager.createScreenCaptureIntent()) |
| 58 | + } |
| 59 | + |
| 60 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 61 | + super.onCreate(savedInstanceState) |
| 62 | + LiveKit.loggingLevel = LoggingLevel.INFO |
| 63 | + viewModel |
| 64 | + setContent { |
| 65 | + LivekitandroidTheme { |
| 66 | + // A surface container using the 'background' color from the theme |
| 67 | + Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) { |
| 68 | + var enableScreenCapture by remember { |
| 69 | + mutableStateOf(true) |
| 70 | + } |
| 71 | + Button( |
| 72 | + onClick = { |
| 73 | + if (enableScreenCapture) { |
| 74 | + requestMediaProjection() |
| 75 | + } else { |
| 76 | + viewModel.stopScreenCapture() |
| 77 | + } |
| 78 | + enableScreenCapture = !enableScreenCapture |
| 79 | + }, |
| 80 | + ) { |
| 81 | + val text = if (enableScreenCapture) { |
| 82 | + "enable" |
| 83 | + } else { |
| 84 | + "disable" |
| 85 | + } |
| 86 | + Text(text = text) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments