尝试从蓝牙耳机录制音频时MediaRecorder崩溃

时间:2019-05-14 15:36:16

标签: android-bluetooth mediarecorder android-mediarecorder headset

我正尝试致电

  

mediaRecorder.start()

。它给出了这个错误。

> read.csv('http://xx.xx.xx.xx:8000/getSum?a=55&b=55')
[1] X.110.
<0 rows> (or 0-length row.names)
Warning message:
In read.table(file = file, header = header, sep = sep, quote = quote,  :
  incomplete final line found by readTableHeader on 'http://xx.xx.xx.xx:8000/getSum?a=55&b=55'

当我通过电话录音时,它可以正常工作,但是当我尝试通过蓝牙录音时,它会如上所述崩溃。我的代码如下:

java.lang.IllegalStateException
    at android.media.MediaRecorder.native_start(Native Method)
    at android.media.MediaRecorder.start(MediaRecorder.java:1143)
    at com.yyy.mystory.xxx.ui.camera.CameraVideoFragment$startRecordingVideo$2$onConfigured$1.run(CameraVideoFragment.kt:691)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6863)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

1 个答案:

答案 0 :(得分:0)

在寻找解决方案的人之后,我发现MediaPlayer不适合播放。这是由于媒体按钮事件被蓝牙设备的Volume_up和volume_down按钮调用了两次。只需针对KeyEvent.ACTION_UP或KeyEvent.ACTION_UP的特定事件启动mediaRecorder。这解决了我的问题。

 mMediaSession = MediaSessionCompat(this, TAG)
    mMediaSession?.setCallback(object : MediaSessionCompat.Callback() {
        override fun onMediaButtonEvent(mediaButtonIntent: Intent): Boolean {
            // Consume the media button event here. Should not send it to other apps.
            val event = mediaButtonIntent.getParcelableExtra(Intent.EXTRA_KEY_EVENT) as KeyEvent
            var action = event.action

            if (event.action == KeyEvent.ACTION_DOWN) {
                //call mediaplayer.start() from here
            }
            return true
        }
    })