AndroidGorilllla
AndroidGorilllla

Reputation: 179

Recording a fragment or a view, output video

Scenario

I have (an imageview and a videoview) or (something custom like a surfaceview and inside a photo to the left and to the right a video) inside a fragment and I need to output the fragment layout as a video, to join the photo and the video into a single video.

Question

I really don't know how to make this work, maybe to record the screen but is not proffesional. Maybe you can help me ? Some tutorials, ideeas or anything will be good.

Thanks.

Upvotes: 2

Views: 2010

Answers (1)

Joe Mizuno
Joe Mizuno

Reputation: 447

Although this is not for fragment, it helps you. You can make video with OpenGLES texture by 5.0 API.

or make screen recorder service. like this,

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;
}

to get MediaProjection in activity

{
        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);
        }
    }

You can record everything on display.

Upvotes: 1

Related Questions