Java 第二次MediaRecorder非法状态异常

Java 第二次MediaRecorder非法状态异常,java,android,exception,illegalstateexception,Java,Android,Exception,Illegalstateexception,这是不言自明的。mediarecorder类的start方法在第二次调用时抛出illegalstateexception。我第一次按下按钮,一切正常。第二次没有。我穿了很多东西,问出了什么问题。我试着从关于媒体录音机的其他问题中寻找解决方案,但没有任何帮助。权限放在清单中。这显然不是问题所在,因为它第一次起作用 主要 录音班 public class Recorder extends AsyncTask<Void,Integer,Void> { public static fina

这是不言自明的。mediarecorder类的start方法在第二次调用时抛出illegalstateexception。我第一次按下按钮,一切正常。第二次没有。我穿了很多东西,问出了什么问题。我试着从关于媒体录音机的其他问题中寻找解决方案,但没有任何帮助。权限放在清单中。这显然不是问题所在,因为它第一次起作用

主要

录音班

public class Recorder extends AsyncTask<Void,Integer,Void> {

public static final String LOG_TAG = "NTS:";
static final int MAX_LENGTH = 30;

MediaRecorder mRecorder;
String mFileName = null;

ProgressBar recordBar;
Activity parentActivity;

int mLength = 0;



public Recorder(Activity activity,ProgressBar progressBar) {
    super();
    mFileName = activity.getFilesDir().getAbsolutePath();
    mFileName += "/recentRecord.3gp";

    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mRecorder.setOutputFile(mFileName);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    recordBar = progressBar;
    parentActivity =activity;

}

public void startRecording(){

    try {
        mRecorder.prepare();
    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed");
    }

    mRecorder.start();

}

public void stopRecording(){

    mRecorder.stop();
    mRecorder.reset();
    mRecorder.release();
    mRecorder = null;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    startRecording();

}

@Override
protected Void doInBackground(Void... params) {
    long secondMillis = System.currentTimeMillis();
   while(mLength < MAX_LENGTH){
       if((System.currentTimeMillis() - secondMillis) >= 1000){
           secondMillis = System.currentTimeMillis();
           mLength++;
           publishProgress(mLength);



       }
   }

    stopRecording();
    return null;
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    stopRecording();
}

@Override
protected void onProgressUpdate(Integer... values) {
    super.onProgressUpdate(values);
    recordBar.setProgress(values[0]);



}

@Override
protected void onCancelled(Void aVoid) {
    super.onCancelled(aVoid);
    stopRecording();

}

@Override
protected void onCancelled() {
    super.onCancelled();
    stopRecording();
}


 }

为什么在onPostExecute中调用stopRecording方法时却在doInBackground中调用它。如果在doInBackground中录制完成,您只需要在onPostExecute中调用stopRecording并从doInBackground中删除调用。我认为这不是问题所在。我删除了它,结果是一样的
public class Recorder extends AsyncTask<Void,Integer,Void> {

public static final String LOG_TAG = "NTS:";
static final int MAX_LENGTH = 30;

MediaRecorder mRecorder;
String mFileName = null;

ProgressBar recordBar;
Activity parentActivity;

int mLength = 0;



public Recorder(Activity activity,ProgressBar progressBar) {
    super();
    mFileName = activity.getFilesDir().getAbsolutePath();
    mFileName += "/recentRecord.3gp";

    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mRecorder.setOutputFile(mFileName);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    recordBar = progressBar;
    parentActivity =activity;

}

public void startRecording(){

    try {
        mRecorder.prepare();
    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed");
    }

    mRecorder.start();

}

public void stopRecording(){

    mRecorder.stop();
    mRecorder.reset();
    mRecorder.release();
    mRecorder = null;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    startRecording();

}

@Override
protected Void doInBackground(Void... params) {
    long secondMillis = System.currentTimeMillis();
   while(mLength < MAX_LENGTH){
       if((System.currentTimeMillis() - secondMillis) >= 1000){
           secondMillis = System.currentTimeMillis();
           mLength++;
           publishProgress(mLength);



       }
   }

    stopRecording();
    return null;
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    stopRecording();
}

@Override
protected void onProgressUpdate(Integer... values) {
    super.onProgressUpdate(values);
    recordBar.setProgress(values[0]);



}

@Override
protected void onCancelled(Void aVoid) {
    super.onCancelled(aVoid);
    stopRecording();

}

@Override
protected void onCancelled() {
    super.onCancelled();
    stopRecording();
}


 }