MediaControlView

public class MediaControlView
extends ViewGroup

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ androidx.media2.widget.MediaControlView


A View that contains the controls for MediaController or SessionPlayer. It provides a wide range of buttons that serve the following functions: play/pause, rewind/fast-forward, skip to next/previous, select subtitle track, enter/exit full screen mode, select audio track, and adjust playback speed.

For simple use cases not requiring communication with MediaSession, apps need to create a SessionPlayer (e.g. MediaPlayer) and set it to this view by calling setPlayer(SessionPlayer). For more advanced use cases that require MediaSession (e.g. handling media key events, integrating with other MediaSession apps as Assistant), apps need to create a MediaController attached to the MediaSession and set it to this view by calling setMediaController(MediaController).

The easiest way to use a MediaControlView is by creating a VideoView, which will internally create a MediaControlView instance and handle all the commands from buttons inside MediaControlView. It is also possible to create a MediaControlView programmatically and add it to a custom video view. For more information, refer to VideoView. By default, the buttons inside MediaControlView will not visible unless the corresponding SessionCommand is marked as allowed. For more details, refer to MediaSession.

Currently, MediaControlView animates off-screen in two steps: 1) Title and bottom bars slide up and down respectively and the transport controls fade out, leaving only the progress bar at the bottom of the view. 2) Progress bar slides down off-screen.

In addition, the following customizations are supported: 1) Set focus to the play/pause button by calling requestPlayButtonFocus(). 2) Set full screen behavior by calling setOnFullScreenListener(OnFullScreenListener)

Displaying metadata : MediaControlView supports displaying metadata by calling MediaItem.setMetadata(MediaMetadata). Metadata display is different for two different media types: music, and non-music. For music, the following metadata are supported: MediaMetadata.METADATA_KEY_TITLE, MediaMetadata.METADATA_KEY_ARTIST, and MediaMetadata.METADATA_KEY_ALBUM_ART. If values for these keys are not set, the following default values will be shown, respectively: R.string.mcv2_music_title_unknown_text R.string.mcv2_music_artist_unknown_text R.drawable.ic_default_album_image For non-music, only MediaMetadata.METADATA_KEY_TITLE metadata is supported. If the value is not set, the following default value will be shown: R.string.mcv2_non_music_title_unknown_text

Summary

Nested classes

interface MediaControlView.OnFullScreenListener

Interface definition of a callback to be invoked to inform the fullscreen mode is changed. 

Inherited constants

Inherited fields

Public constructors

MediaControlView(Context context)
MediaControlView(Context context, AttributeSet attrs)
MediaControlView(Context context, AttributeSet attrs, int defStyleAttr)

Public methods

CharSequence getAccessibilityClassName()
boolean onTouchEvent(MotionEvent ev)
boolean onTrackballEvent(MotionEvent ev)
void onVisibilityAggregated(boolean isVisible)
void requestPlayButtonFocus()

Requests focus for the play/pause button.

void setMediaController(MediaController controller)

Sets MediaController to control playback with this view.

void setOnFullScreenListener(MediaControlView.OnFullScreenListener listener)

Sets a listener to be called when the fullscreen mode should be changed.

void setPlayer(SessionPlayer player)

Sets SessionPlayer to control playback with this view.

Protected methods

void onAttachedToWindow()
void onDetachedFromWindow()
void onLayout(boolean changed, int left, int top, int right, int bottom)
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Inherited methods

Public constructors

MediaControlView

public MediaControlView (Context context)

Parameters
context Context

MediaControlView

public MediaControlView (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

MediaControlView

public MediaControlView (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Public methods

getAccessibilityClassName

public CharSequence getAccessibilityClassName ()

Returns
CharSequence

onTouchEvent

public boolean onTouchEvent (MotionEvent ev)

Parameters
ev MotionEvent

Returns
boolean

onTrackballEvent

public boolean onTrackballEvent (MotionEvent ev)

Parameters
ev MotionEvent

Returns
boolean

onVisibilityAggregated

public void onVisibilityAggregated (boolean isVisible)

Parameters
isVisible boolean

requestPlayButtonFocus

public void requestPlayButtonFocus ()

Requests focus for the play/pause button.

setMediaController

public void setMediaController (MediaController controller)

Sets MediaController to control playback with this view. Setting a MediaController will unset any MediaController or SessionPlayer that was previously set.

Note that MediaControlView allows controlling playback through its UI components, but calling the corresponding methods (e.g. MediaController.play(), MediaController.pause()) will work as well.

Parameters
controller MediaController: the controller

setOnFullScreenListener

public void setOnFullScreenListener (MediaControlView.OnFullScreenListener listener)

Sets a listener to be called when the fullscreen mode should be changed. A non-null listener needs to be set in order to display the fullscreen button.

Parameters
listener MediaControlView.OnFullScreenListener: The listener to be called. A value of null removes any existing listener and hides the fullscreen button.

setPlayer

public void setPlayer (SessionPlayer player)

Sets SessionPlayer to control playback with this view. Setting a SessionPlayer will unset any MediaController or SessionPlayer that was previously set.

Note that MediaControlView allows controlling playback through its UI components, but calling the corresponding methods (e.g. SessionPlayer.play(), SessionPlayer.pause()) will work as well.

Parameters
player SessionPlayer: the player

Protected methods

onAttachedToWindow

protected void onAttachedToWindow ()

onDetachedFromWindow

protected void onDetachedFromWindow ()

onLayout

protected void onLayout (boolean changed, 
                int left, 
                int top, 
                int right, 
                int bottom)

Parameters
changed boolean

left int

top int

right int

bottom int

onMeasure

protected void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int