Miracast设备上的MediaRouter和Presentation API android(AnyCast)

时间:2018-07-09 13:33:31

标签: android casting buffering airplay miracast

这是我的源代码 我正在尝试在辅助显示器上缓冲本地内容。 首先,我搜索显示设备以发送内容,但是在这里,我将getDisplyDevices()设置为null 那我该如何解决这个问题呢?

public class MainActivity extends Activity {
    MediaRouter router=null;
    Presentation preso=null;
    SimpleCallback cb=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    protected void onStart() {
        super.onStart();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (cb==null) {
                cb=new RouteCallback();
                router=(MediaRouter)getSystemService(MEDIA_ROUTER_SERVICE);
            }

            handleRoute(router.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO));
            router.addCallback(MediaRouter.ROUTE_TYPE_LIVE_VIDEO, cb);
        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    protected void onStop() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            clearPreso();

            if (router != null) {
                router.removeCallback(cb);
            }
        }

        super.onStop();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void handleRoute(RouteInfo route) {
        if (route == null) {
            clearPreso();
        }
        else {
            Display display=route.getPresentationDisplay();

            if (route.isEnabled() && display != null) {
                if (preso == null) {
                    showPreso(route);
                    Log.d(getClass().getSimpleName(), "enabled route");
                }
                else if (preso.getDisplay().getDisplayId() != display.getDisplayId()) {
                    clearPreso();
                    showPreso(route);
                    Log.d(getClass().getSimpleName(), "switched route");
                }
                else {
                    // no-op: should already be set
                }
            }
            else {
                clearPreso();
                Log.d(getClass().getSimpleName(), "disabled route");
            }
        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void clearPreso() {
        if (preso != null) {
            preso.dismiss();
            preso=null;
        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void showPreso(RouteInfo route) {
        preso=new SimplePresentation(this, route.getPresentationDisplay());
        preso.show();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private class RouteCallback extends SimpleCallback {
        @Override
        public void onRoutePresentationDisplayChanged(MediaRouter router,
                                                      RouteInfo route) {
            handleRoute(route);
        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private class SimplePresentation extends Presentation {
        SimplePresentation(Context ctxt, Display display) {
            super(ctxt, display);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            WebView wv=new WebView(getContext());


            wv.getSettings().setJavaScriptEnabled(true);
            wv.loadUrl("https://ctxtsprodeme.xendesktop.net/");

            setContentView(wv);
        }
    }
}

在这里,我可以检测到Miracast设备,但无法从移动设备向显示设备运行视频。 请告诉我任何有解决方案的人。

0 个答案:

没有答案