2017-07-03 78 views
0

我正在使用媒體錄音機在android中錄製電話。 mediarecorder有時正在錄製音頻,有些時候它不錄製音頻。當我們播放錄製的音頻時顯示爲「播放器不支持這種類型的音頻文件」的錯誤。媒體記錄器啓動時應用程序崩潰了嗎?

下面的代碼是在錄製音頻以下

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)  

顯示

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的錯誤,請幫助我如何解決這個問題。

回答

1

非法狀態表示您的媒體記錄器尚未準備好錄製。這可能有幾個原因。首先,確保您可以訪問您想要播放的文件。如果沒問題,請檢查媒體記錄器圖表,確定您應該播放哪個狀態。 https://developer.android.com/reference/android/media/MediaRecorder.html


另外,如果mediaRecorder.prepare()會發生什麼?您應該將mediaRecorder.start()放入try塊。

+0

錄製的文件存儲在外部storage.when我點擊播放「播放器不支持此音頻」錯誤顯示 –

+0

另外我插入mediaRecorder.start()在try塊 –