android.support.v4.media
Class TransportMediator

java.lang.Object
  extended by android.support.v4.media.TransportController
      extended by android.support.v4.media.TransportMediator

public class TransportMediator
extends TransportController

Helper for implementing a media transport control (with play, pause, skip, and other media actions). Takes care of both key events and advanced features like RemoteControlClient. This class is intended to serve as an intermediary between transport controls (whether they be on-screen controls, hardware buttons, remote controls) and the actual player. The player is represented by a single TransportPerformer that must be supplied to this class. On-screen controls that want to control and show the state of the player should do this through calls to the TransportController interface.

Here is a simple but fairly complete sample of a video player that is built around this class. Note that the MediaController class used here is not the one included in the standard Android framework, but a custom implementation. Real applications often implement their own transport controls, or you can copy the implementation here out of Support4Demos.


Field Summary
static int FLAG_KEY_MEDIA_FAST_FORWARD
          Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_FAST_FORWARD RemoveControlClient.FLAG_KEY_MEDIA_FAST_FORWARD
static int FLAG_KEY_MEDIA_NEXT
          Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_NEXT RemoveControlClient.FLAG_KEY_MEDIA_NEXT
static int FLAG_KEY_MEDIA_PAUSE
          Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_PAUSE RemoveControlClient.FLAG_KEY_MEDIA_PAUSE
static int FLAG_KEY_MEDIA_PLAY
          Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_PLAY RemoveControlClient.FLAG_KEY_MEDIA_PLAY
static int FLAG_KEY_MEDIA_PLAY_PAUSE
          Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_PLAY_PAUSE RemoveControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
static int FLAG_KEY_MEDIA_PREVIOUS
          Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_PREVIOUS RemoveControlClient.FLAG_KEY_MEDIA_PREVIOUS
static int FLAG_KEY_MEDIA_REWIND
          Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_REWIND RemoveControlClient.FLAG_KEY_MEDIA_REWIND
static int FLAG_KEY_MEDIA_STOP
          Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_STOP RemoveControlClient.FLAG_KEY_MEDIA_STOP
static int KEYCODE_MEDIA_PAUSE
          Synonym for KeyEvent.KEYCODE_MEDIA_PAUSE
static int KEYCODE_MEDIA_PLAY
          Synonym for KeyEvent.KEYCODE_MEDIA_PLAY
static int KEYCODE_MEDIA_RECORD
          Synonym for KeyEvent.KEYCODE_MEDIA_RECORD
 
Constructor Summary
TransportMediator(android.app.Activity activity, TransportPerformer callbacks)
           
TransportMediator(android.view.View view, TransportPerformer callbacks)
           
 
Method Summary
 void destroy()
          Optionally call when no longer using the TransportController.
 boolean dispatchKeyEvent(android.view.KeyEvent event)
          Must call from Activity.dispatchKeyEvent to give the transport an opportunity to intercept media keys.
 int getBufferPercentage()
          Retrieve amount, in percentage (0-100), that the media stream has been buffered on to the local device.
 long getCurrentPosition()
          Retrieve the current playback location in the media stream, in milliseconds.
 long getDuration()
          Retrieve the total duration of the media stream, in milliseconds.
 java.lang.Object getRemoteControlClient()
          Return the RemoteControlClient associated with this transport.
 int getTransportControlFlags()
          Retrieves the flags for the media transport control buttons that this transport supports.
 boolean isPlaying()
          Return whether the player is currently playing its stream.
 void pausePlaying()
          Move the controller into the paused state.
 void refreshState()
           
 void registerStateListener(TransportStateListener listener)
          Start listening to changes in playback state.
 void seekTo(long pos)
          Move to a new location in the media stream.
 void startPlaying()
          Move the controller into the playing state.
 void stopPlaying()
          Move the controller into the stopped state.
 void unregisterStateListener(TransportStateListener listener)
          Stop listening to changes in playback state.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

KEYCODE_MEDIA_PLAY

public static final int KEYCODE_MEDIA_PLAY
Synonym for KeyEvent.KEYCODE_MEDIA_PLAY

See Also:
Constant Field Values

KEYCODE_MEDIA_PAUSE

public static final int KEYCODE_MEDIA_PAUSE
Synonym for KeyEvent.KEYCODE_MEDIA_PAUSE

See Also:
Constant Field Values

KEYCODE_MEDIA_RECORD

public static final int KEYCODE_MEDIA_RECORD
Synonym for KeyEvent.KEYCODE_MEDIA_RECORD

See Also:
Constant Field Values

FLAG_KEY_MEDIA_PREVIOUS

public static final int FLAG_KEY_MEDIA_PREVIOUS
Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_PREVIOUS RemoveControlClient.FLAG_KEY_MEDIA_PREVIOUS

See Also:
Constant Field Values

FLAG_KEY_MEDIA_REWIND

public static final int FLAG_KEY_MEDIA_REWIND
Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_REWIND RemoveControlClient.FLAG_KEY_MEDIA_REWIND

See Also:
Constant Field Values

FLAG_KEY_MEDIA_PLAY

public static final int FLAG_KEY_MEDIA_PLAY
Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_PLAY RemoveControlClient.FLAG_KEY_MEDIA_PLAY

