java - MediaRecorder 停止时出错 : stop called in invalid state 4

标签 java android

我正在创建一个录制语音应用程序,当我试图停止在 java 中录制调试控制台时说:“MediaRecorder 停止在无效状态下调用: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)

最佳答案

private void stopRecording() {
    if (null != recorder){
        try {
            recorder.prepare();  <-- Here's the problem

停止 MediaRecorder 时不应调用 prepareprepare 方法是您在启动 记录器之前调用的方法。引用the state diagramMediaRecorder 文档中。

关于java - MediaRecorder 停止时出错 : stop called in invalid state 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25041225/

相关文章:

android - 在android中创建通用布局结构?

java - Spark ClassCastException 无法将 FiniteDuration 的实例分配给 Scala 2.10.5 上的字段 RpcTimeout.duration

java - 当 java 类被注解 @WebService 时会发生什么?

java - 使用 GSON 解析 json 数据源

android - 在 android 上使用选定的 UID 模拟 nfc 卡

android - 从应用程序包生成 APK 时签署信息

使用 azure 移动服务(.Net 后端)的 Android 设备特定推送通知

java - 使用递归的整数除法——以及其他一些有趣的限制

java ? Collection 中的 super T 复制为什么正确工作

linux - 找到 native 库时出现 Java UnsatisfiedLinkError