延迟在 android 上准备媒体记录器 [英] Delay in preparing media recorder on android

查看:50
本文介绍了延迟在 android 上准备媒体记录器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 10 秒内录制了多个小视频.在创建任何视频之前,我需要一次又一次地准备和启动媒体记录器.因此,每个视频都有一两秒的时间延迟.有没有什么办法可以避免在准备像 Instagram 这样的媒体记录器时出现这种时间延迟?

I am recording multiple small videos within 10 sec. I need to prepare and start media recorder again and again before creating any video. Due to which, there is a time lag of one or two seconds in every video. Is there any way to avoid this time delay in preparing the media recorder like Instagram?

public boolean startRecording() {

    startCamera();
    camera.unlock();

    int rotation = 0;
    cameraInfo = new CameraInfo();
    Camera.getCameraInfo(cameraId, cameraInfo);
    if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        rotation = 270;
    } else {
        rotation = 90;
    }

    mMediaRecorder = new MediaRecorder();
    mMediaRecorder.setOrientationHint(rotation);
    mMediaRecorder.setCamera(camera);
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    mMediaRecorder.setVideoFrameRate(30); 
    mMediaRecorder.setVideoEncodingBitRate(1500000);
    mMediaRecorder.setVideoSize(640, 480);
    File f = new File(cVideoFilePath);
            int countNames = 1;
    String[] fileNameList = null;
    fileNameList = f.list();
    for (int i = 0; i < fileNameList.length; i++) {

        if (fileNameList[i].contains("TapVid")) {
            countNames++;
        }

    }

    updatedFileNumber = countNames;
    prRecordedFile = new File(cVideoFilePath + "Vid" + updatedFileNumber
            + ".mp4");
    mMediaRecorder.setOutputFile(prRecordedFile.getPath());

    mMediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());

    try {
        mMediaRecorder.prepare();

        mMediaRecorder.start();
    } catch (Exception e) {
        e.printStackTrace();
        releaseMediaRecorder();
        return false;
    }


    return true;
}

推荐答案

因此 Instagram 也受到 MediaRecorder 延迟的影响.您可以判断何时进行快速点击.进度条重置.只有当您按住录制按钮超过 1 秒时,它才会真正捕捉到任何内容.

So Instagram also suffers from the MediaRecorder delay. You can tell when you do quick taps. The progress bar resets. It's only once you've held the record button for longer than a second does it actually capture anything.

这只是 MediaRecorder 的固有缺陷.解决此问题的唯一方法是使用 setPreviewCallbackWithBuffer API.在使用此 API 时,您可以为相机提供缓冲区以显示预览,并且它会用它在预览表面上显示的 YUV 帧回调给您.然后,您可以将此缓冲区传递给 MediaCodec 以对视频进行编码.

This is just an inherent flaw in MediaRecorder. The only way around this is to use the setPreviewCallbackWithBuffer API. In using this API you provide the camera with buffers to display the preview, and it calls back to you with the YUV frame that it showed on the preview surface. You can then pass this buffer through to MediaCodec to encode the video.

理想情况下,OEM 已实施 MediaCodec 以了解从相机传递的缓冲区,但我们在 4.3 之前的许多设备上看到性能参差不齐.例如,期望与相机提供的对齐和 YUV 格式不同的随机 MediaCodec 实现.您还必须自己捕捉音频并保持 AV 同步.

Ideally, OEMs have implemented MediaCodec to understand the buffers being passed from the camera, but we've seen spotty performance on a lot of devices pre 4.3. Random MediaCodec implementations that expect different alignments and YUV formats than the camera provides for example. You'll also have to capture audio yourself and maintain AV sync.

或者,您可以将缓冲区传递给 ffmpeg 进行编码 - 请注意您在 ffmpeg 中构建的编解码器.VP8 + vorbis 是我们见过的性能最好的免版税编解码器.

Alternatively you can pass the buffers to ffmpeg to encode - just be careful which codecs you build into ffmpeg. VP8 + vorbis is the best performing royalty free codecs we've seen.

这篇关于延迟在 android 上准备媒体记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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