How can i pause voice recording in Android?

前端 未结 4 825
孤独总比滥情好
孤独总比滥情好 2021-02-09 00:41

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 bu

4条回答
  •  遇见更好的自我
    2021-02-09 01:00

    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 listMovies = new ArrayList<>();
                for (String filename : sourceFiles) {
                    listMovies.add(MovieCreator.build(filename));
                }
                List 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.

提交回复
热议问题