davidliu
Committed by GitHub

Add example usage for ScreenAudioCapturer (#582)

/*
* Copyright 2024 LiveKit, Inc.
* Copyright 2024-2025 LiveKit, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
... ... @@ -61,6 +61,10 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
room.localParticipant.setScreenShareEnabled(true, data)
// Optionally disable the mic for screenshare audio only
// val javaAudioDeviceModule = (room.lkObjects.audioDeviceModule as? JavaAudioDeviceModule)
// javaAudioDeviceModule?.setAudioRecordEnabled(false)
// Publish the audio track.
room.localParticipant.setMicrophoneEnabled(true)
val screenCaptureTrack = room.localParticipant.getTrackPublication(Track.Source.SCREEN_SHARE)?.track as? LocalVideoTrack ?: return@launch
... ...
/*
* Copyright 2024 LiveKit, Inc.
* Copyright 2024-2025 LiveKit, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
... ... @@ -61,7 +61,38 @@ private val DEFAULT_CONFIGURATOR: AudioPlaybackCaptureConfigurator = { builder -
*
* Example usage:
* ```
* suspend fun startScreenCapture(data: Intent) {
* if (ActivityCompat.checkSelfPermission(getApplication(), Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
* return
* }
*
* // Publish the screen share video.
* room.localParticipant.setScreenShareEnabled(true, data)
*
* // Optionally disable the mic for screenshare audio only
* // val javaAudioDeviceModule = (room.lkObjects.audioDeviceModule as? JavaAudioDeviceModule)
* // javaAudioDeviceModule?.setAudioRecordEnabled(false)
*
* // Publish the audio track.
* room.localParticipant.setMicrophoneEnabled(true)
* val screenCaptureTrack = room.localParticipant.getTrackPublication(Track.Source.SCREEN_SHARE)?.track as? LocalVideoTrack ?: return
* val audioTrack = room.localParticipant.getTrackPublication(Track.Source.MICROPHONE)?.track as? LocalAudioTrack ?: return
*
* // Start capturing the screen share audio.
* val audioCapturer = ScreenAudioCapturer.createFromScreenShareTrack(screenCaptureTrack) ?: return
* audioCapturer.gain = 0.1f // Lower the volume so that mic can still be heard clearly.
* audioTrack.setAudioBufferCallback(audioCapturer)
* }
*
* suspend fun stopScreenCapture() {
* (room.localParticipant.getTrackPublication(Track.Source.MICROPHONE)?.track as? LocalAudioTrack)
* ?.setAudioBufferCallback(null)
* room.localParticipant.setMicrophoneEnabled(false)
* room.localParticipant.setScreenShareEnabled(false)
*
* // Remember to release when done capturing.
* audioCapturer?.releaseAudioResources()
* }
* ```
*/
@RequiresApi(Build.VERSION_CODES.Q)
... ...
... ... @@ -25,6 +25,7 @@ import com.vdurmont.semver4j.Semver
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import io.livekit.android.audio.ScreenAudioCapturer
import io.livekit.android.dagger.CapabilitiesGetter
import io.livekit.android.dagger.InjectionNames
import io.livekit.android.events.ParticipantEvent
... ... @@ -271,11 +272,14 @@ internal constructor(
*
* This will use capture and publish default options from [Room].
*
* For screenshare audio, a [ScreenAudioCapturer] can be used.
*
* @param mediaProjectionPermissionResultData The resultData returned from launching
* [MediaProjectionManager.createScreenCaptureIntent()](https://developer.android.com/reference/android/media/projection/MediaProjectionManager#createScreenCaptureIntent()).
* @throws IllegalArgumentException if attempting to enable screenshare without [mediaProjectionPermissionResultData]
* @see Room.screenShareTrackCaptureDefaults
* @see Room.screenShareTrackPublishDefaults
* @see ScreenAudioCapturer
*/
suspend fun setScreenShareEnabled(
enabled: Boolean,
... ...