You could simply skip the step of creating a temporary file. If anything happens between start of recording and stop (user canceled, device turned off, app crashed, ...) then you can sort out file fragments and corrupted files in a housekeeping task later.
So, I would start recording straight forward into the final file. The file will have the filenumber+1 as a 8 digit number with leading zeros:
// I get the files on the directory I'm writing and the amount of files + 1
File f = new File(myDirectory.toString());
File file[] = f.listFiles();
int fileNumber = file.length+1;
// START THE RECORDER
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(
String.format(Locale.US, "%s%crecord-%08d.3pg",
myDirectory, File.separatorChar, fileNumber));
try {
recorder.prepare();
} catch(IOException e) {
Log.e(TAG, "error while preparing recording", e);
// You might want to inform the user too, with a Toast
return;
}
recorder.start();
Stopping and informing the media player is also straight forward:
recorder.stop();
recorder.release();
// Here comes all your code to inform Media scanner
In the end you need some housekeeping, that checks for incomplete files in the directory and deletes them. It could run on every startup onCreate()
. To find out if a file is complete, you might have a database already in use. In this database you can keep track of all known file names (insert
when recording was stopped) and compare files with the database.