媒体记录器启动时应用程序崩溃?

The application crashes when the media recorder starts?

我正在使用媒体录音机录制 android 中的通话。 mediaRecorder 有时会录制音频,有时不会录制音频。当我们播放录制的音频时 错误显示为 "The player doesn't supports this type of audio file ".

下面的代码用于录制音频

 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.access00(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.access00(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) 

请帮我解决这个问题。

非法状态表示您的媒体记录器尚未准备好进行记录。可能有几个原因。首先,确保您可以找到要播放的文件。如果没问题,请查看媒体记录器图表,了解您应该播放的状态。 https://developer.android.com/reference/android/media/MediaRecorder.html


另外,如果 mediaRecorder.prepare() 会发生什么?您应该将 mediaRecorder.start() 放入 try 块中。