case TelephonyManager.CALL_STATE_OFFHOOK: // 接听状态
String dir = Environment.getExternalStorageDirectory()
.getPath();
// 初始化录音器,多媒体api,参见MediaRecorder
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC); //设置音源,麦克
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);// 文件格式 3gp
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); //声音编码
File file = new File(dir, "temp.3gp");
System.out.println(file.getAbsolutePath());
recorder.setOutputFile(file.getAbsolutePath());
recorder.prepare();
recorder.start(); // Recording is now started
System.out.println("录音开始");
System.out.println("电话处于接听状态");
break;
挂断
case TelephonyManager.CALL_STATE_IDLE: // 闲置状态
if (null != recorder) {
System.out.println("尝试停止录音");
recorder.stop();
System.out.println("录音结束");
recorder.reset();
recorder.release();
recorder = null;
}
System.out.println("电话处于闲置状态");
break;
报错:
11-13 15:12:21.435: W/System.err(7293): java.lang.IllegalStateException
11-13 15:12:21.435: W/System.err(7293): at android.media.MediaRecorder.native_stop(Native Method)
11-13 15:12:21.435: W/System.err(7293): at android.media.MediaRecorder.stop(MediaRecorder.java:586)
11-13 15:12:21.435: W/System.err(7293): at com.ecarechina.phonelistener.PhoneListenerService$MyPhoneStateListener.onCallStateChanged(PhoneListenerService.java:56)
11-13 15:12:21.435: W/System.err(7293): at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:319)
11-13 15:12:21.435: W/System.err(7293): at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 15:12:21.435: W/System.err(7293): at android.os.Looper.loop(Looper.java:130)
11-13 15:12:21.435: W/System.err(7293): at android.app.ActivityThread.main(ActivityThread.java:3703)
11-13 15:12:21.435: W/System.err(7293): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 15:12:21.435: W/System.err(7293): at java.lang.reflect.Method.invoke(Method.java:507)
11-13 15:12:21.445: W/System.err(7293): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
11-13 15:12:21.445: W/System.err(7293): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
11-13 15:12:21.445: W/System.err(7293): at dalvik.system.NativeStart.main(Native Method)
------解决方案--------------------
来电话的时候 先判断一下recorder 是否在运行,
------解决方案--------------------
先判断recorder 是否是stop状态再stop,报错就是recorder调用stop的时候出错的