其他分享
首页 > 其他分享> > android-MediaRecorder:启动失败

android-MediaRecorder:启动失败

作者:互联网

我已经用谷歌搜索了,但是没有找到任何解决方案

我正在记录来电和去电

代码可以很好地与拨出电话配合使用,但在拨入电话时会产生异常

    recorder = new MediaRecorder();

    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File(sdCard.getAbsolutePath() + "/OK");
    dir.mkdirs();

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
 recorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/OK/"+"yes"+".3gpp");
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    try {
       recorder.prepare();
       recorder.start();
    } catch (IllegalStateException e) {
         Log.d("illegal",e.toString());
    } catch (IOException e) {
         Log.d("io",e.toString());
   }

logcat的

02-23 01:43:08.346  11231-11231/com.example.myapps.acr **I/record﹕ start**
02-23 01:43:08.426  11231-11231/com.example.myapps.acr **E/MediaRecorder﹕ start failed: -38**
02-23 01:43:08.426  11231-11231/com.example.myapps.acr **D/illegal﹕ java.lang.IllegalStateException**

请帮助为什么它不能在拨入电话上工作.

解决方法:

recorder.prepare();
Thread.sleep(1000);
recorder.start();

对于来电,需要花费一些时间进行准备.

保持准备1秒钟,一切正常.

标签:android,mediarecorder
来源: https://codeday.me/bug/20191012/1900511.html