暂停&amp;使用 Android MediaRecorder 恢复(API 级别 <24) [英] Pause &amp; Resume with Android MediaRecorder (API level &lt; 24)

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

问题描述

在使用 MediaRecorder 时,我们没有 API 级别低于 24 的暂停/恢复.所以有一种方法可以做到这一点:

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 为音频文件为真,为视频文件为假.

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

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

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