I'm using a MediaRecorder to record audio and save it to a .mp4 with AAC audio. Everything works fine on all the devices I've tried except the Nexus S with Android 4.1. On those devices I either get an error(1, -2147483648) on start() (I think) or it continues fine, but the output file is always empty. I have the necessary permissions, as the app works on other devices.
mRecorder.reset();
mRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {
@Override
public void onError(MediaRecorder mr, int what, int extra) {
Log.e("sagasg", what + " " + extra);
}
});
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mRecorder.setAudioSamplingRate(SAMPLING_RATE);
mRecorder.setAudioEncodingBitRate(BIT_RATE);
mFileName = "unnamed-" + mTimeStarted.year + "-" + mTimeStarted.month + "-" + mTimeStarted.monthDay
+ "-" + mTimeStarted.hour + "-" + mTimeStarted.minute + "-" + mTimeStarted.second;
mRecorder.setOutputFile(mFilePath + mFileName + mExtension);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
I've solved it by making random changes to the settings. It started working when I removed
mRecorder.setAudioSamplingRate(SAMPLING_RATE);
Where sampling rate is
public static final int SAMPLING_RATE = 48000;
According to the documentation AAC supports 8-48 kHz, but for some reason it doesn't. Now I just have to fix yet another bug which only appears on the Nexus S. Now I understand why developers prefer iOS.
Edit: now it doesn't crash because of this options, just the recording is an empty file. Tried other values, like, 24000. Same result. I'll have to stick with the default sampling rate.