1

Iam developing an android application "User album" in which user pics are scrolling in a viewpager with a background music,here if user wants to make a video with scrolling pics and audio music we should provide a "video screen cast option" to make a video.

I write some code like this to make a video cast.

My code:

    public void onToggleScreenShare(View view) {
    if (((ToggleButton) view).isChecked()) {
        initRecorder();
        shareScreen();
    } else {
        mMediaRecorder.stop();
        mMediaRecorder.reset();
        Log.v(TAG, "Stopping Recording");
        stopScreenSharing();
    }
}
private void shareScreen() {
    if (mMediaProjection == null) {
        startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
        return;
    }
    mVirtualDisplay = createVirtualDisplay();
    mMediaRecorder.start();
}
private VirtualDisplay createVirtualDisplay() {
    return mMediaProjection.createVirtualDisplay("MainActivity",
            DISPLAY_WIDTH, DISPLAY_HEIGHT, mScreenDensity,
            DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
            mMediaRecorder.getSurface(), null /*Callbacks*/, null
            /*Handler*/);
}
private void initRecorder() {
    try {
      /*****here mMediaRecorder is not supported for kitkat version*******/
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mMediaRecorder.setOutputFile(Environment
                .getExternalStoragePublicDirectory(Environment
                        .DIRECTORY_DOWNLOADS) + "/video_"+System.currentTimeMillis()+".mp4");
        mMediaRecorder.setVideoSize(DISPLAY_WIDTH, DISPLAY_HEIGHT);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
        mMediaRecorder.setVideoFrameRate(30);
        int rotation = getWindowManager().getDefaultDisplay().getRotation();
        int orientation = ORIENTATIONS.get(rotation + 90);
        mMediaRecorder.setOrientationHint(orientation);
        mMediaRecorder.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
private class MediaProjectionCallback extends MediaProjection.Callback {
    @Override
    public void onStop() {
        if (mToggleButton.isChecked()) {
            mToggleButton.setChecked(false);
            mMediaRecorder.stop();
            mMediaRecorder.reset();
            Log.v(TAG, "Recording Stopped");
        }
        mMediaProjection = null;
        stopScreenSharing();
    }
}

private void stopScreenSharing() {
    if (mVirtualDisplay == null) {
        return;
    }
    mVirtualDisplay.release();
    //mMediaRecorder.release(); //If used: mMediaRecorder object cannot
    // be reused again
    destroyMediaProjection();
}

By using th eabove code it works well from lollipop but FOR KITKAT VERSION it is saying that media recorder is not supported for kitkat version, i tried for other codes but didn't find the correct solution.

can you suggest me how to implement video cast for KitKat OS version?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
uma
  • 259
  • 3
  • 22
  • Did you search these links? 1) https://stackoverflow.com/questions/31722833/how-can-i-use-media-projection-for-older-api 2) https://stackoverflow.com/questions/20307031/apis-for-android-4-4-screen-recording – Lokesh Aug 28 '17 at 07:02

0 Answers0