gpt4 book ai didi

android - 录制音频文件时 MediaRecorder.stop() 方法出错

转载 作者:行者123 更新时间:2023-12-01 21:59:44 26 4
gpt4 key购买 nike

AddRecordingActivity.java

when i clicked on stop recording button then an application is unfortunately stoped

<小时/>
public class AddRecordingActivity extends Activity {
Button btnHome, btnRemoveRecording;
ImageView ivVideoRecord, ivAudioRecord, ivStopRecording;
Chronometer stopWatch;
Context con;
private String outputFile = null;
MediaRecorder audioRecorder;
TextView tvAudioFileName;
EditText etAudioFileName;
Button btnYes,btnNo;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
declareData();
clickedOnAudioRecordButton();
clickedOnVideoRecordButton();
clickedOnStopRecordingButton();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_add_recording, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
SimpleFactory factory = new SimpleFactory();
return factory.onCreateView(name, context, attrs);
}

public void declareData(){
FontManager.getInstance().initialize(this, R.xml.fonts);
setContentView(R.layout.activity_add_recording);
con = AddRecordingActivity.this;
btnHome = (Button) findViewById(R.id.bHome);
btnRemoveRecording = (Button) findViewById(R.id.bRemoveRecording);
ivAudioRecord = (ImageView) findViewById(R.id.ivAudioRecording);
ivVideoRecord = (ImageView) findViewById(R.id.ivVideoRecording);
ivStopRecording = (ImageView) findViewById(R.id.ivStopRecording);
stopWatch = (Chronometer) findViewById(R.id.stopWatch);

Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd-hh-mm-ss", now);
outputFile = Environment.getExternalStorageDirectory().toString()+"/recording-"+now+".3gp";
// outputFile = Environment.getExternalStorageDirectory().getAbsolutePath()+"/recording.3gp";

audioRecorder = new MediaRecorder();
audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
audioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);

ivStopRecording.setEnabled(false);
ivStopRecording.setAlpha(0.4f);
btnHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickedOnHomeButton();
}
});
}

public void clickedOnAudioRecordButton(){
ivAudioRecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ivStopRecording.setEnabled(true);
ivStopRecording.setAlpha(1.0f);
stopWatch.start();

try {
audioRecorder.prepare();
audioRecorder.start();
Log.d("TAG","Audio started Successfully.");
} catch (IOException e) {
e.printStackTrace();
}

Toast.makeText(con, "Recording started", Toast.LENGTH_LONG).show();
}
});
}

public void clickedOnVideoRecordButton(){
ivVideoRecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ivStopRecording.setEnabled(true);
ivStopRecording.setAlpha(1.0f);
stopWatch.start();
}
});
}

public void clickedOnStopRecordingButton(){
ivStopRecording.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd-hh-mm-ss", now);
outputFile = Environment.getExternalStorageDirectory().toString()+"/recording-"+now+".3gp";
final Dialog renameDialog = new Dialog(con);
renameDialog.setContentView(R.layout.row_rename_dialog);
renameDialog.setTitle("Rename");
tvAudioFileName = (TextView) renameDialog.findViewById(R.id.tvRecordingName);
etAudioFileName = (EditText) renameDialog.findViewById(R.id.etAudioRecordingName);
btnYes = (Button) renameDialog.findViewById(R.id.btnYes);
btnNo = (Button) renameDialog.findViewById(R.id.btnNo);
tvAudioFileName.setText(outputFile + "?");
etAudioFileName.setText(outputFile);

btnYes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
audioRecorder.setOutputFile(etAudioFileName.getText().toString().trim());
renameDialog.dismiss();
}
});

btnNo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
audioRecorder.setOutputFile(outputFile);
renameDialog.dismiss();
}
});
renameDialog.show();

stopWatch.stop();
audioRecorder.stop(); // here is error
audioRecorder.release();
audioRecorder = null;
Toast.makeText(con, "Audio recorded successfully",Toast.LENGTH_LONG).show();
}
});
}

public void clickedOnHomeButton(){
startActivity(new Intent(con,MainActivity.class));
finish();
}
}

when stop button is clicked, one custom dialog will be open for renaming the audio file in application

<小时/>

日志猫

03-05 10:35:29.145    3478-3478/com.becbingham.mybabyhelper E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.becbingham.mybabyhelper, PID: 3478
java.lang.IllegalStateException
at android.media.MediaRecorder.stop(Native Method)
at com.becbingham.mybabyhelper.AddRecordingActivity$4.onClick(AddRecordingActivity.java:174)
at android.view.View.performClick(View.java:4848)
at android.view.View$PerformClick.run(View.java:20260)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5624)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

最佳答案

根据文档:

 MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(PATH_NAME);
recorder.prepare();
recorder.start(); // Recording is now started
...
recorder.stop();
recorder.reset(); // You can reuse the object by going back to setAudioSource() step
recorder.release(); // Now the object cannot be reused

所以,如果您调用recorder.stop();在调用recorder.start();之前if 将显示异常 java.lang.IllegalStateException at android.media.MediaRecorder.stop(Native Method)您需要正确处理停止功能。或者提供一个检查,仅当记录器启动时才会调用。

另请检查您是否添加了权限:<uses-permission android:name="android.permission.RECORD_AUDIO" />

关于android - 录制音频文件时 MediaRecorder.stop() 方法出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35810295/

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