我正在创建一个记录来电和外拨电话的应用。录音应该在通话结束时停止。但是我得到非法状态异常。例外是mediarecorder.stop()
。&#34;在无效状态4和#34之间停止呼叫; < /strong>。我不知道这意味着什么。这是我第一次使用MediaRecorder。我已经检查了类似的问题..但是他们的答案对我没有帮助。感谢您的帮助。谢谢。这个问题Error on MediaRecorder Stop : stop called in invalid state 4没有帮助我,因为该问题中的问题是用户名为在调用 stop()方法之前准备好()。它与我的情况不太相似。
代码:
import android.Manifest;
import android.content.Context;
import android.media.MediaRecorder;
import android.support.v4.app.ActivityCompat;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
public class TeleListener extends PhoneStateListener {
private Context context;
boolean isRinging=false;
String filepath;
String internalfilename="AUD";
public TeleListener(Context context,String filepath) {
this.context = context;
this.filepath=filepath;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
MediaRecorder mediaRecorder= new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(filepath+internalfilename);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
if (isRinging) {
mediaRecorder.stop();
mediaRecorder.reset();
mediaRecorder.release();
isRinging=false;
}
Toast.makeText(context, "Idle", Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
isRinging=false;
Toast.makeText(context, "Offhook", Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
isRinging=true;
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(context, "Ringing", Toast.LENGTH_SHORT).show();
break;
}
}
}
例外:
E/MediaRecorder: stop called in an invalid state: 4
03-E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException
at android.media.MediaRecorder._stop(Native Method)
at android.media.MediaRecorder.stop(MediaRecorder.java:967)
at TeleListener.onCallStateChanged(TeleListener.java:43)
at android.telephony.PhoneStateListener$1.handleMessage
(PhoneStateListener.java:323)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案 0 :(得分:-2)
private void stopRecording() {
if (null != recorder){
try {
recorder.prepare();
当您停止 MediaRecorder 时,您不应该致电准备。 准备方法是您在启动录像机之前调用的方法。请参阅 MediaRecorder 文档中的状态图。