使用mediaRecorder录制时出现空例外

时间:2012-12-11 15:34:26

标签: android broadcastreceiver mediarecorder

我正在打电话录音申请。我的问题是,收到电话时录音工作正常,但不会停止录音。我的代码和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.MIC);
                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;
            }
    }

} logcat的

12-11 22:53:51.502: E/MediaRecorder(2831): stop called in an invalid state: 1
12-11 22:53:51.502: D/AndroidRuntime(2831): Shutting down VM
12-11 22:53:51.502: W/dalvikvm(2831): threadid=1: thread exiting with uncaught exception (group=0x2b544300)
12-11 22:53:51.502: E/AndroidRuntime(2831): FATAL EXCEPTION: main
12-11 22:53:51.502: E/AndroidRuntime(2831): java.lang.RuntimeException: Unable to start receiver com.example.callrecorder.IncomingCallReceiver: java.lang.IllegalStateException
12-11 22:53:51.502: E/AndroidRuntime(2831): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2362)
12-11 22:53:51.502: E/AndroidRuntime(2831): at android.app.ActivityThread.access$1500(ActivityThread.java:142)
12-11 22:53:51.502: E/AndroidRuntime(2831): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
12-11 22:53:51.502: E/AndroidRuntime(2831): at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 22:53:51.502: E/AndroidRuntime(2831): at android.os.Looper.loop(Looper.java:137)
12-11 22:53:51.502: E/AndroidRuntime(2831): at android.app.ActivityThread.main(ActivityThread.java:4931)
12-11 22:53:51.502: E/AndroidRuntime(2831): at java.lang.reflect.Method.invokeNative(Native Method)
12-11 22:53:51.502: E/AndroidRuntime(2831): at java.lang.reflect.Method.invoke(Method.java:511)
12-11 22:53:51.502: E/AndroidRuntime(2831): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
12-11 22:53:51.502: E/AndroidRuntime(2831): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
12-11 22:53:51.502: E/AndroidRuntime(2831): at dalvik.system.NativeStart.main(Native Method)
12-11 22:53:51.502: E/AndroidRuntime(2831): Caused by: java.lang.IllegalStateException
12-11 22:53:51.502: E/AndroidRuntime(2831): at android.media.MediaRecorder.stop(Native Method)
12-11 22:53:51.502: E/AndroidRuntime(2831): at com.example.callrecorder.IncomingCallReceiver.onReceive(IncomingCallReceiver.java:63)
12-11 22:53:51.502: E/AndroidRuntime(2831): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2355)
12-11 22:53:51.502: E/AndroidRuntime(2831): ... 10 more

1 个答案:

答案 0 :(得分:0)

当我们切断或结束通话并且你开始录制时,你发错了TelephonyManager.EXTRA_STATE_OFFHOOK

当您选择呼叫时,TelephonyManager.EXTRA_STATE_IDLE将会触发,这将在TelephonyManager.EXTRA_STATE_OFFHOOK之前触发,并在TelephonyManager.EXTRA_STATE_IDLE状态下触发mRecorder.stop();这是null ...而没有初始化。

你的mRecorder也应该是静态的,因为BroadcastReceiver会为不同的事件调用多个。