public class TransportMediator extends TransportController
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.
Modifier and Type | Field and Description |
---|---|
static int |
FLAG_KEY_MEDIA_FAST_FORWARD
Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_FAST_FORWARD
RemoteControlClient.FLAG_KEY_MEDIA_FAST_FORWARD
|
static int |
FLAG_KEY_MEDIA_NEXT
Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_NEXT
RemoteControlClient.FLAG_KEY_MEDIA_NEXT
|
static int |
FLAG_KEY_MEDIA_PAUSE
Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_PAUSE
RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
|
static int |
FLAG_KEY_MEDIA_PLAY
Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_PLAY
RemoteControlClient.FLAG_KEY_MEDIA_PLAY
|
static int |
FLAG_KEY_MEDIA_PLAY_PAUSE
Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_PLAY_PAUSE
RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
|
static int |
FLAG_KEY_MEDIA_PREVIOUS
Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_PREVIOUS
RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
|
static int |
FLAG_KEY_MEDIA_REWIND
Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_REWIND
RemoteControlClient.FLAG_KEY_MEDIA_REWIND
|
static int |
FLAG_KEY_MEDIA_STOP
Synonym for {@link android.media.RemoteControlClient#FLAG_KEY_MEDIA_STOP
RemoteControlClient.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 and Description |
---|
TransportMediator(Activity activity,
TransportPerformer callbacks) |
TransportMediator(View view,
TransportPerformer callbacks) |
Modifier and Type | Method and Description |
---|---|
void |
destroy()
Optionally call when no longer using the TransportController.
|
boolean |
dispatchKeyEvent(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.
|
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.
|
public static final int KEYCODE_MEDIA_PLAY
KeyEvent.KEYCODE_MEDIA_PLAY
public static final int KEYCODE_MEDIA_PAUSE
KeyEvent.KEYCODE_MEDIA_PAUSE
public static final int KEYCODE_MEDIA_RECORD
KeyEvent.KEYCODE_MEDIA_RECORD
public static final int FLAG_KEY_MEDIA_PREVIOUS
public static final int FLAG_KEY_MEDIA_REWIND
public static final int FLAG_KEY_MEDIA_PLAY
public static final int FLAG_KEY_MEDIA_PLAY_PAUSE
public static final int FLAG_KEY_MEDIA_PAUSE
public static final int FLAG_KEY_MEDIA_STOP
public static final int FLAG_KEY_MEDIA_FAST_FORWARD
public static final int FLAG_KEY_MEDIA_NEXT
public TransportMediator(Activity activity, TransportPerformer callbacks)
public TransportMediator(View view, TransportPerformer callbacks)
public Object getRemoteControlClient()
RemoteControlClient
associated with this transport.
This returns a generic Object since the RemoteControlClient is not availble before
Build.VERSION_CODES.ICE_CREAM_SANDWICH
. Further, this class
will not use RemoteControlClient in its implementation until
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.
public boolean dispatchKeyEvent(KeyEvent event)
Activity.dispatchKeyEvent
to give
the transport an opportunity to intercept media keys. Any such keys will show up
in TransportPerformer
.event
- public void registerStateListener(TransportStateListener listener)
TransportController
registerStateListener
in class TransportController
public void unregisterStateListener(TransportStateListener listener)
TransportController
unregisterStateListener
in class TransportController
public void refreshState()
public void startPlaying()
startPlaying
in class TransportController
public void pausePlaying()
pausePlaying
in class TransportController
public void stopPlaying()
stopPlaying
in class TransportController
public long getDuration()
TransportController
getDuration
in class TransportController
public long getCurrentPosition()
TransportController
getCurrentPosition
in class TransportController
public void seekTo(long pos)
TransportController
seekTo
in class TransportController
pos
- Position to move to, in milliseconds.public boolean isPlaying()
TransportController
isPlaying
in class TransportController
public int getBufferPercentage()
TransportController
getBufferPercentage
in class TransportController
public int getTransportControlFlags()
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
getTransportControlFlags
in class TransportController
public void destroy()