我正在尝试实现以下功能,用于健身android应用程序:
我的问题是,你如何将视频和音频结合起来?对于视频,我使用ExoPlayer。ExoPlayer和SoundPool相结合对音频有好处吗?我是否应该为这两种情况创建多个ExoPlayer实例(如果是的话,我应该将AudioPlayer实例绑定到什么)?
发布于 2022-11-15 14:03:50
根据这的说法,我发现最好的解决方案是使用ExoPlayer来播放视频,使用SoundPool来表示短音频。
我使用一个观察者函数来侦听计时器事件(即勾选== 4000),并在每次事件发生时使用soundPool.play()。
对于视频,我收听计时器事件(播放、暂停、停止)并使用videoPlayer.play()。
参见下面的代码作为示例:
private fun subscribeObservers() {
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
launch {
sharedViewModel.timer.tick.collect { tick ->
// tick can only be multiple of 1000
if (tick == 0L) {
binding.startWorkoutTvTimer.text = ""
} else {
binding.startWorkoutTvTimer.text = (tick / 1000).toString()
}
if (tick == 4000L) {
sharedViewModel.soundPool?.play(sharedViewModel.countdownSoundId, 1F, 1F, 0, 0, 1F)
}
// SoundPool needs to be preloaded from previous Fragment
if (tick == 10000L) {
if (sharedViewModel.currentExercise == 0) {
sharedViewModel.soundPool?.play(sharedViewModel.beginWorkoutSoundId, 1F, 1F, 1, 0, 1F)
} else {
sharedViewModel.soundPool?.play(sharedViewModel.goSoundId, 1F, 1F, 1, 0, 1F)
}
}
}
}
launch {
sharedViewModel.timer.timerMode.collect { playerMode ->
when (playerMode) {
TimerMode.PLAYING -> {
binding.startWorkoutBtnPause.setBackgroundResource(R.drawable.ic_baseline_pause_24)
videoPlayer?.play()
}
TimerMode.PAUSED -> {
binding.startWorkoutBtnPause.setBackgroundResource(R.drawable.ic_baseline_play_arrow_24)
videoPlayer?.pause()
}
TimerMode.STOPPED -> {
}
}
}
}
}
}
}https://stackoverflow.com/questions/74364126
复制相似问题