不能停止录音?

时间:2011-10-02 15:43:12

标签: android

  

可能重复:
  Stop Recording and Context

我无法理解什么是错的?!!! 当电话响铃然后开始录音。 但是当调用结束时,我得到NullPointerException,因为MediRecorder为空,但记录仍在进行,直到我关闭窗口并出现错误。 我的问题与此Sound Recorder Widget doesnt stop recording

相同
public class Call extends BroadcastReceiver
{   
    private MediaRecorder mRecorder;    
    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))
        {          
            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mRecorder.setOutputFile("/sdcard/Record.3gp";);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            try {mRecorder.prepare();} 
            catch (IOException e){}
            mRecorder.start();

            // if I stop recording here, then everything is fine
            /* 
               try 
                    {Thread.sleep(300000);} 
               catch (InterruptedException e) 
                    {e.printStackTrace();}
               mRecorder.stop();
               mRecorder.release();
               mRecorder = null;    
            */

        }

        if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE))
        {
            if(mRecorder != null)  // But this always false. if I remove this condition -
            {
                 mRecorder.stop(); // - then here NullPointerException
                 mRecorder.release();
                 mRecorder = null;
            }
        }
    }
}

logcat的:

10-01 07:13:28.054: ERROR/AndroidRuntime(553): Uncaught handler: thread main exiting due to uncaught exception
10-01 07:13:29.134: ERROR/AndroidRuntime(553): java.lang.RuntimeException: Unable to start receiver xxx.xxx.xxx.Call: java.lang.NullPointerException
10-01 07:13:29.134: ERROR/AndroidRuntime(553):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2646)
10-01 07:13:29.134: ERROR/AndroidRuntime(553):     at android.app.ActivityThread.access$3100(ActivityThread.java:119)
10-01 07:13:29.134: ERROR/AndroidRuntime(553):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913)
10-01 07:13:29.134: ERROR/AndroidRuntime(553):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-01 07:13:29.134: ERROR/AndroidRuntime(553):     at android.os.Looper.loop(Looper.java:123)
10-01 07:13:29.134: ERROR/AndroidRuntime(553):     at android.app.ActivityThread.main(ActivityThread.java:4363)
10-01 07:13:29.134: ERROR/AndroidRuntime(553):     at java.lang.reflect.Method.invokeNative(Native Method)
10-01 07:13:29.134: ERROR/AndroidRuntime(553):     at java.lang.reflect.Method.invoke(Method.java:521)
10-01 07:13:29.134: ERROR/AndroidRuntime(553):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-01 07:13:29.134: ERROR/AndroidRuntime(553):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-01 07:13:29.134: ERROR/AndroidRuntime(553):     at dalvik.system.NativeStart.main(Native Method)
10-01 07:13:29.134: ERROR/AndroidRuntime(553): Caused by: java.lang.NullPointerException
10-01 07:13:29.134: ERROR/AndroidRuntime(553):     at xxx.xxx.xxx.Call.onReceive(Call.java:49)
10-01 07:13:29.134: ERROR/AndroidRuntime(553):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2637)
10-01 07:13:29.134: ERROR/AndroidRuntime(553):     ... 10 more
10-01 07:13:33.184: ERROR/audio_input(31): unsupported parameter: x-pvmf/media-input-node/cap-config-interface;valtype=key_specific_value
10-01 07:13:33.184: ERROR/audio_input(31): VerifyAndSetParameter failed

2 个答案:

答案 0 :(得分:1)

您正在使用与您的类MediaRecorder对象相同的名称在“if phone is ringing”语句中声明MediaRecorder对象的新实例,一旦您离开该if语句的范围,该实例就会丢失。只需删除行MediaRecorder前面的MediaRecorder mRecorder = new MediaRecorder();声明,您就应该好了。

答案 1 :(得分:1)

问题是你在MediaRecorder语句中实例化一个新的if,它掩盖了MediaRecorder mRecorder类成员。所以改变:

  if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
        {          
            MediaRecorder mRecorder = new MediaRecorder();
        .....
        }          

到此:

  if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
        {          
           mRecorder = new MediaRecorder();
        .....
        }