So as the title says I'm getting a mediaRecorder stop exception -1007, however this isn't the point really.
To be clear my question is how do I avoid any errors like the ones specified, and also record the best quality audio for the device it's running on.
I need to be able to record high quality audio on any android device with a MIC that's >=API16
Here is the code I'm using including what's worked before commented out:
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;
}
}
}
Current error:
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)
If your answer contains the words "THREE_GPP" or "AMR_NB" it simply won't be accepted because the audio from that is complete garbage.
Thanks to anyone who can help in advance I appologize if I seem a bit angry, this is just really annoying.
Below is the snippet I use in one of my apps to record the highest quality audio. Note that not all devices support a sample rate of 44.1 kHz. Currently, there's no way to check if a device supports a particular audio source, sample rate or file type. You have to handle that yourself - start recording, check if the recording failed(if file wasn't saved to dir) and start the recording again with a setting supported by all devices (Mic, 3GP, MediaRecorder.AudioEncoder.AMR_NB). I've updated my answer to handle that:
MediaRecorder recorder;
File audioFile;
String storeLoc = Environment.getExternalStorageDirectory() .getAbsolutePath() + "/MyRecordings/";
File path = new File(storeLoc);
if(!path.exists())
path.mkdir();
Start Recording:
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();
}
Stop Recording:
recorder.stop();
recorder.release();
File from = new File(audioFile.getAbsolutePath());
File to = new File(storeLoc, "FinalFileName");
from.renameTo(to);