2014-05-05 110 views
6

我正在記錄一個電話。當我開始記錄一個電話,然後不幸停止。 &它給出錯誤MediaRecorder啓動失敗-2147483648。我提到這個答案link。但我不明白。請告訴我在我的代碼中有什麼問題?這是我的代碼。Android Media Recording:java.lang.RuntimeException:啓動失敗

public class IncomingCall extends BroadcastReceiver { 

Context pcontext; 
private static MediaRecorder recorder; 
private boolean recordedStart = false; 

@SuppressWarnings("unchecked") 
public void onReceive(Context context, Intent intent) { 
    pcontext = context; 
    recorder = new MediaRecorder(); 

try { 
      TelephonyManager tmgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
      MyPhoneStateListener PhoneListener = new MyPhoneStateListener(); 
      tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE); 

    } catch (Exception e) { 
     Log.e("Phone Receive Error", " " + e); 
    } 

} 

private class MyPhoneStateListener extends PhoneStateListener { 
    public void onCallStateChanged(int state, String incomingNumber) { 



     switch (state) { 
     case TelephonyManager.CALL_STATE_RINGING: 
      Log.e("MyPhoneListener",state+" incoming no:"+incomingNumber); 
      Log.e("CALL_STATE_RINGING", "CALL_STATE_RINGING"); 

      break; 

     case TelephonyManager.CALL_STATE_OFFHOOK: 

      recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); 
      recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); 
      recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 
      recorder.setOutputFile(Environment.getExternalStorageDirectory()+"/MyRecorder.mp3"); 
      Log.e("Path", ""+Environment.getExternalStorageDirectory()+"/MyRecorder.mp3"); 
      try { 
       recorder.prepare(); 
       recorder.start(); 
       recordedStart = true; 
       Log.e("Start", "Recorder Start"); 
      } catch (IllegalStateException | IOException e) { 
       // TODO Auto-generated catch block 
       Log.e("Error", ""+e); 
      } 

      break; 
     case TelephonyManager.CALL_STATE_IDLE: 
      Log.e("CALL_STATE_IDLE", "CALL_STATE_IDLE"); 
      if (recordedStart == true) { 
       recorder.stop(); 
       recorder.release(); 
       recordedStart = false; 
       Log.e("Stop", "Recorder Stop"); 
      } 
      break; 
     } 
    } 
} 

}

logcat的錯誤

05-05 10:57:23.771: E/MediaRecorder(12812): start failed: -2147483648 
05-05 10:57:23.771: D/AndroidRuntime(12812): Shutting down VM 
05-05 10:57:23.771: W/dalvikvm(12812): threadid=1: thread exiting with uncaught exception (group=0x416bdd40) 
05-05 10:57:23.774: E/AndroidRuntime(12812): FATAL EXCEPTION: main 
05-05 10:57:23.774: E/AndroidRuntime(12812): Process: web.revolution.autocallanswer, PID: 12812 
05-05 10:57:23.774: E/AndroidRuntime(12812): java.lang.RuntimeException: start failed. 
05-05 10:57:23.774: E/AndroidRuntime(12812): at android.media.MediaRecorder.start(Native Method) 
05-05 10:57:23.774: E/AndroidRuntime(12812): at web.revolution.autocallanswer.IncomingCall$MyPhoneStateListener.onCallStateChanged(IncomingCall.java:98) 
05-05 10:57:23.774: E/AndroidRuntime(12812): at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:389) 
05-05 10:57:23.774: E/AndroidRuntime(12812): at android.os.Handler.dispatchMessage(Handler.java:102) 
05-05 10:57:23.774: E/AndroidRuntime(12812): at android.os.Looper.loop(Looper.java:136) 
05-05 10:57:23.774: E/AndroidRuntime(12812): at android.app.ActivityThread.main(ActivityThread.java:5102) 
05-05 10:57:23.774: E/AndroidRuntime(12812): at java.lang.reflect.Method.invokeNative(Native Method) 
05-05 10:57:23.774: E/AndroidRuntime(12812): at java.lang.reflect.Method.invoke(Method.java:515) 
05-05 10:57:23.774: E/AndroidRuntime(12812): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
05-05 10:57:23.774: E/AndroidRuntime(12812): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
05-05 10:57:23.774: E/AndroidRuntime(12812): at dalvik.system.NativeStart.main(Native Method) 
05-05 10:57:25.586: I/Process(12812): Sending signal. PID: 12812 SIG: 9 
05-05 10:57:27.052: E/CALL_STATE_IDLE(13255): CALL_STATE_IDLE 

回答

9

這個問題,通過這種音頻源造成

recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); 

不是你的設備和Android上工作! 改爲

recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 

或其他來源。

也可以刪除通過改變recorderstart FC()來的try/catch與IllegalStateException異常,異常與此相同

 try { 
     recorder.prepare(); 

    } catch (IllegalStateException e) { 
     // TODO Auto-generated catch block 
     Log.d("ERROR ","IllegalStateException"); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     Log.d("ERROR ","IOException"); 
     e.printStackTrace(); 
    } 
    try { 
    recorder.start(); 
    } catch (Exception e) { 

    } 

我有這個問題我自己,這是偉大的工作電話錄音,但具有與問題接收者的語音質量是非常低,如果你也找到這個電話的解決方案。

1

當我試圖在我的Nexus5上錄製VOICE_CALL/VOICE_DOWNLINK/VOICE_UPLINK時,我遇到了同樣的問題。我不想長時間說話,這個問題在Android下的許多智能手機上都是猶豫不決的。但有幾種設備可以正常使用這種音頻源。其中之一是三星專門Note3(Android 4.3)。我在該設備上試過,一切都很好。據我所知,當更新4.4.2/3達到Note3時,這個問題會再次出現,可能是因爲與美國法律的衝突。但是現在你可以在Note3上使用這個特性而沒有任何條件。

如果您使用其它設備此音源你必須阻止應用程序的崩潰與此代碼:

try { 
    mRecorder.start(); 
    } catch (Throwable t) { 
     t.printStackTrace(); 
     Log.w(LOG_TAG, t); 
    } 

,那麼你不會任何問題,這個問題...

什麼其他設備可以很好地處理這個來源,我不知道。我只在Note3上測試過。有趣的是它如何在Galaxy3/4/5上工作。

0

我知道它遲到了,但對於其他有同樣問題的人,我的解決方案是將音頻源設置爲MediaRecorder.AudioSource.VOICE_COMMUNICATION,這樣接收機的語音質量就不會降低。