Example usage for android.media.projection MediaProjectionManager getMediaProjection

List of usage examples for android.media.projection MediaProjectionManager getMediaProjection

Introduction

In this page you can find the example usage for android.media.projection MediaProjectionManager getMediaProjection.

Prototype

public MediaProjection getMediaProjection(int resultCode, @NonNull Intent resultData) 

Source Link

Document

Retrieve the MediaProjection obtained from a succesful screen capture request.

Usage

From source file:com.commonsware.android.andcorder.RecorderService.java

synchronized private void startRecorder() {
    if (session == null) {
        MediaProjectionManager mgr = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
        MediaProjection projection = mgr.getMediaProjection(resultCode, resultData);

        session = new RecordingSession(this, new RecordingConfig(this), projection);
        session.start();/*w w w. j  av a2 s .  c  o  m*/
    }
}

From source file:com.orpheusdroid.screenrecorder.RecorderService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    //return super.onStartCommand(intent, flags, startId);
    //Find the action to perform from intent
    switch (intent.getAction()) {
    case Const.SCREEN_RECORDING_START:
        /* Wish MediaRecorder had a method isRecording() or similar. But, we are forced to
         * manage the state ourself. Let's hope the request is honored.
          * Request: https://code.google.com/p/android/issues/detail?id=800 */
        if (!isRecording) {
            //Get values from Default SharedPreferences
            getValues();/*  w  w  w  .  ja  v  a 2  s  .  c  om*/
            Intent data = intent.getParcelableExtra(Const.RECORDER_INTENT_DATA);
            int result = intent.getIntExtra(Const.RECORDER_INTENT_RESULT, Activity.RESULT_OK);

            //Initialize MediaRecorder class and initialize it with preferred configuration
            mMediaRecorder = new MediaRecorder();
            initRecorder();

            //Set Callback for MediaProjection
            mMediaProjectionCallback = new MediaProjectionCallback();
            MediaProjectionManager mProjectionManager = (MediaProjectionManager) getSystemService(
                    Context.MEDIA_PROJECTION_SERVICE);

            //Initialize MediaProjection using data received from Intent
            mMediaProjection = mProjectionManager.getMediaProjection(result, data);
            mMediaProjection.registerCallback(mMediaProjectionCallback, null);

            /* Create a new virtual display with the actual default display
             * and pass it on to MediaRecorder to start recording */
            mVirtualDisplay = createVirtualDisplay();
            try {
                mMediaRecorder.start();
                isRecording = true;
                Toast.makeText(this, R.string.screen_recording_started_toast, Toast.LENGTH_SHORT).show();
            } catch (IllegalStateException e) {
                Log.d(Const.TAG,
                        "Mediarecorder reached Illegal state exception. Did you start the recording twice?");
                Toast.makeText(this, R.string.recording_failed_toast, Toast.LENGTH_SHORT).show();
                isRecording = false;
            }

            /* Add Pause action to Notification to pause screen recording if the user's android version
             * is >= Nougat(API 24) since pause() isnt available previous to API24 else build
             * Notification with only default stop() action */
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                //startTime is to calculate elapsed recording time to update notification during pause/resume
                startTime = System.currentTimeMillis();
                Intent recordPauseIntent = new Intent(this, RecorderService.class);
                recordPauseIntent.setAction(Const.SCREEN_RECORDING_PAUSE);
                PendingIntent precordPauseIntent = PendingIntent.getService(this, 0, recordPauseIntent, 0);
                NotificationCompat.Action action = new NotificationCompat.Action(
                        android.R.drawable.ic_media_pause,
                        getString(R.string.screen_recording_notification_action_pause), precordPauseIntent);

                //Start Notification as foreground
                startNotificationForeGround(createNotification(action).build(),
                        Const.SCREEN_RECORDER_NOTIFICATION_ID);
            } else
                startNotificationForeGround(createNotification(null).build(),
                        Const.SCREEN_RECORDER_NOTIFICATION_ID);
        } else {
            Toast.makeText(this, R.string.screenrecording_already_active_toast, Toast.LENGTH_SHORT).show();
        }
        break;
    case Const.SCREEN_RECORDING_PAUSE:
        pauseScreenRecording();
        break;
    case Const.SCREEN_RECORDING_RESUME:
        resumeScreenRecording();
        break;
    case Const.SCREEN_RECORDING_STOP:
        stopScreenSharing();
        //The service is started as foreground service and hence has to be stopped
        stopForeground(true);
        break;
    }
    return START_STICKY;
}