【问题标题】:Android Media Recording: java.lang.RuntimeException: start failedAndroid 媒体录制:java.lang.RuntimeException:启动失败
【发布时间】:2014-05-05 06:03:50
【问题描述】:

我正在记录一个电话。当我开始记录电话时,不幸的是它停止了。 & 它给出了错误 MediaRecorder start fail -2147483648。我参考这个答案link。但我不明白。请告诉我我的代码有什么问题?这是我的代码。

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

【问题讨论】:

    标签: android audio-recording phone-call


    【解决方案1】:

    这个音频源引起的问题

       recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
    

    无法在您的设备和 Android 上运行! 将其更改为

       recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    

    或其他来源。

    您也可以通过更改 recorderstart() 来删除 FC 以尝试/捕获 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) {
    
        }
    

    我自己也遇到过这个问题,这对于通话录音非常有用,但是如果您也找到此电话的解决方案,则接收器语音质量问题非常低。

    【讨论】:

      【解决方案2】:

      当我尝试在我的 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 上的工作原理。

      【讨论】:

        【解决方案3】:

        我知道回答晚了,但是对于遇到同样问题的其他人,我的解决方案是设置音频源为MediaRecorder.AudioSource.VOICE_COMMUNICATION,这样接收者的语音质量就不会降低。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-11-17
          • 2016-01-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多