fix video capture sound playing in video recording

- add custom MediaActionSound, with functionality to notify callers when playing sound ends
- only start video recording when the sound has played completely
This commit is contained in:
darthpaul
2022-11-24 14:01:21 +00:00
parent 97304bd497
commit f08180aa38
4 changed files with 225 additions and 15 deletions

View File

@ -57,7 +57,7 @@ class CameraXPreview(
private val contentResolver = activity.contentResolver
private val mainExecutor = ContextCompat.getMainExecutor(activity)
private val displayManager = activity.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
private val mediaSoundHelper = MediaSoundHelper()
private val mediaSoundHelper = MediaSoundHelper(activity)
private val windowMetricsCalculator = WindowMetricsCalculator.getOrCreate()
private val videoQualityManager = VideoQualityManager(activity)
private val imageQualityManager = ImageQualityManager(activity)
@ -353,6 +353,11 @@ class CameraXPreview(
orientationEventListener.disable()
}
override fun onDestroy(owner: LifecycleOwner) {
super.onDestroy(owner)
mediaSoundHelper.release()
}
override fun isInPhotoMode(): Boolean {
return isPhotoCapture
}
@ -540,7 +545,14 @@ class CameraXPreview(
override fun toggleRecording() {
if (currentRecording == null || recordingState is VideoRecordEvent.Finalize) {
startRecording()
if (config.isSoundEnabled) {
mediaSoundHelper.playStartVideoRecordingSound(onPlayComplete = {
startRecording()
})
listener.onVideoRecordingStarted()
} else {
startRecording()
}
} else {
currentRecording?.stop()
currentRecording = null
@ -571,7 +583,6 @@ class CameraXPreview(
recordingState = recordEvent
when (recordEvent) {
is VideoRecordEvent.Start -> {
playStartVideoRecordingSoundIfEnabled()
listener.onVideoRecordingStarted()
}
@ -598,12 +609,6 @@ class CameraXPreview(
}
}
private fun playStartVideoRecordingSoundIfEnabled() {
if (config.isSoundEnabled) {
mediaSoundHelper.playStartVideoRecordingSound()
}
}
private fun playStopVideoRecordingSoundIfEnabled() {
if (config.isSoundEnabled) {
mediaSoundHelper.playStopVideoRecordingSound()