See Also:
Constant Field Values

FLAG_KEY_MEDIA_PLAY_PAUSE

public static final int FLAG_KEY_MEDIA_PLAY_PAUSE
Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_PLAY_PAUSE RemoveControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE

See Also:
Constant Field Values

FLAG_KEY_MEDIA_PAUSE

public static final int FLAG_KEY_MEDIA_PAUSE
Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_PAUSE RemoveControlClient.FLAG_KEY_MEDIA_PAUSE

See Also:
Constant Field Values

FLAG_KEY_MEDIA_STOP

public static final int FLAG_KEY_MEDIA_STOP
Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_STOP RemoveControlClient.FLAG_KEY_MEDIA_STOP

See Also:
Constant Field Values

FLAG_KEY_MEDIA_FAST_FORWARD

public static final int FLAG_KEY_MEDIA_FAST_FORWARD
Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_FAST_FORWARD RemoveControlClient.FLAG_KEY_MEDIA_FAST_FORWARD

See Also:
Constant Field Values

FLAG_KEY_MEDIA_NEXT

public static final int FLAG_KEY_MEDIA_NEXT
Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_NEXT RemoveControlClient.FLAG_KEY_MEDIA_NEXT

See Also:
Constant Field Values
Constructor Detail

TransportMediator

public TransportMediator(android.app.Activity activity,
                         TransportPerformer callbacks)

TransportMediator

public TransportMediator(android.view.View view,
                         TransportPerformer callbacks)
Method Detail

getRemoteControlClient

public java.lang.Object getRemoteControlClient()
Return the RemoteControlClient associated with this transport. This returns a generic Object since the RemoteControlClient is not availble before android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH. Further, this class will not use RemoteControlClient in its implementation until android.os.Build.VERSION_CODES#JELLY_BEAN_MR2. You should always check for null here and not do anything with the RemoteControlClient if none is given; this way you don't need to worry about the current platform API version.

Note that this class takes possession of the RemoteControlClient.OnGetPlaybackPositionListener and RemoteControlClient.OnPlaybackPositionUpdateListener callbacks; you will interact with these through TransportPerformer.onGetCurrentPosition and TransportPerformer.onSeekTo, respectively.


dispatchKeyEvent

public boolean dispatchKeyEvent(android.view.KeyEvent event)
Must call from Activity.dispatchKeyEvent to give the transport an opportunity to intercept media keys. Any such keys will show up in TransportPerformer.

Parameters:
event -

registerStateListener

public void registerStateListener(TransportStateListener listener)
Description copied from class: TransportController
Start listening to changes in playback state.

Specified by:
registerStateListener in class TransportController

unregisterStateListener

public void unregisterStateListener(TransportStateListener listener)
Description copied from class: TransportController
Stop listening to changes in playback state.

Specified by:
unregisterStateListener in class TransportController

refreshState

public void refreshState()

startPlaying

public void startPlaying()
Move the controller into the playing state. This updates the remote control client to indicate it is playing, and takes audio focus for the app.

Specified by:
startPlaying in class TransportController

pausePlaying

public void pausePlaying()
Move the controller into the paused state. This updates the remote control client to indicate it is paused, but keeps audio focus.

Specified by:
pausePlaying in class TransportController

stopPlaying

public void stopPlaying()
Move the controller into the stopped state. This updates the remote control client to indicate it is stopped, and removes audio focus from the app.

Specified by:
stopPlaying in class TransportController

getDuration

public long getDuration()
Description copied from class: TransportController
Retrieve the total duration of the media stream, in milliseconds.

Specified by:
getDuration in class TransportController

getCurrentPosition

public long getCurrentPosition()
Description copied from class: TransportController
Retrieve the current playback location in the media stream, in milliseconds.

Specified by:
getCurrentPosition in class TransportController

seekTo

public void seekTo(long pos)
Description copied from class: TransportController
Move to a new location in the media stream.

Specified by:
seekTo in class TransportController
Parameters:
pos - Position to move to, in milliseconds.

isPlaying

public boolean isPlaying()
Description copied from class: TransportController
Return whether the player is currently playing its stream.

Specified by:
isPlaying in class TransportController

getBufferPercentage

public int getBufferPercentage()
Description copied from class: TransportController
Retrieve amount, in percentage (0-100), that the media stream has been buffered on to the local device. Return 100 if the stream is always local.

Specified by:
getBufferPercentage in class TransportController

getTransportControlFlags

public int getTransportControlFlags()
Retrieves the flags for the media transport control buttons that this transport supports. Result is a combination of the following flags: FLAG_KEY_MEDIA_PREVIOUS, FLAG_KEY_MEDIA_REWIND, FLAG_KEY_MEDIA_PLAY, FLAG_KEY_MEDIA_PLAY_PAUSE, FLAG_KEY_MEDIA_PAUSE, FLAG_KEY_MEDIA_STOP, FLAG_KEY_MEDIA_FAST_FORWARD, FLAG_KEY_MEDIA_NEXT

Specified by:
getTransportControlFlags in class TransportController

destroy

public void destroy()
Optionally call when no longer using the TransportController. Its resources will also be automatically cleaned up when your activity/view is detached from its window, so you don't normally need to call this explicitly.