2

我正在使用androidMediaProjection截屏。截屏后需要停止投影,virtual display应该释放而VirtualDisplay.release()不是释放显示。这是创建显示的代码。

startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE) {

        sMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);

        // display metrics
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        mDensity = metrics.densityDpi;
        mDisplay = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        mDisplay.getSize(size);
        mWidth = size.x;
        mHeight = size.y;

        // register media projection stop callback
        sMediaProjection.registerCallback(new MediaProjectionStopCallback(), mHandler);

        // register orientation change callback
        mOrientationChangeCallback = new OrientationChangeCallback(this);
        if (mOrientationChangeCallback.canDetectOrientation()) {
            mOrientationChangeCallback.enable();
        }


        // start capture reader
        mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 1);
        mVirtualDisplay = sMediaProjection.createVirtualDisplay(SCREENCAP_NAME, mWidth, mHeight, mDensity, VIRTUAL_DISPLAY_FLAGS, mImageReader.getSurface(), null, mHandler);
        mImageReader.setOnImageAvailableListener(new ImageAvailableListener(), mHandler);
    }
}

为了停止投影,我打电话sMediaProjection.stop();,这是我的MediaProjectionStopCallback实现。

private class MediaProjectionStopCallback extends MediaProjection.Callback {
    @Override
    public void onStop() {
        Log.e("ScreenCapture", "stopping projection.");
        mHandler.post(new Runnable() {
            @Override
                public void run() {
                if (mVirtualDisplay != null) {
                    mVirtualDisplay.release();
                    Log.e("Virtual Display", "Released");
                }
                if (mImageReader != null)
                    mImageReader.setOnImageAvailableListener(null, null);    
                if (mOrientationChangeCallback != null)
                    mOrientationChangeCallback.disable();
                sMediaProjection.unregisterCallback(MediaProjectionStopCallback.this);

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

        //Toast.makeText(getApplicationContext(), "Projection Stopped", Toast.LENGTH_SHORT).show();
    }
}

这是日志猫。

03-02 14:52:55.925    8264-8732/codistan.pk.squeeze_me E/ScreenCapture﹕ stopping projection.
03-02 14:52:55.925    8264-8732/codistan.pk.squeeze_me E/Virtual Display﹕ Released
03-02 14:52:55.925    8264-8732/codistan.pk.squeeze_me E/codistan.pk.squeeze_me.ScreenCaptureActivity-Display﹕ Display Count  1
03-02 14:52:55.935    8264-8732/codistan.pk.squeeze_me E/codistan.pk.squeeze_me.ScreenCaptureActivity-Display﹕ Display name: screencap Display id: 1

我已经仔细检查过,上述onStop方法被正确调用,如在 logcat 中所见。在我检查可用显示器时释放显示器后onStop,虚拟显示器仍列为可用显示器。It affects the phone display and graphics and I can't play any video and the issue remains even after uninstalling the app untill I restart the phone.我已经检查了这个链接Android virtual display release doesn't remove display并搜索了很多但没有发现任何有用的信息。非常感谢您的帮助。

4

1 回答 1

0

尝试这个?

if (mOrientationChangeCallback != null) {
    mOrientationChangeCallback.disable();
}
if (mVirtualDisplay != null) {
    mVirtualDisplay.release();
}
if (mMediaProjection != null) {
    mMediaProjection.stop();
}
if (mImageReader != null) {
    mImageReader.setOnImageAvailableListener(null, null);
    mImageReader.close();
}
于 2016-12-08T03:31:21.287 回答