ExpandedControllerActivity

public abstract class ExpandedControllerActivity extends AppCompatActivity
implements ControlButtonsContainer

This abstract class provides most of the implementation of an expanded controller, which is an out-of-the-box remote player, used when casting media to a cast device. The expanded controller contains a dark Toolbar, a background album art image, a seek bar control, a play/pause toggle button, and up to 4 configurable control buttons. To fully implement an expanded controller, developers should do the following:

  • Subclass this class to add a cast button in its toolbar.
  • Set attribute castExpandedControllerToolbarStyle to ThemeOverlay.AppCompat.Dark.ActionBar in your theme to use light text and icon color in the Toolbar.

Define cast button menu xml

Developers need to define a menu xml that contains a cast button for the expanded controller Activity:
 // cast_expanded_controller_menu.xml
 <menu xmlns:android="//schemas.android.com/apk/res/android"
       xmlns:app="//schemas.android.com/apk/res-auto" >
     <item
     android:id="@+id/media_route_menu_item"
     app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
     app:showAsAction="always" />
 </menu>
 

Set up the cast button

Developers need to subclass ExpandedControllerActivity to set up the Cast button, and register the Activity in the application's manifest file:
 // MyExpandedControllerActivity.java
 class MyExpandedControllerActivity extends ExpandedControllerActivity {
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         super.onCreateOptionsMenu(menu);
         getMenuInflater().inflate(R.menu.cast_expanded_controller_menu, menu);
         CastButtonFactory.setUpMediaRouteButton(this, menu, R.id.media_route_menu_item);
         return true;
     }
 }
 

Set Toolbar theme

Since this Activity defaults to a dark theme Toolbar, developers need to set an overlay theme for the toolbar to use light text and a light icon color:
 // my_styles.xml
 <style name="Theme.CastVideosTheme" parent="Theme.AppCompat.Light.NoActionBar">
     <item name="castExpandedControllerToolbarStyle">
         @style/ThemeOverlay.AppCompat.Dark.ActionBar
     </item>
 </style>
 

Configurable control buttons

The Activity has five slots to show control buttons. The middle slot always shows a play/pause toggle button and is non-configurable. The other four slots are configurable by the sender app. By default the Activity shows a closed caption button, a skip to the previous item button, a skip to the next item button, and a mute toggle button in these slots, from start to end. Developers can use the attribute castControlButtons to override which buttons to show in which slots. The list of supported control buttons are defined as ID resources:

@id/cast_button_type_empty: Not placing a button in this slot.
@id/cast_button_type_custom: A custom button.
@id/cast_button_type_play_pause_toggle: A button that toggles playback.
@id/cast_button_type_skip_previous: A button that skips to the previous item in the queue.
@id/cast_button_type_skip_next: A button that skips to the next item in the queue.
@id/cast_button_type_rewind_30_seconds: A button that rewinds the playback by 30 seconds.
@id/cast_button_type_forward_30_seconds: A button that skips forward the playback by 30 seconds.
@id/cast_button_type_mute_toggle: A button that mutes and unmutes the remote receiver.
@id/cast_button_type_closed_caption: A button that opens a dialog to select text and audio tracks.

Here is an example of showing a rewind button in the second slot, a forward button in the third slot, and leaving the first and last slot empty:

 // arrays.xml
 <array name="cast_expanded_controller_control_buttons">
     <item>@id/cast_button_type_empty</item>
     <item>@id/cast_button_type_rewind_30_seconds</item>
     <item>@id/cast_button_type_forward_30_seconds</item>
     <item>@id/cast_button_type_empty</item>
 </array>
 ...
 // styles.xml
 <style name="Theme.MyTheme">
     <item name="castExpandedControllerStyle">
         @style/CustomCastExpandedController
     </item>
 </style>
 ...
 <style name="CustomCastExpandedController" parent="CastExpandedController">
     <item name="castControlButtons">
         @array/cast_expanded_controller_control_buttons
     </item>
 </style>
 
The array must contain exactly four items, otherwise a runtime exception will be thrown. If you don't want to show a button in a slot, use @id/cast_button_type_empty.

Add custom control buttons

This activity supports adding custom control buttons which are not provided by the SDK, such as a "thumb up" button.

1. Specify a slot to contain a custom button using @id/cast_button_type_custom in the castControlButtons attribute. You can then use getButtonImageViewAt(int) to obtain the ImageView for that custom button.

2. Implement a subclass of UIController. The UIController contains methods that are called by the SDK when the state of the cast session or media session changes. Your subclass of UIController should take a ImageView as one of the parameters, and update its state as needed.

3. Override onCreate(Bundle), call getButtonImageViewAt(int) to get the view object of the button, and then call bindViewToUIController(View, UIController) to associate the view with your custom UIController.

4. See MediaIntentReceiver for how to handle the action from your custom button.

