一个按钮在Kotlin中实现媒体录制的播放和暂停功能。

huangapple go评论91阅读模式
英文:

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()
                }
             }

huangapple
  • 本文由 发表于 2020年7月30日 00:51:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63158620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定