Android mediaRecorder Stop 失败 - 尝试指定设备的最高记录质量
Posted
技术标签:
【中文标题】Android mediaRecorder Stop 失败 - 尝试指定设备的最高记录质量【英文标题】:Android mediaRecorder Stop failed - Trying to Specify Maximum record quality for device 【发布时间】:2014-05-03 04:18:34 【问题描述】:所以正如标题所说,我收到了 mediaRecorder 停止异常 -1007,但这并不是重点。
要明确我的问题是如何避免出现指定错误,并为正在运行的设备录制最优质的音频。
我需要能够在任何具有 >=API16
MIC 的 android 设备上录制高质量音频这是我正在使用的代码,包括注释掉之前的工作:
public class RecordingHandler
private static MediaRecorder recorder;
static void beginRecording(String output) throws Exception
ditchMediaRecorder();
File outFile = new File(output);
if (outFile.exists())
outFile.delete();
recorder = new MediaRecorder();
recorder.setAudiosource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.setOutputFile(output);
try
recorder.prepare();
recorder.start();
catch (IllegalStateException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
//this was working before
// recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
// recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// recorder.setOutputFile(output);
//
// try
// recorder.prepare();
// catch (IOException e)
// Log.e("Recording Handler", "prepare() failed");
//
// recorder.start();
static void ditchMediaRecorder()
// if we have the recorder, release it
if (recorder != null)
recorder.release();
static void stopRecording()
if (recorder != null)
recorder.stop(); //error happening here
recorder.reset();
recorder.release();
recorder = null;
当前错误:
05-02 21:00:29.443: E/MediaRecorder(28252): stop failed: -1007
05-02 21:00:29.443: E/InputEventReceiver(28252): Exception dispatching input event.
05-02 21:00:29.443: E/MessageQueue-JNI(28252): Exception in MessageQueue callback: handleReceiveCallback
05-02 21:00:29.443: E/MessageQueue-JNI(28252): java.lang.RuntimeException: stop failed.
05-02 21:00:29.443: E/MessageQueue-JNI(28252): at android.media.MediaRecorder.stop(Native Method)
05-02 21:00:29.443: E/MessageQueue-JNI(28252): at com.appulse.beatbox.RecordingHandler.stopRecording(RecordingHandler.java:56)
如果您的答案包含“THREE_GPP”或“AMR_NB”字样,则根本不会被接受,因为其中的音频完全是垃圾。
感谢任何可以提前帮助的人,如果我看起来有点生气,我很抱歉,这真的很烦人。
【问题讨论】:
【参考方案1】:以下是我在我的一个应用中使用的 sn-p,用于录制最高质量的音频。请注意,并非所有设备都支持 44.1 kHz 的采样率。目前,无法检查设备是否支持特定的音频源、采样率或文件类型。您必须自己处理 - 开始录制,检查录制是否失败(如果文件未保存到目录)并使用所有设备(麦克风、3GP、MediaRecorder.AudioEncoder.AMR_NB)支持的设置再次开始录制。我已经更新了我的答案来处理这个问题:
MediaRecorder recorder;
File audioFile;
字符串 storeLoc = Environment.getExternalStorageDirectory() .getAbsolutePath() + "/MyRecordings/";
File path = new File(storeLoc);
if(!path.exists())
path.mkdir();
开始录制:
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.setAudioEncodingBitRate(320000);
recorder.setAudioSamplingRate(44100);
try
audioFile = File.createTempFile("temporaryFileName", ".mp4", path);
catch (IOException e)
// throw new RuntimeException(
// "Couldn't create recording audio file", e);
recorder.setOutputFile(audioFile.getAbsolutePath());
try
recorder.prepare();
recorder.start();
catch (Exception e)
//couldn't start recording probably because settings not supported on device
//so delete recording and start again but this time using basic settings
File unSuccessfulRecFile = new File(audioFile.getAbsolutePath());
unSuccessfulRecFile.delete();
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//exception might also be due to filetype being mp4 so change to 3gp
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try
audioFile = File.createTempFile("temporaryFileName", ".3gp", path);
catch (IOException eI)
recorder.setOutputFile(audioFile.getAbsolutePath());
recorder.prepare();
recorder.start();
停止录制:
recorder.stop();
recorder.release();
File from = new File(audioFile.getAbsolutePath());
File to = new File(storeLoc, "FinalFileName");
from.renameTo(to);
【讨论】:
感谢您的回答,一个快速的问题,您如何检查录音是否无法正常工作,以便切换到默认设置“3gp”等? 我不确定 MediaRecorder 是否会在 stop() 和 release() 被调用后写入输出文件。以上是关于Android mediaRecorder Stop 失败 - 尝试指定设备的最高记录质量的主要内容,如果未能解决你的问题,请参考以下文章
Android 10 源码MediaRecorder 录像流程:MediaRecorder 配置
Android-MediaRecorder-音频录制-警告-W/MediaRecorder(13811): mediarecorder went away with unhandled events(