Code example for MediaRouter

Methods: addCallbackgetSelectedRoute

0
    protected void onResume() { 
        // Be sure to call the super class. 
        super.onResume(); 
 
        // Listen for changes to media routes. 
        mMediaRouter.addCallback(MediaRouter.ROUTE_TYPE_LIVE_VIDEO, mMediaRouterCallback);
 
        // Update the presentation based on the currently selected route. 
        mPaused = false;
        updatePresentation(); 
    } 
 
    @Override 
    protected void onPause() { 
        // Be sure to call the super class. 
        super.onPause(); 
 
        // Stop listening for changes to media routes. 
        mMediaRouter.removeCallback(mMediaRouterCallback);
 
        // Pause rendering. 
        mPaused = true;
        updateContents(); 
    } 
 
    @Override 
    protected void onStop() { 
        // Be sure to call the super class. 
        super.onStop(); 
 
        // Dismiss the presentation when the activity is not visible. 
        if (mPresentation != null) {
            Log.i(TAG, "Dismissing presentation because the activity is no longer visible.");
            mPresentation.dismiss();
            mPresentation = null;
        } 
    } 
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) {
        // Be sure to call the super class. 
        super.onCreateOptionsMenu(menu);
 
        // Inflate the menu and configure the media router action provider. 
        getMenuInflater().inflate(R.menu.presentation_with_media_router_menu, menu);
 
        MenuItem mediaRouteMenuItem = menu.findItem(R.id.menu_media_route);
        MediaRouteActionProvider mediaRouteActionProvider =
                (MediaRouteActionProvider)mediaRouteMenuItem.getActionProvider();
        mediaRouteActionProvider.setRouteTypes(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
 
        // Return true to show the menu. 
        return true; 
    } 
 
    private void updatePresentation() { 
        // Get the current route and its presentation display. 
        MediaRouter.RouteInfo route = mMediaRouter.getSelectedRoute(
                MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
        Display presentationDisplay = route != null ? route.getPresentationDisplay() : null;
 
        // Dismiss the current presentation if the display has changed. 
        if (mPresentation != null && mPresentation.getDisplay() != presentationDisplay) {
Stop searching for code, let great code find you!  Add Codota to your java IDE