public abstract class

ExpandedControllerActivity

extends AppCompatActivity
implements ControlButtonsContainer
java.lang.Object
   ↳ android.content.Context
     ↳ android.content.ContextWrapper
       ↳ android.view.ContextThemeWrapper
         ↳ android.app.Activity
           ↳ android.support.v4.app.SupportActivity
             ↳ android.support.v4.app.FragmentActivity
               ↳ android.support.v7.app.AppCompatActivity
                 ↳ com.google.android.gms.cast.framework.media.widget.ExpandedControllerActivity

Class Overview

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="http://schemas.android.com/apk/res/android"
       xmlns:app="http://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.

Summary

XML Attributes
Attribute Name Related Method Description
com.google.android.gms:castButtonColor  
com.google.android.gms:castClosedCaptionsButtonDrawable  
com.google.android.gms:castControlButtons  
com.google.android.gms:castForward30ButtonDrawable  
com.google.android.gms:castMuteToggleButtonDrawable  
com.google.android.gms:castPauseButtonDrawable  
com.google.android.gms:castPlayButtonDrawable  
com.google.android.gms:castRewind30ButtonDrawable  
com.google.android.gms:castSeekBarProgressDrawable  
com.google.android.gms:castSeekBarThumbDrawable  
com.google.android.gms:castSkipNextButtonDrawable  
com.google.android.gms:castSkipPreviousButtonDrawable  
com.google.android.gms:castStopButtonDrawable  
[Expand]
Inherited Constants
From class android.app.Activity
From class android.content.Context
From interface android.content.ComponentCallbacks2
[Expand]
Inherited Fields
From class android.app.Activity
Public Constructors
ExpandedControllerActivity()
Public Methods
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 onOptionsItemSelected(MenuItem item)
void onWindowFocusChanged(boolean hasFocus)
Protected Methods
void onCreate(Bundle savedInstanceState)
void onDestroy()
void onPause()
void onResume()
[Expand]
Inherited Methods
From class android.support.v7.app.AppCompatActivity
From class android.support.v4.app.FragmentActivity
From class android.support.v4.app.SupportActivity
From class android.app.Activity
From class android.view.ContextThemeWrapper
From class android.content.ContextWrapper
From class android.content.Context
From class java.lang.Object
From interface android.support.v7.app.AppCompatCallback
From interface android.support.v4.app.TaskStackBuilder.SupportParentable
From interface android.support.v7.app.ActionBarDrawerToggle.DelegateProvider
From interface android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback
From interface android.support.v4.app.ActivityCompatApi23.RequestPermissionsRequestCodeValidator
From interface android.view.LayoutInflater.Factory2
From interface android.view.Window.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.View.OnCreateContextMenuListener
From interface android.content.ComponentCallbacks2
From interface com.google.android.gms.cast.framework.media.widget.ControlButtonsContainer
From interface android.view.LayoutInflater.Factory
From interface android.content.ComponentCallbacks

XML Attributes

com.google.android.gms:castButtonColor

Related Methods

com.google.android.gms:castClosedCaptionsButtonDrawable

Related Methods

com.google.android.gms:castControlButtons

Related Methods

com.google.android.gms:castForward30ButtonDrawable

Related Methods

com.google.android.gms:castMuteToggleButtonDrawable

Related Methods

com.google.android.gms:castPauseButtonDrawable

Related Methods

com.google.android.gms:castPlayButtonDrawable

Related Methods

com.google.android.gms:castRewind30ButtonDrawable

Related Methods

com.google.android.gms:castSeekBarProgressDrawable

Related Methods

com.google.android.gms:castSeekBarThumbDrawable

Related Methods

com.google.android.gms:castSkipNextButtonDrawable

Related Methods

com.google.android.gms:castSkipPreviousButtonDrawable

Related Methods

com.google.android.gms: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 int: the index of the slot in this container.
Returns
ImageView
Throws
IndexOutOfBoundsException

public final int getButtonSlotCount ()

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

Returns
int

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 int: the index of the slot in this container.
Returns
int
Throws
IndexOutOfBoundsException

public SeekBar getSeekBar ()

Returns the SeekBar instance.

Returns
SeekBar

public TextView getStatusTextView ()

Returns the TextView that displays the status text.

Returns
TextView

public UIMediaController getUIMediaController ()

Returns the UIMediaController used to bind views in this container.

Returns
UIMediaController

public boolean onOptionsItemSelected (MenuItem item)

Parameters
item MenuItem
Returns
boolean

public void onWindowFocusChanged (boolean hasFocus)

Parameters
hasFocus boolean

Protected Methods

protected void onCreate (Bundle savedInstanceState)

Parameters
savedInstanceState Bundle

protected void onDestroy ()

protected void onPause ()

protected void onResume ()