2014-05-05 72 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,这样接收机的语音质量就不会降低。