to top
Android APIs
public class

MediaController

extends FrameLayout
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ android.widget.MediaController

Class Overview

A view containing controls for a MediaPlayer. Typically contains the buttons like "Play/Pause", "Rewind", "Fast Forward" and a progress slider. It takes care of synchronizing the controls with the state of the MediaPlayer.

The way to use this class is to instantiate it programatically. The MediaController will create a default set of controls and put them in a window floating above your application. Specifically, the controls will float above the view specified with setAnchorView(). The window will disappear if left idle for three seconds and reappear when the user touches the anchor view.

Functions like show() and hide() have no effect when MediaController is created in an xml layout. MediaController will hide and show the buttons according to these rules:

  • The "previous" and "next" buttons are hidden until setPrevNextListeners() has been called
  • The "previous" and "next" buttons are visible but disabled if setPrevNextListeners() was called with null listeners
  • The "rewind" and "fastforward" buttons are shown unless requested otherwise by using the MediaController(Context, boolean) constructor with the boolean set to false

Summary

Nested Classes
interface MediaController.MediaPlayerControl  
[Expand]
Inherited XML Attributes
From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Constants
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Fields
From class android.view.View
Public Constructors
MediaController(Context context, AttributeSet attrs)
MediaController(Context context, boolean useFastForward)
MediaController(Context context)
Public Methods
boolean dispatchKeyEvent(KeyEvent event)
Dispatch a key event to the next view on the focus path.
void hide()
Remove the controller from the screen.
boolean isShowing()
void onFinishInflate()
Finalize inflating a view from XML.
void onInitializeAccessibilityEvent(AccessibilityEvent event)
Initializes an AccessibilityEvent with information about this View which is the event source.
void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
Initializes an AccessibilityNodeInfo with information about this view.
boolean onTouchEvent(MotionEvent event)
Implement this method to handle touch screen motion events.
boolean onTrackballEvent(MotionEvent ev)
Implement this method to handle trackball motion events.
void setAnchorView(View view)
Set the view that acts as the anchor for the control view.
void setEnabled(boolean enabled)
Set the enabled state of this view.
void setMediaPlayer(MediaController.MediaPlayerControl player)
void setPrevNextListeners(View.OnClickListener next, View.OnClickListener prev)
void show(int timeout)
Show the controller on screen.
void show()
Show the controller on screen.
[Expand]
Inherited Methods
From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.ViewManager
From interface android.view.ViewParent
From interface android.view.accessibility.AccessibilityEventSource

Public Constructors

public MediaController (Context context, AttributeSet attrs)

Added in API level 1

public MediaController (Context context, boolean useFastForward)

Added in API level 1

public MediaController (Context context)

Added in API level 1

Public Methods

public boolean dispatchKeyEvent (KeyEvent event)

Added in API level 1

Dispatch a key event to the next view on the focus path. This path runs from the top of the view tree down to the currently focused view. If this view has focus, it will dispatch to itself. Otherwise it will dispatch the next node down the focus path. This method also fires any key listeners.

Parameters
event The key event to be dispatched.
Returns
  • True if the event was handled, false otherwise.

public void hide ()

Added in API level 1

Remove the controller from the screen.

public boolean isShowing ()

Added in API level 1

public void onFinishInflate ()

Added in API level 1

Finalize inflating a view from XML. This is called as the last phase of inflation, after all child views have been added.

Even if the subclass overrides onFinishInflate, they should always be sure to call the super method, so that we get called.

public void onInitializeAccessibilityEvent (AccessibilityEvent event)

Added in API level 14

Initializes an AccessibilityEvent with information about this View which is the event source. In other words, the source of an accessibility event is the view whose state change triggered firing the event.

Example: Setting the password property of an event in addition to properties set by the super implementation:

 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
     super.onInitializeAccessibilityEvent(event);
     event.setPassword(true);
 }

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its onInitializeAccessibilityEvent(View, AccessibilityEvent) is responsible for handling this call.

Note: Always call the super implementation before adding information to the event, in case the default implementation has basic information to add.

Parameters
event The event to initialize.

public void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)

Added in API level 14

Initializes an AccessibilityNodeInfo with information about this view. The base implementation sets:

Subclasses should override this method, call the super implementation, and set additional attributes.

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo) is responsible for handling this call.

Parameters
info The instance to initialize.

public boolean onTouchEvent (MotionEvent event)

Added in API level 1

Implement this method to handle touch screen motion events.

If this method is used to detect click actions, it is recommended that the actions be performed by implementing and calling performClick(). This will ensure consistent system behavior, including:

  • obeying click sound preferences
  • dispatching OnClickListener calls
  • handling ACTION_CLICK when accessibility features are enabled

Parameters
event The motion event.
Returns
  • True if the event was handled, false otherwise.

public boolean onTrackballEvent (MotionEvent ev)

Added in API level 1

Implement this method to handle trackball motion events. The relative movement of the trackball since the last event can be retrieve with MotionEvent.getX() and MotionEvent.getY(). These are normalized so that a movement of 1 corresponds to the user pressing one DPAD key (so they will often be fractional values, representing the more fine-grained movement information available from a trackball).

Parameters
ev The motion event.
Returns
  • True if the event was handled, false otherwise.

public void setAnchorView (View view)

Added in API level 1

Set the view that acts as the anchor for the control view. This can for example be a VideoView, or your Activity's main view. When VideoView calls this method, it will use the VideoView's parent as the anchor.

Parameters
view The view to which to anchor the controller when it is visible.

public void setEnabled (boolean enabled)

Added in API level 1

Set the enabled state of this view. The interpretation of the enabled state varies by subclass.

Parameters
enabled True if this view is enabled, false otherwise.

public void setMediaPlayer (MediaController.MediaPlayerControl player)

Added in API level 1

public void setPrevNextListeners (View.OnClickListener next, View.OnClickListener prev)

Added in API level 1

public void show (int timeout)

Added in API level 1

Show the controller on screen. It will go away automatically after 'timeout' milliseconds of inactivity.

Parameters
timeout The timeout in milliseconds. Use 0 to show the controller until hide() is called.

public void show ()

Added in API level 1

Show the controller on screen. It will go away automatically after 3 seconds of inactivity.