录制片段或视图,输出视频

Recording a fragment or a view, output video

场景

我在一个片段中有(一个图像视图和一个 videoview)或(一些自定义的东西,比如表面视图和照片里面的左边和右边的视频)我需要输出片段布局作为视频,将照片和视频合并为一个视频。

问题

我真的不知道怎么弄这个,可能是录屏但是不专业。也许你能帮我?一些教程,想法或任何东西都会很好。

谢谢。

虽然this不是fragment,但是对你有帮助。您可以在 5.0 API 之前使用 OpenGLES 纹理制作视频。

或者做录屏服务。像这样,

public boolean startRecord(MediaProjection mediaProjection, File file) {

    mRecorder = new MediaRecorder();
    mRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mRecorder.setAudioSource(MediaRecorder.AudioSource..DEFAULT);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    mRecorder.setOutputFile(file.getPath());
    mRecorder.setMaxDuration(60000);
    mRecorder.setVideoSize(720,1280);

    try {
        mRecorder.prepare();
        mMediaProjection = mediaProjection;
        Surface targetSurface = mRecorder.getSurface();
        mDisplay = mMediaProjection.createVirtualDisplay("ScreenRecorder", 720, 1280, 240,
                DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, targetSurface, null, null);

        mRecorder.start();
        mRecording = true;
        showNotification();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }

    return true;
}

在 activity

中获取 MediaProjection
{
        btnStartRecording.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = mMediaProjectionManager.createScreenCaptureIntent();
                startActivityForResult(intent, REQUEST_CODE_SCREEN_CAST);
            }
        });
}
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_SCREEN_CAST) {
            if (resultCode != RESULT_OK) {
                Toast.makeText(this, "Permission denied, close this app", Toast.LENGTH_SHORT).show();
                finish();
            }
            mMediaProjection = mMediaProjectionManager.getMediaProjection(resultCode, data);
            if (mMediaProjection == null) {
                return;
            }
            File moviesFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
            File movieFile = new File(moviesFolder, "test.mp4");
            if (mService != null) {
                mService.startRecord(mMediaProjection, movieFile);
            }
            else{
                mMediaProjection.stop();
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

您可以记录显示的所有内容。