Question

I'm building an audio recorder using MediaRecorder and I want to rename the created file (because createTempFile adds a long random number to the file name)

So my main steps are:

    // 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();
    String fileNumber = String.valueOf(file.length+1);    

        try {
    // I create the temp file to record
      audiofile = File.createTempFile("record-"+fileNumber, ".3gp", myDirectory);

    } catch (IOException e) {
      Log.e(TAG, "sdcard access error");
      return;
    }    
    // 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(audiofile.getAbsolutePath());
    recorder.prepare();
    recorder.start();

When the user clicks the stop button I add the file in the directoy

    values.put(MediaStore.Audio.Media.TITLE, audiofile.getName());
    values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
    values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
    values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
    ContentResolver contentResolver = getContentResolver();

    Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Uri newUri = contentResolver.insert(base, values);


    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
    Toast.makeText(this, "Successfully added file in " +                     recordedDirectory+audiofile.getName(), Toast.LENGTH_LONG).show();

The problem is that the files gets named e.g. "record-3824925563.3gp"

Is there anyway to rename the TempFile before saving it or preventing createTempFile() from adding the random number? Or any other solution to achieve this?

I tried creating a new file and then casting .renameTo(newFile) to my audio file but that didn't work.

EDIT: As suggested by Michael I tried creating a New File with the parameters I want and renaming it

File AudioFile = new File(myDirectory+"/record-"+fileNumber+".3gp");

AudioFile.createNewFile();

audiofile.renameTo(AudioFile);

Which throws no errors. But the files endup with the same random numbers appended

EDIT 2:

As per jboi answer, which works great, I'm adding the final code I used. I created the file to make reference to it on my final MediaStore functions.

        File f = new File(myDirectory.toString());        
    File file[] = f.listFiles();
    int fileNumber = file.length+1;

    audiofile = new File(String.format(Locale.US, "%s%crecord-%08d.3pg",
            quickrecorderDirectory, File.separatorChar, fileNumber));

    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(audiofile.getAbsolutePath());
Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top