安卓媒体录制:java.lang.RuntimeException:启动失败

8

我正在处理电话记录,但是当我开始录制电话时,它意外地停止了,还出现了错误MediaRecorder start fail -2147483648。我参考了这个答案链接,但是我不理解。请告诉我我的代码有什么问题?以下是我的代码。

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;
        }
    }
}

}

日志错误

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
3个回答

12

这个问题是由于这个音频源引起的

   recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);

不适用于您的设备和安卓系统!请更改。
   recorder.setAudioSource(MediaRecorder.AudioSource.MIC);

或其他来源。

另外,您可以通过将recorderstart()更改为try/catch和IllegalStateException、Exception相同来删除FC。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接