-
- All Implemented Interfaces:
-
io.livekit.android.audio.AudioBufferCallback
@RequiresApi(value = 29) public final class ScreenAudioCapturer extends MixerAudioBufferCallback
A mixer for capturing screen share audio.
Requires a media projection, which can be obtained from the screen share track.
Additionally, for screen capture to work properly while your app is in the background, a foreground service with the type
microphonemust be running. Otherwise, audio capture will not return any audio data.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() }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classScreenAudioCapturer.Companion
-
Constructor Summary
Constructors Constructor Description ScreenAudioCapturer(MediaProjection mediaProjection, Function1<AudioPlaybackCaptureConfiguration.Builder, Unit> captureConfigurator)
-
Method Summary
Modifier and Type Method Description final FloatgetGain()A multiplier to adjust the volume of the captured audio data. final UnitsetGain(Float gain)A multiplier to adjust the volume of the captured audio data. MixerAudioBufferCallback.BufferResponseonBufferRequest(ByteBuffer originalBuffer, Integer audioFormat, Integer channelCount, Integer sampleRate, Integer bytesRead, Long captureTimeNs)final BooleaninitAudioRecord(Integer audioFormat, Integer channelCount, Integer sampleRate)final UnitreleaseAudioResources()Release any audio resources associated with this capturer. -
-
Constructor Detail
-
ScreenAudioCapturer
ScreenAudioCapturer(MediaProjection mediaProjection, Function1<AudioPlaybackCaptureConfiguration.Builder, Unit> captureConfigurator)
-
-
Method Detail
-
getGain
final Float getGain()
A multiplier to adjust the volume of the captured audio data.
Values above 1 will increase the volume, values less than 1 will decrease it.
-
setGain
final Unit setGain(Float gain)
A multiplier to adjust the volume of the captured audio data.
Values above 1 will increase the volume, values less than 1 will decrease it.
-
onBufferRequest
MixerAudioBufferCallback.BufferResponse onBufferRequest(ByteBuffer originalBuffer, Integer audioFormat, Integer channelCount, Integer sampleRate, Integer bytesRead, Long captureTimeNs)
-
initAudioRecord
final Boolean initAudioRecord(Integer audioFormat, Integer channelCount, Integer sampleRate)
-
releaseAudioResources
final Unit releaseAudioResources()
Release any audio resources associated with this capturer. This is not managed by LiveKit, so you must call this function when finished to prevent memory leaks.
-
-
-
-