gpt4 book ai didi

android - 在 android 中存储音频文件名时出现 IllegalStateException

转载 作者:行者123 更新时间:2023-11-29 17:54:37 25 4
gpt4 key购买 nike

我正在开发一个项目,用户通过单击录制按钮录制音频并通过单击停止按钮停止音频,当用户按下停止按钮时,弹出窗口打开要求输入名称,此名称的音频将存储在 SD 卡中。

现在我的问题是当用户按下停止按钮时发生错误,提示 java.lang.IllegalStateException。

下面是我的 Activity 代码。

public class Record_Audio extends Activity {

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 };
Chronometer myChronometer;
Handler seekHandler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.record_audio);

myChronometer = (Chronometer) findViewById(R.id.chronometer);
Button buttonStart = (Button) findViewById(R.id.btnStart);

buttonStart.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(Record_Audio.this,
"Start Recording With LongClick", Toast.LENGTH_SHORT)
.show();
enableButtons(true);
startRecording();
return true;
}
});

setButtonHandlers();
enableButtons(false);
setFormatButtonCaption();
}

private void setButtonHandlers() {
((Button) findViewById(R.id.btnStart)).setOnClickListener(btnClick);
((Button) findViewById(R.id.btnStop)).setOnClickListener(btnClick);
((Button) findViewById(R.id.btnFormat)).setOnClickListener(btnClick);
((Button) findViewById(R.id.allrecording)).setOnClickListener(btnClick);
}

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();
File file = new File(filepath, AUDIO_RECORDER_FOLDER);

if (!file.exists()) {
file.mkdirs();
}
return (file.getAbsolutePath() + "/" + Code.audioName + file_exts[currentFormat]);
}

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

try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
myChronometer.setBase(SystemClock.elapsedRealtime());
myChronometer.start();
}

private void stopRecording() {
if (null != recorder) {
recorder.stop();
recorder.reset();
recorder.release();

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(output_formats[currentFormat]);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);

displayAlertDialog();

recorder = null;
myChronometer.stop();
}
}

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 void displayAlertDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
Record_Audio.this);

// Setting Dialog Title
alertDialog.setTitle("Would you Like to save your Recording");

// Setting Dialog Message
alertDialog.setMessage("Enter Audio Name");

final EditText editTextAudioName = new EditText(Record_Audio.this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
editTextAudioName.setLayoutParams(lp);
alertDialog.setView(editTextAudioName);

// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.save_icon);

// Setting Positive "Yes" Button
alertDialog.setPositiveButton("Save",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog
Code.audioName = editTextAudioName.getText().toString().trim();
}
});

// Setting Negative "NO" Button
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog
dialog.cancel();
}
});
alertDialog.show();
}

private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
@Override
public void onError(MediaRecorder mr, int what, int extra) {
Toast.makeText(Record_Audio.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(Record_Audio.this,
"Warning: " + what + ", " + extra, Toast.LENGTH_SHORT)
.show();
}
};

private View.OnClickListener btnClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStart: {
Toast.makeText(Record_Audio.this, "Start Recording",
Toast.LENGTH_SHORT).show();
enableButtons(true);
startRecording();
break;
}
case R.id.btnStop: {
Toast.makeText(Record_Audio.this, "Stop Recording",
Toast.LENGTH_SHORT).show();
enableButtons(false);
stopRecording();
// displayFormatDialog();
break;
}
case R.id.btnFormat: {
displayFormatDialog();
break;
}

case R.id.allrecording: {
Intent intent = new Intent(Record_Audio.this, PlayAudio.class);
startActivity(intent);
break;
}
}
}
};
}

下面是我的logcat

E/AndroidRuntime(14328): FATAL EXCEPTION: main
E/AndroidRuntime(14328): java.lang.IllegalStateException
E/AndroidRuntime(14328): at android.media.MediaRecorder.stop(Native Method)
E/AndroidRuntime(14328): at iqualtech.skirr.Record_Audio.stopRecording(Record_Audio.java:113)
E/AndroidRuntime(14328): at iqualtech.skirr.Record_Audio.access$2(Record_Audio.java:111)
E/AndroidRuntime(14328): at iqualtech.skirr.Record_Audio$3.onClick(Record_Audio.java:221)
E/AndroidRuntime(14328): at android.view.View.performClick(View.java:3517)
E/AndroidRuntime(14328): at android.view.View$PerformClick.run(View.java:14155)
E/AndroidRuntime(14328): at android.os.Handler.handleCallback(Handler.java:605)
E/AndroidRuntime(14328): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(14328): at android.os.Looper.loop(Looper.java:154)
E/AndroidRuntime(14328): at android.app.ActivityThread.main(ActivityThread.java:4624)
E/AndroidRuntime(14328): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(14328): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(14328): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
E/AndroidRuntime(14328): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
E/AndroidRuntime(14328): at dalvik.system.NativeStart.main(Native Method)

最佳答案

在开始录制之前获取文件名,因为它即将到来 NULL...
所以打开你的弹出窗口并点击弹出按钮,开始录制:

recorder = new MediaRecorder(); 
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(output_formats[currentFormat]);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);

关于android - 在 android 中存储音频文件名时出现 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20348306/

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