【问题标题】:Error on MediaRecorder Stop : stop called in invalid state 4MediaRecorder Stop 错误:在无效状态 4 中调用停止
【发布时间】:2014-08-07 03:35:53
【问题描述】:

我正在创建一个录制语音应用程序,当我试图停止在 java 中录制调试控制台时说:“MediaRecorder stop 在无效状态下调用:4” 这是我的代码的一部分:

import java.io.File;
import java.io.IOException;
import com.androidexample.tabbar.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Tab1 extends Activity implements OnClickListener{
private static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp";
private static final String AUDIO_RECORDER_FILE_EXT_MP4 = ".mp4";
private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";

private MediaRecorder recorder = null;
private int currentFormat = 0;
private int output_formats[] = { MediaRecorder.OutputFormat.MPEG_4,
        MediaRecorder.OutputFormat.THREE_GPP };
private String file_exts[] = { AUDIO_RECORDER_FILE_EXT_MP4,
        AUDIO_RECORDER_FILE_EXT_3GP };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab1);

    Button btnStart =(Button)findViewById(R.id.btnStart);
    btnStart.setOnClickListener(this);

    Button btnStop =(Button)findViewById(R.id.btnStop);
    btnStop.setOnClickListener(this);

    Button btnFormat =(Button)findViewById(R.id.btnFormat);
    btnFormat.setOnClickListener(this);


    enableButtons(false);
    setFormatButtonCaption();
}

private void setButtonHandlers() {

}

private void enableButton(int id, boolean isEnable) {
    ((Button) findViewById(id)).setEnabled(isEnable);
}

private void enableButtons(boolean isRecording) {
    enableButton(R.id.btnStart, !isRecording);
    enableButton(R.id.btnFormat, !isRecording);
    enableButton(R.id.btnStop, isRecording);
}

private void setFormatButtonCaption() {
    ((Button) findViewById(R.id.btnFormat))
            .setText(getString(R.string.audio_format) + " ("
                    + file_exts[currentFormat] + ")");
}

private String getFilename() {
    //String filepath = Environment.getExternalStorageDirectory().getPath();
    String filepath =Environment.getExternalStorageDirectory() + File.separator 
            + Environment.DIRECTORY_DCIM + File.separator + "FILE_NAME";
    File file = new File(filepath, AUDIO_RECORDER_FOLDER);


    if (!file.exists()) {
        file.mkdirs();
    }

    return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]);
}

private void startRecording() {
    recorder = new MediaRecorder();

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(getFilename());

    recorder.setOnErrorListener(errorListener);
    recorder.setOnInfoListener(infoListener);

    try {
        recorder.prepare();
        recorder.start();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void stopRecording() {
    if (null != recorder){

            try {
                recorder.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            recorder.stop();
            recorder.reset();
            recorder.release();

            recorder = null;

    }
}

private void displayFormatDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    String formats[] = { "MPEG 4", "3GPP" };

    builder.setTitle(getString(R.string.choose_format_title))
            .setSingleChoiceItems(formats, currentFormat,
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            currentFormat = which;
                            setFormatButtonCaption();

                            dialog.dismiss();
                        }
                    }).show();
}

private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
    @Override
    public void onError(MediaRecorder mr, int what, int extra) {
        Toast.makeText(Tab1.this,
                "Error: " + what + ", " + extra, Toast.LENGTH_SHORT).show();
    }
};

private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
    @Override
    public void onInfo(MediaRecorder mr, int what, int extra) {
        Toast.makeText(Tab1.this,
                "Warning: " + what + ", " + extra, Toast.LENGTH_SHORT)
                .show();
    }
};

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
switch(v.getId()){
case R.id.btnStart : 
    Toast.makeText(Tab1.this, "Start Recording",
            Toast.LENGTH_SHORT).show();

    enableButtons(true);
    startRecording();

    break;
case R.id.btnStop : 
    Toast.makeText(Tab1.this, "Stop Recording",
            Toast.LENGTH_SHORT).show();
    enableButtons(false);
    stopRecording();

    break;

case R.id.btnFormat : 
    displayFormatDialog();

    break;



}

}

}

这是我的日志猫信息:

07-30 15:11:21.972: E/MediaRecorder(836): stop called in an invalid state: 4
07-30 15:11:21.972: D/AndroidRuntime(836): Shutting down VM
07-30 15:11:21.972: W/dalvikvm(836): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-30 15:11:21.982: E/AndroidRuntime(836): FATAL EXCEPTION: main
07-30 15:11:21.982: E/AndroidRuntime(836): java.lang.IllegalStateException
07-30 15:11:21.982: E/AndroidRuntime(836):  at android.media.MediaRecorder.stop(Native Method)

【问题讨论】:

  • 你好 amin..你能帮我解决我面临的有关 MediaRecorder 的问题吗?我面临着你所面临的同样的例外。这是链接。如果可以,请你帮助我。 stackoverflow.com/questions/49296213/…

标签: java android


【解决方案1】:
private void stopRecording() {
    if (null != recorder){
        try {
            recorder.prepare();  <-- Here's the problem

当您停止MediaRecorder 时,您不应该调用prepareprepare 方法是您在启动记录器之前调用的方法。请参阅MediaRecorder 文档中的the state diagram

【讨论】:

  • 感谢您的回复,首先我没有使用 prepare() 方法。没有这种方法,我又遇到了同样的问题:“在无效状态 4 中停止调用”:(
  • 在这种情况下,您的录制可能一开始就没有正确开始。在日志中查找其他异常或错误消息。
  • 谢谢迈克尔。问题是在 sdcard 上写权限。在清单文件中,我只是添加了权限规则并解决了问题。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-01
  • 1970-01-01
相关资源
最近更新 更多