我正在使用媒体记录器记录android中的调用。有些时候,mediaRecorder正在录制音频,有时候它不会录制音频。当我们播放录制的音频时错误显示为"播放器不支持此类音频文件"。
以下代码用于录制音频
try {
f1 = File.createTempFile("Sound", ".mp3", dir);
} catch (IOException e) {
e.printStackTrace();
}
fileName = f1.getName();
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mediaRecorder.setOutputFile(f1.getAbsolutePath());
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaRecorder.start();
recordstarted = true;
我的logcat错误如下所示
E/MediaRecorder: start failed: -38
07-03 11:10:23.718 8558-8558/com.seyali.callLog E/UncaughtException: java.lang.RuntimeException: Unable to start receiver com.seyali.callLog.receiver.CallReceiver: java.lang.IllegalStateException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2593)
at android.app.ActivityThread.access$1700(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1369)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException
at android.media.MediaRecorder.start(Native Method)
at com.seyali.callLog.receiver.CallReceiver.onReceive(CallReceiver.java:175)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2586)
at android.app.ActivityThread.access$1700(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1369)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
请帮我解决这个问题。
答案 0 :(得分:2)
非法状态表示您的媒体录制器尚未准备好录制。可能有几个原因。首先,确保您可以访问要播放的文件。如果没问题,请查看介质记录器图表,了解您应该播放的状态。 https://developer.android.com/reference/android/media/MediaRecorder.html
此外,如果mediaRecorder.prepare()
会发生什么?您应该将mediaRecorder.start()
放入try块。