Skip to content

Removes the MediaProjection since we are recording our own app #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.media.MediaRouter;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
Expand Down Expand Up @@ -57,13 +55,6 @@ public class MainActivity extends Activity {
private VirtualDisplay mVirtualDisplay;
private MediaRecorder mMediaRecorder;

private int mResultCode;
private Intent mResultData;

private MediaProjectionManager mProjectionManager;
private MediaProjection mProjection;
private MediaProjection.Callback mProjectionCallback;

private MediaPlayer mMediaPlayer;
private SurfaceView mSurfaceView;

Expand Down Expand Up @@ -92,7 +83,6 @@ protected void onCreate(Bundle savedInstanceState) {

mDisplayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
mDisplayManager.registerDisplayListener(mDisplayListener, null);
mProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
mMediaRecorder = new MediaRecorder();

mButtonCreate = (Button) findViewById(R.id.btn_create_virtual_display);
Expand Down Expand Up @@ -216,80 +206,30 @@ protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
mDisplayManager.unregisterDisplayListener(mDisplayListener);
if (mProjection != null) {
Log.i(TAG, "Stop media projection");
mProjection.unregisterCallback(mProjectionCallback);
mProjection.stop();
mProjection = null;
}
mMediaRecorder.release();
}

private void startScreenCapture() {
if (mProjection != null) {
// start virtual display
Log.i(TAG, "The media projection is already gotten");
createVirtualDisplay();
} else if (mResultCode != 0 && mResultData != null) {
// get media projection
Log.i(TAG, "Get media projection with the existing permission");
mProjection = getProjection();
createVirtualDisplay();
} else {
Log.i(TAG, "Request the permission for media projection");
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), SCREEN_CAPTURE_PERMISSION_CODE);
}
createVirtualDisplay();
}

private void stopScreenCapture() {
destroyVirtualDisplay();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
mResultCode = resultCode;
mResultData = data;
if (requestCode != SCREEN_CAPTURE_PERMISSION_CODE) {
Toast.makeText(this, "Unknown request code: " + requestCode, Toast.LENGTH_SHORT).show();
return;
}
if (resultCode != RESULT_OK) {
Toast.makeText(this, "Screen Cast Permission Denied", Toast.LENGTH_SHORT).show();
return;
}
Log.i(TAG, "Get media projection with the new permission");
mProjection = getProjection();
createVirtualDisplay();
}

private MediaProjection getProjection() {
MediaProjection projection = mProjectionManager.getMediaProjection(mResultCode, mResultData);
// Add a callback to be informed if the projection
// will be stopped from the status bar.
mProjectionCallback = new MediaProjection.Callback() {
@Override
public void onStop() {
Log.d(TAG, "MediaProjection.Callback onStop obj:" + toString());
destroyVirtualDisplay();
mProjection = null;
}
};
projection.registerCallback(mProjectionCallback, null);
return projection;
}

private void createVirtualDisplay() {
if (mProjection != null && mVirtualDisplay == null) {
if (mVirtualDisplay == null) {
Log.d(TAG, "createVirtualDisplay WxH (px): " + mWidth + "x" + mHeight +
", dpi: " + mMetrics.densityDpi);
if (!prepareMediaRecorder(mWidth, mHeight, FRAMERATE, FILENAME)) {
Toast.makeText(this, "Can't prepare MediaRecorder", Toast.LENGTH_LONG).show();
return;
}
int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION;
//flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
mVirtualDisplay = mProjection.createVirtualDisplay("MyVirtualDisplay",
mWidth, mHeight, mMetrics.densityDpi, flags, mMediaRecorder.getSurface(),
flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY;

mVirtualDisplay = mDisplayManager.createVirtualDisplay("MyVirtualDisplay",
mWidth, mHeight, mMetrics.densityDpi, mMediaRecorder.getSurface(), flags,
null /*Callbacks*/, null /*Handler*/);
mButtonCreate.setEnabled(false);
mButtonDestroy.setEnabled(true);
Expand Down