MediaRecorder记录并制作文件但抛出异常
我的 MediaRecorder 有奇怪的问题。它记录声音,用录音制作文件。但是当我想使用 stop() 方法停止录制时,它会抛出 IllegalStateException。通常我使用 setMaxDuration() 方法,所以通常我使用 OnInfoListener 结束录制并且它工作正常。但我还想在 ImageView 的 OnTouchListener 中停止 MediaRecorder。我的代码在这里:
private static String OUTPUT_FILE;
private void prepareRecording() throws Exception {
OUTPUT_FILE = "/sdcard/temp.3gpp";
File outFile = new File(OUTPUT_FILE);
if (outFile.exists())
outFile.delete();
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(OUTPUT_FILE);
recorder.setMaxDuration(10000);
try {
recorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
recorder.start();
}
private void startRecording() {
ImageView image = (ImageView) this.findViewById(R.id.image);
image.post(new Runnable() {
@Override
public void run() {
try {
prepareRecording();
} catch (Exception e) {
e.printStackTrace();
}
image.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (recorder != null)
recorder.stop();
return true;
}
});
recorder.setOnInfoListener(new OnInfoListener() {
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
if (recorder != null && what == 800){
recorder.stop();
}
}
});
}});
}
当我触摸 ImageView 时,我在代码 recorder.stop() 行出现致命异常 java.lang.IllegalStateException 。我测试了我的代码,发现 MediaRecorder 似乎没有启动。因此,我在 recorder.stop() 处遇到 IllegalStateException。但是当录音停止时,我可以在我的SD卡上找到录音文件。我的代码有什么问题吗?
我还将 AnimationDrawable 连接到我的图像。它工作正常。
I have strange problem with my MediaRecorder. It records voice, make a file with recording. But when I want to stop recording using stop() method, it throws IllegalStateException. Generally I have used setMaxDuration() method, so usually I have ending recording using OnInfoListener and it works properly. But I want also to stop MediaRecorder in the OnTouchListener of the ImageView. My code is here:
private static String OUTPUT_FILE;
private void prepareRecording() throws Exception {
OUTPUT_FILE = "/sdcard/temp.3gpp";
File outFile = new File(OUTPUT_FILE);
if (outFile.exists())
outFile.delete();
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(OUTPUT_FILE);
recorder.setMaxDuration(10000);
try {
recorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
recorder.start();
}
private void startRecording() {
ImageView image = (ImageView) this.findViewById(R.id.image);
image.post(new Runnable() {
@Override
public void run() {
try {
prepareRecording();
} catch (Exception e) {
e.printStackTrace();
}
image.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (recorder != null)
recorder.stop();
return true;
}
});
recorder.setOnInfoListener(new OnInfoListener() {
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
if (recorder != null && what == 800){
recorder.stop();
}
}
});
}});
}
When I have touched the ImageView I have FATAL EXCEPTION java.lang.IllegalStateException at the line with code recorder.stop(). I have tested my code and I have realized that MediaRecorder seems to be not started. Therefore I have IllegalStateException at recorder.stop(). But when recording stops, I can find on my sdcard recording file. Whats wrong with my code?
I have also AnimationDrawable connected to the my image. It works properly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经解决了我的问题。需要在onTouch方法中启动新线程以避免异常。
I have resolved my problem. It is necessary to start new thread in onTouch method to avoid eceptions.