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

时间:2016-02-17 04:47:14

标签: android android-fragments android-video-player android-videoview android-screen-support

方案

我在片段内部(图像视图和视频视图)或(像Surfaceview一样自定义,在左边和右边的照片内部),我需要输出片段布局作为视频,加入将照片和视频合并为一个视频。

问题

我真的不知道如何做这项工作,也许是为了记录屏幕而不是专业。也许你可以帮帮我?一些教程,想法或任何东西都会很好。

感谢。

1 个答案:

答案 0 :(得分:1)

虽然this不适用于片段,但它会对您有所帮助。您可以通过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;
}

在活动中获取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);
        }
    }

您可以在显示屏上记录所有内容。