Committed by
GitHub
Comments for screen share audio capture example (#531)
* Comments for screen share audio capture example * spotless
正在显示
1 个修改的文件
包含
7 行增加
和
2 行删除
| @@ -48,8 +48,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { | @@ -48,8 +48,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { | ||
| 48 | room.connect(url, token) | 48 | room.connect(url, token) |
| 49 | } | 49 | } |
| 50 | 50 | ||
| 51 | - // Start a foreground service to keep the call from being interrupted if the | ||
| 52 | - // app goes into the background. | 51 | + // Screen share audio capture in the background requires a foreground service with MICROPHONE type. |
| 53 | val foregroundServiceIntent = Intent(application, ForegroundService::class.java) | 52 | val foregroundServiceIntent = Intent(application, ForegroundService::class.java) |
| 54 | application.startForegroundService(foregroundServiceIntent) | 53 | application.startForegroundService(foregroundServiceIntent) |
| 55 | } | 54 | } |
| @@ -59,11 +58,15 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { | @@ -59,11 +58,15 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { | ||
| 59 | if (ActivityCompat.checkSelfPermission(getApplication(), Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) { | 58 | if (ActivityCompat.checkSelfPermission(getApplication(), Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) { |
| 60 | return@launch | 59 | return@launch |
| 61 | } | 60 | } |
| 61 | + | ||
| 62 | room.localParticipant.setScreenShareEnabled(true, data) | 62 | room.localParticipant.setScreenShareEnabled(true, data) |
| 63 | + | ||
| 64 | + // Publish the audio track. | ||
| 63 | room.localParticipant.setMicrophoneEnabled(true) | 65 | room.localParticipant.setMicrophoneEnabled(true) |
| 64 | val screenCaptureTrack = room.localParticipant.getTrackPublication(Track.Source.SCREEN_SHARE)?.track as? LocalVideoTrack ?: return@launch | 66 | val screenCaptureTrack = room.localParticipant.getTrackPublication(Track.Source.SCREEN_SHARE)?.track as? LocalVideoTrack ?: return@launch |
| 65 | val audioTrack = room.localParticipant.getTrackPublication(Track.Source.MICROPHONE)?.track as? LocalAudioTrack ?: return@launch | 67 | val audioTrack = room.localParticipant.getTrackPublication(Track.Source.MICROPHONE)?.track as? LocalAudioTrack ?: return@launch |
| 66 | 68 | ||
| 69 | + // Start capturing the screen share audio. | ||
| 67 | audioCapturer = ScreenAudioCapturer.createFromScreenShareTrack(screenCaptureTrack) ?: return@launch | 70 | audioCapturer = ScreenAudioCapturer.createFromScreenShareTrack(screenCaptureTrack) ?: return@launch |
| 68 | audioCapturer?.gain = 0.1f // Lower the volume so that mic can still be heard clearly. | 71 | audioCapturer?.gain = 0.1f // Lower the volume so that mic can still be heard clearly. |
| 69 | audioTrack.setAudioBufferCallback(audioCapturer!!) | 72 | audioTrack.setAudioBufferCallback(audioCapturer!!) |
| @@ -76,6 +79,8 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { | @@ -76,6 +79,8 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { | ||
| 76 | ?.setAudioBufferCallback(null) | 79 | ?.setAudioBufferCallback(null) |
| 77 | room.localParticipant.setMicrophoneEnabled(false) | 80 | room.localParticipant.setMicrophoneEnabled(false) |
| 78 | room.localParticipant.setScreenShareEnabled(false) | 81 | room.localParticipant.setScreenShareEnabled(false) |
| 82 | + | ||
| 83 | + // Remember to release when done capturing. | ||
| 79 | audioCapturer?.releaseAudioResources() | 84 | audioCapturer?.releaseAudioResources() |
| 80 | } | 85 | } |
| 81 | } | 86 | } |
-
请 注册 或 登录 后发表评论