问题
I am creating an app that records phone calls, both incoming and outgoing. The recording should stop when call ends.But I get an Illegal State Exception.Th exception is at mediarecorder.stop()
."Stop called between invalid state 4".I have no idea what that means.This is the first time I am using MediaRecorder. I have checked similar questions..but their answers did not help me.Your help would be appreciated.Thanks in advance..This question Error on MediaRecorder Stop : stop called in invalid state 4 did not help me as the problem in that question was the user called prepare() before he called the stop() method. It is not similar to my case.
Code:
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;
}
}
}
Exception:
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)
回答1:
private void stopRecording() {
if (null != recorder){
try {
recorder.prepare();
You should not be calling prepare when you're stopping the MediaRecorder. The prepare method is something you call prior to starting the recorder. Refer to the state diagram in the MediaRecorder documentation.
来源:https://stackoverflow.com/questions/49296213/android-mediarecorder-exception-when-stopped