Android MediaRecorder-非法状态异常
引发异常的代码部分:Android MediaRecorder-非法状态异常,android,mediarecorder,Android,Mediarecorder,引发异常的代码部分: recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); recorder.setOutputF
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setOutputFile("test.3gp");
recorder.prepare();
recorder.start();
E/MediaRecorder: start failed: -38
W/System.err: java.lang.IllegalStateException
W/System.err: at android.media.MediaRecorder.start(Native Method)
W/System.err: at com.example.amit.recording.RecordService.startRecording(RecordService.java:280)
W/System.err: at com.example.amit.recording.RecordService.onStartCommand(RecordService.java:141)
W/System.err: at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3422)
W/System.err: at android.app.ActivityThread.access$2200(ActivityThread.java:181)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:145)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6117)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
我得到了一个例外:
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setOutputFile("test.3gp");
recorder.prepare();
recorder.start();
E/MediaRecorder: start failed: -38
W/System.err: java.lang.IllegalStateException
W/System.err: at android.media.MediaRecorder.start(Native Method)
W/System.err: at com.example.amit.recording.RecordService.startRecording(RecordService.java:280)
W/System.err: at com.example.amit.recording.RecordService.onStartCommand(RecordService.java:141)
W/System.err: at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3422)
W/System.err: at android.app.ActivityThread.access$2200(ActivityThread.java:181)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:145)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6117)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
“recorder.start()”导致此问题。另外,当我停止记录时,得到以下信息:“在无效状态下调用stop:0”
顺便说一句-在清单中获得了我需要的所有权限。谢谢
编辑
我在“prepare()”和“start()”之前添加了“Thread.sleep(2000);”,现在它在正确的路径中保存了一个3gp文件,但是异常仍然存在——在start()和on stop()。为什么会这样 如果目标值大于22,则必须实现运行时权限。了解如何在android中实现运行时权限 我想您可能有文件路径问题。使用完整路径exEnvirement.getExternalStorage()+File.separator+test.3gp;还要检查存储权限。路径是正确的,我得到了存储权限:)似乎不是这个问题。请检查文件的路径并替换默认输出行<代码>mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.ThreeGPP)代码>不工作,谢谢尝试。我编辑了问题,请阅读此问题由此音频源
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL)引起代码>无法在您的设备和android上工作!将其更改为recorder.setAudioSource(MediaRecorder.AudioSource.MIC)代码>麦克风引发相同的异常。还可能我应该添加,当我停止记录时出现的错误:“在无效状态下停止调用:0”