Code example for MediaRouter

Methods: addCallback, getSelectedRoute

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