How can i pause voice recording in Android?

14,830

Solution 1

http://developer.android.com/reference/android/media/MediaRecorder.html

MediaRecorder does not have pause and resume methods. You need to use stop and start methods instead.

Solution 2

I had such a requirement in one of my projects, What we done was like make a raw file for saving recorded data in start of recording using AudioRecord , the for each resume we append the data to the same file

like

FileOutputStream fos= new FileOutputStream(filename, true); 

here the filename is the name of the raw file and append the new recording data to it.

And when user stop the recording we will convert the entire raw file to .wav( or other) formats. Sorry that i cant post the entire code. Hope this will give you a direction to work.

Solution 3

You can refer my answer here if still have this issue. For API level >= 24 pause/resume methods are available in Android MediaRecorder class.

For API level < 24

Add below dependency in your gradle file:

compile 'com.googlecode.mp4parser:isoparser:1.0.2'

The solution is to stop recorder when user pause and start again on resume as already mentioned in many other answers in stackoverflow. Store all the audio/video files generated in an array and use below method to merge all media files. The example is taken from mp4parser library and modified little bit as per my need.

public static boolean mergeMediaFiles(boolean isAudio, String sourceFiles[], String targetFile) {
        try {
            String mediaKey = isAudio ? "soun" : "vide";
            List<Movie> listMovies = new ArrayList<>();
            for (String filename : sourceFiles) {
                listMovies.add(MovieCreator.build(filename));
            }
            List<Track> listTracks = new LinkedList<>();
            for (Movie movie : listMovies) {
                for (Track track : movie.getTracks()) {
                    if (track.getHandler().equals(mediaKey)) {
                        listTracks.add(track);
                    }
                }
            }
            Movie outputMovie = new Movie();
            if (!listTracks.isEmpty()) {
                outputMovie.addTrack(new AppendTrack(listTracks.toArray(new Track[listTracks.size()])));
            }
            Container container = new DefaultMp4Builder().build(outputMovie);
            FileChannel fileChannel = new RandomAccessFile(String.format(targetFile), "rw").getChannel();
            container.writeContainer(fileChannel);
            fileChannel.close();
            return true;
        }
        catch (IOException e) {
            Log.e(LOG_TAG, "Error merging media files. exception: "+e.getMessage());
            return false;
        }
    }

Use flag isAudio as true for Audio files and false for Video files.

Solution 4

You can't do it using Android API, but you can save a lot of mp4 files and merge it using mp4parser: powerful library written in Java. Also see my simple recorder with a "pause": https://github.com/lassana/continuous-audiorecorder.

Share:
14,830
patel
Author by

patel

Updated on July 31, 2022

Comments

  • patel
    patel almost 2 years

    My aim is to pause in recording file. I see in Android developer site its but Media Recorder have not pause option.

    Java supports merge two audio file programatically but In android its not work.

    Join two WAV files from Java?

    And also I used default device audio recorder Apps which is available in all device but in Samsung few devices have not returened recording path.

    Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
    startActivityForResult(intent,REQUESTCODE_RECORDING);
    

    Any one help for voice recording with pause functionality.

  • patel
    patel about 11 years
    i wants to have voice recording in pause option. Java supports so, how can i implement in android.
  • Raghunandan
    Raghunandan about 11 years
    You cannot pause and resume you need to stop and start. so on stop save the audio file and on start start recording again. merge the two. For merging stackoverflow.com/questions/6161231/merging-pcm-audio-files
  • Arun C
    Arun C about 11 years
    Ya appending the data into one raw file and convert it at last
  • patel
    patel about 11 years
    I try for merge two mp3 file but not success so, can u send me sample example link for merge two mp3 file.
  • Raghunandan
    Raghunandan about 11 years
  • Srijith
    Srijith almost 10 years
    Hi, i tried using your MP4ParserWrapper class, but when i record for more than 10 minutes or something, my app gets crashed and the file didn't save. How can i improve its performance.
  • oorosco
    oorosco over 7 years
    Thank you. you are a life saver.
  • Fortran
    Fortran over 6 years
    @Raghunandan and @ Arun C, What variant has less device resources? "FileOutputStream" or 'com.googlecode.mp4parser:isoparser:1.0.2' ?
  • Raghunandan
    Raghunandan over 6 years
    @Fortran i have no idea. I haven't uses mp4parser