英文:
one button to play and pause with media recording in kotlin
问题
我想知道如何使用一个按钮来播放和暂停我的MediaRecorder。
我的意图是按下按钮开始录制,再次按下它会暂停录制。
buttonRecordandplaying.setOnClickListener {
// 播放的代码
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, "正在录制", Toast.LENGTH_SHORT).show()
}
// 暂停的代码
grabacion.stop()
grabacion.release()
Toast.makeText(this, "录制结束", Toast.LENGTH_SHORT).show()
注意:这里只提供了代码的翻译,没有其他额外的内容。
英文:
I would like to know how I can do it with one button to play and pause my MediaRecorder
My intention is to press the button and start recording and when I press it again it will pause and record.
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
得分: 1
你可以添加一个值来检查播放状态:
private boolean isPlaying = false;
然后,当播放器正在播放时将其设置为true。当停止播放器时将其设置为false。还可以检查该值并更改播放按钮图标:
buttonRecordandplaying.setOnClickListener {
if (!isPlaying) {
// 播放的代码
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 {
// 暂停的代码
isPlaying = false
grabacion.stop()
grabacion.release()
Toast.makeText(this, "Grabación finalizada", Toast.LENGTH_SHORT).show()
}
}
英文:
You can add 1 value for checking playing state
private boolean isPlaying = false
Then make it true when the player is playing. And make it false when you stop the player.
Also you can check that value and change the play button icon.
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()
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论