35

我正在使用来自 Android vision API的FaceTracker示例。但是,当在视频上绘制叠加层时,我在录制视频时遇到了困难。

一种方法是将位图存储为图像并使用 FFmpeg 或 Xuggler 处理它们以将它们合并为视频,但我想知道如果我们可以在运行时在投影时录制视频,是否有更好的解决方案。

更新 1: 我用媒体记录器更新了以下课程,但录音仍然无法正常工作。当我调用 triggerRecording() 函数时,它会引发以下错误:

MediaRecorder:以无效状态开始调用:4

我在清单文件中有外部存储权限。

更新 2:

我已经在代码中修复了上述问题,并在 onSurfaceCreated 回调中移动了 setupMediaRecorder()。但是,当我停止录制时,它会引发运行时异常。根据文档,如果没有视频/音频数据,将抛出运行时异常。

那么,我在这里缺少什么?

public class CameraSourcePreview extends ViewGroup {
    private static final String TAG = "CameraSourcePreview";

    private static final SparseIntArray ORIENTATIONS = new SparseIntArray();

    static {
        ORIENTATIONS.append(Surface.ROTATION_0, 90);
        ORIENTATIONS.append(Surface.ROTATION_90, 0);
        ORIENTATIONS.append(Surface.ROTATION_180, 270);
        ORIENTATIONS.append(Surface.ROTATION_270, 180);
    }

    private MediaRecorder mMediaRecorder;
    /**
     * Whether the app is recording video now
     */
    private boolean mIsRecordingVideo;

    private Context mContext;
    private SurfaceView mSurfaceView;
    private boolean mStartRequested;
    private boolean mSurfaceAvailable;
    private CameraSource mCameraSource;

    private GraphicOverlay mOverlay;

    public CameraSourcePreview(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mStartRequested = false;
        mSurfaceAvailable = false;

        mSurfaceView = new SurfaceView(context);

        mSurfaceView.getHolder().addCallback(new SurfaceCallback());

        addView(mSurfaceView);

        mMediaRecorder = new MediaRecorder();
    }

