暂停和使用Android MediaRecorder进行恢复(API级别< 24) [英] Pause & Resume with Android MediaRecorder (API level < 24)

查看:209
本文介绍了暂停和使用Android MediaRecorder进行恢复(API级别< 24)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MediaRecorder时,对于24级以下的API级别,我们没有暂停/恢复. 因此,有一种方法可以做到:

While using MediaRecorder, we don't have pause/resume for API level below 24. So there can be a way to do this is:

  1. 在暂停事件中,停止记录器并创建记录的文件.
  2. 然后在继续播放时再次开始录制并创建另一个文件,并继续这样做直到用户按下停止为止.
  3. 最后合并所有文件.

许多人都在SO上问了这个问题,但仍然找不到解决的办法.人们谈论通过在暂停操作时停止记录并在恢复时重新启动来创建多个媒体文件.所以我的问题是我们如何以编程方式合并/合并所有媒体文件?

Many people asked this question on SO, but couldn't find anyway to solve this. People talk about creating multiple media files by stopping recording on pause action and restarting on resume. So my question is How can we merge/join all media file programmatically?

注意:在我的情况下是MPEG4容器-m4a用于音频,mp4用于视频.

Note: in my case MPEG4 container - m4a for audio and mp4 for video.

我尝试使用SequenceInputStream合并各个生成的记录文件的多个InputStream.但是它总是只产生第一个文件.

I tried using SequenceInputStream to merge multiple InputStream of respective generated recorded files. But it always results the first file only.

代码段:

Enumeration<InputStream> enu = Collections.enumeration(inputStreams);
        SequenceInputStream sqStream = new SequenceInputStream(enu);
        while ((oneByte = sqStream.read(buffer)) != -1) {
            fileOutputStream.write(buffer, 0, oneByte);

        }
        sqStream.close();
        while (enu.hasMoreElements()) {
            InputStream element = enu.nextElement();
            element.close();
        }
        fileOutputStream.flush();
        fileOutputStream.close();

推荐答案

我可以使用 mp4parser 解决此问题>图书馆.非常感谢这个图书馆的作者:)

I could solve this problem using mp4parser library. Thanks much to author of this library :)

在gradle文件中添加以下依赖项:

Add below dependency in your gradle file:

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

解决方案是在用户暂停时停止记录器,然后像在stackoverflow中的许多其他答案中所提到的那样,在恢复时重新开始.将所有生成的音频/视频文件存储在数组中,并使用以下方法合并所有媒体文件.该示例也取自mp4parser库,并根据我的需要进行了一些修改.

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 also 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;
        }
    }

将标志isAudio用于音频文件,将true设置为视频文件.

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

这篇关于暂停和使用Android MediaRecorder进行恢复(API级别&lt; 24)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