3

I am using android Virtual Display along with Projection Manager At certain time, the projection need to be stopped, hence the following actions are taken

@Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mMediaProjection.stop();
        Log.e(TAG, "mMediaProjection.stopped ");
        mVirtualDisplay.release();
        Log.e(TAG, "virtual display.released ");
    }

The surfaceDestroyed is related to the surface which backs the Virtual Display.

When it is the time to start Media Projection again, if available displays are iterated, the previously created virtual display still listed as one of available displays

DisplayManager disp = (DisplayManager) getSystemService(DISPLAY_SERVICE);
        Display[] allDisplays = disp.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
        Log.e(TAG+"-Display", "Display Count  "+allDisplays.length);
        for(Display dl : allDisplays){
            Log.e(TAG+"-Display", "Display name " + dl.getName()+" Display id "+dl.getDisplayId());
        }

How to properly handle Virtual display creation and removal ?

Note :

Following is the snippet of media projection and virtual display setup

mMediaProjection = mMediaProjectionManager.getMediaProjection(mResultCode, mResultData);
mVirtualDisplay = mMediaProjection.createVirtualDisplay("ScreenCapture",
                mSurfaceView.getWidth(), mSurfaceView.getHeight(), mScreenDensity,
                 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC | DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR |DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION,
                mSurface, null, null);
nmxprime
  • 1,506
  • 3
  • 25
  • 52
  • FWIW, you can get a list of the active virtual displays from `adb shell dumpsys SurfaceFlinger` (along with lots of other goop). Bear in mind that `surfaceDestroyed()` is not always called when the app is paused; see https://source.android.com/devices/graphics/architecture.html#activity – fadden Mar 02 '16 at 06:49
  • 1
    Now, I call `mMediaProjection.stop(); Log.e(TAG, "mMediaProjection.stopped "); mVirtualDisplay.release(); Log.e(TAG, "virtual display.released ");` manually when projection has to be terminated. But still i see all displays not released if i run `dumpsys display` – nmxprime May 24 '16 at 12:44
  • Thanks for the hint `Bear in mind that surfaceDestroyed() is not always called when the app is paused` – nmxprime May 24 '16 at 12:51

0 Answers0