我试图在Android中录制音频,但我面临一个问题。试图记录音频,但得到消息“mediarecorder带走未处理的事件”和“致命信号11(SIGSEGV)....”
我有开始和停止按钮,“开始”开始录制和“停止”停止录制。
问题是,当我按下停止按钮,然后我的应用程序记录一条消息“W/MediaRecorder(635):mediarecorder消失了未处理的事件”。 (开始功能正在保存音频文件。)
然后,如果我再次按开始或停止按钮,然后我得到错误消息“A/libc(743):致命的信号11(SIGSEGV)在0x00000010(代码= 1 ),螺纹743(xample.recorder)”记录类
代码如下:
/**
* Creates a new audio recording at the given path (relative to root of SD card).
*/
public AudioRecorder(String path) {
this.path = sanitizePath(path);
}
private String sanitizePath(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
if (!path.contains(".")) {
path += ".3gp";
}
return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
}
public void start() throws IOException {
String state = android.os.Environment.getExternalStorageState();
if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
throw new IOException("SD Card is not mounted. It is " + state + ".");
}
// make sure the directory we plan to store the recording in exists
File directory = new File(path).getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
throw new IOException("Path to file could not be created.");
}
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
try{
recorder.prepare();
}
catch(IOException e){
Log.e("Recorder","Recording failed");
}
recorder.start();
}
/**
* Stops a recording that has been previously started.
*/
public void stop() throws IOException {
recorder.stop();
recorder.release();
}
主要活动的代码如下:
/*
* */
public class Recorder extends Activity implements OnClickListener
{
private static final String TAG="Recorder";
AudioRecorder ar=new AudioRecorder("/TestAudio.3gp");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recorder);
final Button start = (Button) this.findViewById(R.id.btn_start);
start.setOnClickListener(this);
final Button stop = (Button) this.findViewById(R.id.btn_stop);
stop.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_recorder, menu);
return true;
}
public void onClick(View v) {
// TODO Auto-generated method stub
try{
switch (v.getId()) {
case R.id.btn_start:
ar.start();
Log.d("Recorder","Recorded");
Toast.makeText(this, "Controll returned from start function", Toast.LENGTH_LONG).show();
break;
case R.id.btn_stop:
ar.stop();
Toast.makeText(this, "Recording stopped; Starting MediaPlayer", Toast.LENGTH_SHORT).show();
//Toast.makeText(this, "Starting media player", Toast.LENGTH_LONG).show();
ar.startPlaying();
//Toast.makeText(this, "Recording stopped", Toast.LENGTH_LONG).show();
break;
}
}
catch(Exception e){
Log.e("Recorder", e.getMessage(), e);
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
这不是完整的代码?我没有在这里看到构造函数recorder = new MediaRecorder();显示所有堆栈跟踪,你是否收到另一个异常? – dimetil 2012-08-08 20:50:40
我用这个教程http://www.benmccann.com/dev-blog/android-audio-recording-tutorial/在开始按钮我调用函数开始和停止按钮我调用函数停止...按下后开始按钮,我按停止,然后如果我再次按开始按钮然后我得到错误信息“A/libc(743):致命的信号11(SIGSEGV)在0x00000010(代码= 1),线程743(xample.recorder)” – blackfyre 2012-08-09 03:58:24
我的修复https:// stackoverflow。com/a/47992111/4592448 – Fortran 2017-12-27 12:25:05