gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:04:15 25 4
gpt4 key购买 nike

我正在创建一个录制语音应用程序,当我试图停止在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com