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