0

我正在做一个通话录音应用程序。我的问题是接到电话时录音工作正常,但不会停止录音。我的代码和 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;
            }
    }

} 日志猫

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
4

1 回答 1

0

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

并且TelephonyManager.EXTRA_STATE_IDLE会在您接听电话时触发,这将在您访问 mRecorder.stop()之前TelephonyManager.EXTRA_STATE_OFFHOOK和状态下触发;TelephonyManager.EXTRA_STATE_IDLE这是空的......并且没有初始化。

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

于 2012-12-11T15:47:44.523 回答