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);