lassana / continuous-audiorecorder

Implementation of the missing feature in Android SDK: audio recorder with a pause.
BSD 2-Clause "Simplified" License
147 stars 51 forks source link

Destination file required to be created prior to using AudioRecorder #18

Closed aterbo closed 7 years ago

aterbo commented 8 years ago

When using the standard MediaRecorder class, it is not necessary to create a file on the device prior to recording, as long the destination file path is set, using MediaRecorder.setOutputFile(String outputFilePath).

When using AudioRecorder, the output file needs to be created and the destination file path to this output file needs to be set prior to recording, using AudioRecorderBuilder.fileName(String outputFilePath).

Here is the original code using MediaRecorder, which was functional:

private void recordAudio() {
    String fileName = UUID.randomUUID().toString().replaceAll("-", "");
    outputFile = getFilesDir().getAbsolutePath();
    outputFile += "/" + fileName + ".3gp";
    MediaRecorder myRecorder = MediaRecorder;

    try {
        myRecorder = new MediaRecorder(); 
        myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB); 
        myRecorder.setOutputFile(outputFile);
        myRecorder.setAudioChannels(1);

        myRecorder.prepare();
        myRecorder.start();

    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Here is the functional version of the code using AudioRecorder:

    private void recordAudio(){
        String fileName = UUID.randomUUID().toString().replaceAll("-", "");
        outputFile = this.getFilesDir().getPath();
        outputFile += "/" + fileName + ".mp4";

        File file = new File(outputFile);
        if (!file.exists()) {
            try {
                file.createNewFile();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }

        myRecorder = AudioRecorderBuilder.with(this)
                .fileName(outputFile)
                .config(AudioRecorder.MediaRecorderConfig.DEFAULT)
                .loggable()
                .build();

        myRecorder.start(new AudioRecorder.OnStartListener() {
            @Override
            public void onStarted() {
                // started
                changeButtonsToRecordingOptions();
            }

            @Override
            public void onException(Exception e) {
                // error
                Log.i("AudioRecorder", "ERROR With continuous audio recorder!");
            }
        });
    }

I believe this error is occurring due to some issue with Mp4ParserWrapper not properly creating the file, which I believe should happen in Line 49.

An example of the final value of the String outputFile is: /data/user/0/com.packageName.app/files/fb0409ac7dd743d6867151c034d145b8.mp4

I'm using a Samsung Galaxy 5 and Android 6.0.

Thanks, Andy

yuvaraj119 commented 7 years ago

Great @aterbo thanks you saved my day. First we have to manually create the file then we have to start recording, how we do in normal media recorder.

SkyeHoefling commented 7 years ago

@aterbo thanks for providing the documentation. I had no idea why it wasn't working until I read this. Has anyone looked into submitting a PR to resolve this?

lassana commented 7 years ago

Should be fixed in 1.3.1.

SkyeHoefling commented 7 years ago

@lassana thank you for addressing this issue, I just tested it and it works like a charm