我想知道如何使用一个按钮来播放和暂停MediaRecorder
我的意图是按下按钮并开始录制,当我再次按下该按钮时,它将暂停并录制。
buttonRecordandplaying.setOnClickListener {
<!--Code to play-->
grabacion = MediaRecorder()
grabacion.setAudioSource(MediaRecorder.AudioSource.MIC)
grabacion.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
grabacion.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB)
grabacion.setOutputFile(archivoSalida)
try {
grabacion.prepare()
grabacion.start()
}catch (e:IOException){
}
Toast.makeText(this,"Grabando",Toast.LENGTH_SHORT).show()
}
<-- code on puse-->
grabacion.stop()
grabacion.release()
Toast.makeText(this, "Grabacin finaliza", Toast.LENGTH_SHORT).show()
最佳答案
您可以添加1个值来检查播放状态
private boolean isPlaying = false
然后在播放器播放时将其设置为真。并在停止播放器时将其设置为假。您也可以检查该值并更改播放按钮图标。
buttonRecordandplaying.setOnClickListener {
if(!isPlaying) {
<!--Code to play-->
grabacion = MediaRecorder()
grabacion.setAudioSource(MediaRecorder.AudioSource.MIC)
grabacion.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
grabacion.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB)
grabacion.setOutputFile(archivoSalida)
try {
grabacion.prepare()
grabacion.start()
isPlaying = true
}catch (e:IOException){
}
Toast.makeText(this,"Grabando",Toast.LENGTH_SHORT).show()
}else {
<-- code on puse-->
isPlaying = false
grabacion.stop()
grabacion.release()
Toast.makeText(this, "Grabacin finaliza", Toast.LENGTH_SHORT).show()
}
}
关于java - 一键播放和暂停Kotlin中的媒体录制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63158620/