Here is an example of associating a button at slot 2 to a UIController called MyCustomUIController:

 // arrays.xml
 <array name="cast_expanded_controller_control_buttons">
     <item>@id/cast_button_type_empty</item>
     <item>@id/cast_button_type_rewind_30_seconds</item>
     <item>@id/cast_button_type_custom</item>
     <item>@id/cast_button_type_empty</item>
 </array>

 // MyCustomUIController.java
 class MyCustomUIController extends UIController {
     private final View mView;

     public MyCustomUIController(View view) {
         mView = view;
     }

     @Override
     public onMediaStatusUpdated() {
         // Update the state of mView based on the latest the media status.
         ...
         mView.setVisible(false);
         ...
     }
 }

 // MyExpandedControllerActivity.java
 class MyExpandedControllerActivity extends ExpandedControllerActivity {
     @Override
     public void onCreate() {
         super.onCreate();
         ImageView customButtonView = getButtonImageViewAt(2);
         MyCustomUIController myCustomUiController = new MyCustomUIController(customButtonView);
         getUIMediaController().bindViewToUIController(customButtonView, myCustomUiController);
         ...
     }
 }
 

CastContext can manage the lifecycle and presentation of this activity.

XML Attribute Summary

Inherited Constant Summary

Inherited Field Summary

Public Constructor Summary

Public Method Summary

final ImageView
getButtonImageViewAt(int slotIndex)
Returns the ImageView of the button at slotIndex in this container.
final int
getButtonSlotCount()
Returns the number of slots to hold control buttons in this container.
final int
getButtonTypeAt(int slotIndex)
Returns the type of the button at slotIndex in this container.
SeekBar
getSeekBar()
Returns the SeekBar instance.
TextView
getStatusTextView()
Returns the TextView that displays the status text.
UIMediaController
getUIMediaController()
Returns the UIMediaController used to bind views in this container.
boolean
void
onWindowFocusChanged(boolean hasFocus)

Protected Method Summary

void
onCreate(Bundle savedInstanceState)
void
void
void

Inherited Method Summary

XML Attributes

CastExpandedController_castButtonColor

Related Methods

    CastExpandedController_castClosedCaptionsButtonDrawable

    Related Methods

      CastExpandedController_castControlButtons

      Related Methods

        CastExpandedController_castForward30ButtonDrawable

        Related Methods

          CastExpandedController_castMuteToggleButtonDrawable

          Related Methods

            CastExpandedController_castPauseButtonDrawable

            Related Methods

              CastExpandedController_castPlayButtonDrawable

              Related Methods

                CastExpandedController_castRewind30ButtonDrawable

                Related Methods

                  CastExpandedController_castSeekBarProgressDrawable

                  Related Methods

                    CastExpandedController_castSeekBarThumbDrawable

                    Related Methods

                      CastExpandedController_castSkipNextButtonDrawable

                      Related Methods

                        CastExpandedController_castSkipPreviousButtonDrawable

                        Related Methods

                          CastExpandedController_castStopButtonDrawable

                          Related Methods

                            Public Constructors

                            public ExpandedControllerActivity ()

                            Public Methods

                            public final ImageView getButtonImageViewAt (int slotIndex)

                            Returns the ImageView of the button at slotIndex in this container. The ImageView is defined in the layout of the Activity which implements this interface.

                            Parameters
                            slotIndex the index of the slot in this container.

                            public final int getButtonSlotCount ()

                            Returns the number of slots to hold control buttons in this container.

                            public final int getButtonTypeAt (int slotIndex)

                            Returns the type of the button at slotIndex in this container.

                            Button types are defined as one of the ID resources:

                            • @id/cast_button_type_empty: Not placing a button in this slot.
                            • @id/cast_button_type_custom: A custom button.
                            • @id/cast_button_type_play_pause_toggle: A button that toggles playback.
                            • @id/cast_button_type_skip_previous: A button that skips to the previous item in the queue.
                            • @id/cast_button_type_skip_next: A button that skips to the next item in the queue.
                            • @id/cast_button_type_rewind_30_seconds: A button that rewinds the playback by 30 seconds.
                            • @id/cast_button_type_forward_30_seconds: A button that skips forward the playback by 30 seconds.
                            • @id/cast_button_type_mute_toggle: A button that mutes and unmutes the remote receiver.
                            • @id/cast_button_type_closed_caption: A button that opens a dialog to select text and audio tracks.
                            Parameters
                            slotIndex the index of the slot in this container.

                            public SeekBar getSeekBar ()

                            Returns the SeekBar instance.

                            public TextView getStatusTextView ()

                            Returns the TextView that displays the status text.

                            public UIMediaController getUIMediaController ()

                            Returns the UIMediaController used to bind views in this container.

                            public boolean onOptionsItemSelected (MenuItem item)

                            public void onWindowFocusChanged (boolean hasFocus)

                            Protected Methods

                            protected void onCreate (Bundle savedInstanceState)

                            protected void onDestroy ()

                            protected void onPause ()

                            protected void onResume ()

                            Attribute Name
                            CastExpandedController_castButtonColor
                            CastExpandedController_castClosedCaptionsButtonDrawable
                            CastExpandedController_castControlButtons
                            CastExpandedController_castForward30ButtonDrawable
                            CastExpandedController_castMuteToggleButtonDrawable
                            CastExpandedController_castPauseButtonDrawable
                            CastExpandedController_castPlayButtonDrawable
                            CastExpandedController_castRewind30ButtonDrawable
                            CastExpandedController_castSeekBarProgressDrawable
                            CastExpandedController_castSeekBarThumbDrawable
                            CastExpandedController_castSkipNextButtonDrawable
                            CastExpandedController_castSkipPreviousButtonDrawable
                            CastExpandedController_castStopButtonDrawable