    private void setUpMediaRecorder() throws IOException {
        mMediaRecorder.setPreviewDisplay(mSurfaceView.getHolder().getSurface());
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

        mMediaRecorder.setOutputFile(Environment.getExternalStorageDirectory() + File.separator + Environment.DIRECTORY_DCIM + File.separator + System.currentTimeMillis() + ".mp4");
        mMediaRecorder.setVideoEncodingBitRate(10000000);
        mMediaRecorder.setVideoFrameRate(30);
        mMediaRecorder.setVideoSize(480, 640);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        //int rotation = mContext.getWindowManager().getDefaultDisplay().getRotation();
        //int orientation = ORIENTATIONS.get(rotation);
        mMediaRecorder.setOrientationHint(ORIENTATIONS.get(0));
        mMediaRecorder.prepare();

        mMediaRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {
            @Override
            public void onError(MediaRecorder mr, int what, int extra) {
                Timber.d(mr.toString() + " : what[" + what + "]" + " Extras[" + extra + "]");
            }
        });
    }

    public void start(CameraSource cameraSource) throws IOException {
        if (cameraSource == null) {
            stop();
        }

        mCameraSource = cameraSource;

        if (mCameraSource != null) {
            mStartRequested = true;
            startIfReady();
        }
    }

    public void start(CameraSource cameraSource, GraphicOverlay overlay) throws IOException {
        mOverlay = overlay;
        start(cameraSource);
    }

    public void stop() {
        if (mCameraSource != null) {
            mCameraSource.stop();
        }
    }

    public void release() {
        if (mCameraSource != null) {
            mCameraSource.release();
            mCameraSource = null;
        }
    }

    private void startIfReady() throws IOException {
        if (mStartRequested && mSurfaceAvailable) {
            mCameraSource.start(mSurfaceView.getHolder());
            if (mOverlay != null) {
                Size size = mCameraSource.getPreviewSize();
                int min = Math.min(size.getWidth(), size.getHeight());
                int max = Math.max(size.getWidth(), size.getHeight());
                if (isPortraitMode()) {
                    // Swap width and height sizes when in portrait, since it will be rotated by
                    // 90 degrees
                    mOverlay.setCameraInfo(min, max, mCameraSource.getCameraFacing());
                } else {
                    mOverlay.setCameraInfo(max, min, mCameraSource.getCameraFacing());
                }
                mOverlay.clear();
            }

            mStartRequested = false;
        }
    }

    private class SurfaceCallback implements SurfaceHolder.Callback {
        @Override
        public void surfaceCreated(SurfaceHolder surface) {
            mSurfaceAvailable = true;
            surface.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

            // setup the media recorder
            try {
                setUpMediaRecorder();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                startIfReady();
            } catch (IOException e) {
                Timber.e(TAG, "Could not start camera source.", e);
            }
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder surface) {
            mSurfaceAvailable = false;
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        int width = 320;
        int height = 240;
        if (mCameraSource != null) {
            Size size = mCameraSource.getPreviewSize();
            if (size != null) {
                width = size.getWidth();
                height = size.getHeight();
            }
        }

        // Swap width and height sizes when in portrait, since it will be rotated 90 degrees
        if (isPortraitMode()) {
            int tmp = width;
            width = height;
            height = tmp;
        }

        final int layoutWidth = right - left;
        final int layoutHeight = bottom - top;

        // Computes height and width for potentially doing fit width.
        int childWidth = layoutWidth;
        int childHeight = (int) (((float) layoutWidth / (float) width) * height);

        // If height is too tall using fit width, does fit height instead.
        if (childHeight > layoutHeight) {
            childHeight = layoutHeight;
            childWidth = (int) (((float) layoutHeight / (float) height) * width);
        }

        for (int i = 0; i < getChildCount(); ++i) {
            getChildAt(i).layout(0, 0, childWidth, childHeight);
        }

        try {
            startIfReady();
        } catch (IOException e) {
            Timber.e(TAG, "Could not start camera source.", e);
        }
    }

    private boolean isPortraitMode() {
        int orientation = mContext.getResources().getConfiguration().orientation;
        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            return false;
        }
        if (orientation == Configuration.ORIENTATION_PORTRAIT) {
            return true;
        }

        Timber.d(TAG, "isPortraitMode returning false by default");
        return false;
    }

    private void startRecordingVideo() {
        try {
            // Start recording
            mMediaRecorder.start();
            mIsRecordingVideo = true;
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    private void stopRecordingVideo() {
        // UI
        mIsRecordingVideo = false;
        // Stop recording
        mMediaRecorder.stop();
        mMediaRecorder.reset();
    }

    public void triggerRecording() {
        if (mIsRecordingVideo) {
            stopRecordingVideo();
            Timber.d("Recording stopped");
        } else {
            startRecordingVideo();
            Timber.d("Recording starting");
        }
    }
}
4

1 回答 1

1

解决方案 1:从 Android Lollipop 中引入了一个MediaProjection API,它与 MediaRecorder 一起可用于将 a 保存SurfaceView到视频文件中。此示例显示如何将 a 输出SurfaceView到视频文件。

解决方案 2:或者,您可以使用Grafika 存储库中提供的简洁编码器类之一。请注意,这将要求您移植FaceTracker应用程序,以便它使用 OpenGL 来执行所有渲染。这是因为 Grafika 示例利用 OpenGL 管道来快速读取和写入纹理数据。

CircularEncoder有一个最小的例子可以在 ContinuousCaptureActivity课堂上使用 a 来实现你想要的。这提供了Frame Blitting的示例,同时将帧缓冲区数据显示到屏幕并输出到视频。

主要变化是使用 GrafikaWindowSurface而不是SurfaceViewFaceTracker 应用程序,这将设置 EGL 上下文,允许您通过编码器将帧缓冲区数据保存到文件中。一旦您可以将所有内容渲染到 WindowSurface,就可以轻松地以与类相同的方式设置记录ContinuousCaptureActivity

于 2017-12-30T07:38:42.087 回答