1

I ran this example: https://github.com/android/media-samples/tree/main/ScreenCapture

And changed the display to be public so that I can access it using scrcpy:

mVirtualDisplay = mMediaProjection.createVirtualDisplay("ScreenCapture",
                mSurfaceView.getWidth(), mSurfaceView.getHeight(), mScreenDensity,
                DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC,
                mSurface, null, null);

When I open the display using scrcpy, I get a completely black screen, while on the preview it's showing correctly.

enter image description here

enter image description here enter image description here

Why is it giving a black screen through scrcpy?

I'm assuming it's because the display is not secure (the warning "WARN: Display doesn't have FLAG_SUPPORTS_PROTECTED_BUFFERS flag, mirroring can be restricted"), but there is no way for a regular app to create a secure virtual display, since the permission is for system apps only.

Any way to get it working?

Kidades
  • 600
  • 2
  • 9
  • 26

1 Answers1

0

scrcpy shows a black screen because in this display (your defined display) three is no presentation view. pls used this code to display something on scrcpy.

import android.app.ActivityOptions;
import android.app.Presentation;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;

public class PresentationDisplay extends Presentation {

    public PresentationDisplay(Context outerContext, Display display) {
        super(outerContext, display);
    }

    public PresentationDisplay(Context outerContext, Display display, int theme) {
        super(outerContext, display, theme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about_ui);
        ActivityOptions options = ActivityOptions.makeTaskLaunchBehind();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            options.setLaunchDisplayId(getDisplay().getDisplayId());
        }

        Log.e("TAG", "DISPLAY_CATEGORY_PRESENTATION  " + getDisplay().getDisplayId());

    }
}

And Use It :

  MediaRouter.RouteInfo route = ((MediaRouter) context.getSystemService(MEDIA_ROUTER_SERVICE)).getSelectedRoute(ROUTE_TYPE_LIVE_VIDEO);
        if (route != null) {
            PresentationDisplay secondaryDisplay = new PresentationDisplay(context,route.getPresentationDisplay());
            secondaryDisplay.show();
        }

Morteza
  • 141
  • 1
  • 6