MediaRecorder启动失败-2147483648

时间:2012-12-18 12:30:50

标签: android mediarecorder

我打算用这个应用程序记录调用。但是当我将audioSource设置为MediaRecorder.AudioSource.VOICE_CALL时,它会产生错误,但是当audioSource设置为MediaRecorder.AudioSource.MIC时,它可以正常工作。我不确定问题出在哪里。问题的logcat如下。非常感谢任何形式的帮助。感谢。

public class IncomingCallReceiver extends BroadcastReceiver {
private MediaRecorder mRecorder;
@Override
public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if(null == bundle)
                return;
        String state = bundle.getString(TelephonyManager.EXTRA_STATE);
        if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
        {
        }
        else if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK)){
            Log.i("TelephonyManager", "Call picked up");
            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mRecorder.setAudioEncodingBitRate(16);
            mRecorder.setAudioSamplingRate(44100);
            mRecorder.setOutputFile("/sdcard/Recording/callrecord.mp4");
            try{
                mRecorder.prepare();
            }
            catch(IOException e){
            }
            mRecorder.start();
            Log.i("StartRecordingCall", "Recording Call end");
        }
        else if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)){
            Log.i("TelephonyManager", "Call hunged up");
            mRecorder.stop();
            mRecorder.release();
            mRecorder=null;
        }
}

/

    12-18 20:15:56.755: E/MediaRecorder(1577): start failed: -2147483648
    12-18 20:15:56.755: D/AndroidRuntime(1577): Shutting down VM
    12-18 20:15:56.755: W/dalvikvm(1577): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
    12-18 20:15:56.765: E/AndroidRuntime(1577): FATAL EXCEPTION: main
    12-18 20:15:56.765: E/AndroidRuntime(1577): java.lang.RuntimeException: start failed.
    12-18 20:15:56.765: E/AndroidRuntime(1577):     at android.media.MediaRecorder.start(Native Method)
    12-18 20:15:56.765: E/AndroidRuntime(1577):     at com.example.callrecorder.MainActivity.startRecording(MainActivity.java:447)
    12-18 20:15:56.765: E/AndroidRuntime(1577):     at com.example.callrecorder.MainActivity.onClick(MainActivity.java:279)
    12-18 20:15:56.765: E/AndroidRuntime(1577):     at android.view.View.performClick(View.java:3534)
    12-18 20:15:56.765: E/AndroidRuntime(1577):     at android.view.View$PerformClick.run(View.java:14263)
    12-18 20:15:56.765: E/AndroidRuntime(1577):     at android.os.Handler.handleCallback(Handler.java:605)
    12-18 20:15:56.765: E/AndroidRuntime(1577):     at android.os.Handler.dispatchMessage(Handler.java:92)
    12-18 20:15:56.765: E/AndroidRuntime(1577):     at android.os.Looper.loop(Looper.java:137)
    12-18 20:15:56.765: E/AndroidRuntime(1577):     at android.app.ActivityThread.main(ActivityThread.java:4441)
    12-18 20:15:56.765: E/AndroidRuntime(1577):     at java.lang.reflect.Method.invokeNative(Native Method)
    12-18 20:15:56.765: E/AndroidRuntime(1577):     at java.lang.reflect.Method.invoke(Method.java:511)
    12-18 20:15:56.765: E/AndroidRuntime(1577):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    12-18 20:15:56.765: E/AndroidRuntime(1577):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    12-18 20:15:56.765: E/AndroidRuntime(1577):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:3)

您运行应用程序的设备可能根本不支持语音通话录音,或者它不喜欢您尝试设置的一个或多个参数。
例如,您可以尝试使用8000 Hz采样率而不是44100 Hz(无论如何44100 Hz对AMR-NB都没有意义)并完全取消对setAudioEncodingBitRate的调用。

您的代码的另一个潜在问题是您的MediaRecorder对象中有BroadcastReceiver个实例,并且尚未将其声明为静态。以下是Android文档对BroadcastReceivers的评价:

  

BroadcastReceiver对象仅在对onReceive(Context,Intent)的调用期间有效。一旦您的代码从此函数返回,系统会认为该对象已完成且不再有效。

换句话说,当您收到包含MediaRecorder的广播时,您期望在那里的EXTRA_STATE_IDLE实例可能实际上不再存在,因为您位于不同的BroadcastReceiver实例中创建MediaRecorder的那个。