FileDocCategorySizeDatePackage
TransportPerformer.javaAPI DocAndroid 5.1 API8527Thu Mar 12 22:22:56 GMT 2015android.support.v4.media

TransportPerformer

public abstract class TransportPerformer extends Object
Implemented by the playback side of the media system, to respond to requests to perform actions and to retrieve its current state. These requests may either come from key events dispatched directly to your UI, or events sent over a media button event receiver that this class keeps active while your window is in focus.

Fields Summary
static final int
AUDIOFOCUS_GAIN
static final int
AUDIOFOCUS_GAIN_TRANSIENT
static final int
AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK
static final int
AUDIOFOCUS_LOSS
static final int
AUDIOFOCUS_LOSS_TRANSIENT
static final int
AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK
Constructors Summary
Methods Summary
public voidonAudioFocusChange(int focusChange)
Report that audio focus has changed on the app. This only happens if you have indicated you have started playing with {@link TransportMediator#startPlaying TransportController.startPlaying}, which takes audio focus for you.

param
focusChange The type of focus change, as per {@link android.media.AudioManager.OnAudioFocusChangeListener#onAudioFocusChange(int) OnAudioFocusChangeListener.onAudioFocusChange}. The default implementation will deliver a {@link KeyEvent#KEYCODE_MEDIA_STOP} when receiving {@link android.media.AudioManager#AUDIOFOCUS_LOSS}.


                                                                 
        
        int keyCode = 0;
        switch (focusChange) {
            case AUDIOFOCUS_LOSS:
                // This will cause us to stop playback, which means we drop audio focus
                // so we will not get any further audio focus gain.
                keyCode = TransportMediator.KEYCODE_MEDIA_PAUSE;
                break;
        }
        if (keyCode != 0) {
            final long now = SystemClock.uptimeMillis();
            onMediaButtonDown(keyCode, new KeyEvent(now, now, KeyEvent.ACTION_DOWN, keyCode, 0));
            onMediaButtonUp(keyCode, new KeyEvent(now, now, KeyEvent.ACTION_UP, keyCode, 0));
        }
    
public intonGetBufferPercentage()
Request to find out how much of the media has been buffered on the local device.

return
Return a percentage (0-100) indicating how much of the total data has been buffered. The default implementation returns 100, meaning the content is always on the local device.

        return 100;
    
public abstract longonGetCurrentPosition()
Request to return the current playback position, in milliseconds.

public abstract longonGetDuration()
Request to return the duration of the current media, in milliseconds.

public intonGetTransportControlFlags()
Retrieves the flags for the media transport control buttons that this transport supports. Result is a combination of the following flags: {@link TransportMediator#FLAG_KEY_MEDIA_PREVIOUS}, {@link TransportMediator#FLAG_KEY_MEDIA_REWIND}, {@link TransportMediator#FLAG_KEY_MEDIA_PLAY}, {@link TransportMediator#FLAG_KEY_MEDIA_PLAY_PAUSE}, {@link TransportMediator#FLAG_KEY_MEDIA_PAUSE}, {@link TransportMediator#FLAG_KEY_MEDIA_STOP}, {@link TransportMediator#FLAG_KEY_MEDIA_FAST_FORWARD}, {@link TransportMediator#FLAG_KEY_MEDIA_NEXT}

The default implementation returns: {@link TransportMediator#FLAG_KEY_MEDIA_PLAY}, {@link TransportMediator#FLAG_KEY_MEDIA_PLAY_PAUSE}, {@link TransportMediator#FLAG_KEY_MEDIA_PAUSE}, and {@link TransportMediator#FLAG_KEY_MEDIA_STOP}

        return TransportMediator.FLAG_KEY_MEDIA_PLAY
                | TransportMediator.FLAG_KEY_MEDIA_PLAY_PAUSE
                | TransportMediator.FLAG_KEY_MEDIA_PAUSE
                | TransportMediator.FLAG_KEY_MEDIA_STOP;
    
public abstract booleanonIsPlaying()
Request to find out whether the player is currently playing its media.

public booleanonMediaButtonDown(int keyCode, android.view.KeyEvent event)
Report that a media button has been pressed. This is like {@link android.view.KeyEvent.Callback#onKeyDown(int, android.view.KeyEvent)} but will only deliver media keys. The default implementation handles these keys:
  • KEYCODE_MEDIA_PLAY: call {@link #onStart}
  • KEYCODE_MEDIA_PAUSE: call {@link #onPause}
  • KEYCODE_MEDIA_STOP: call {@link #onStop}
  • KEYCODE_MEDIA_PLAY_PAUSE and KEYCODE_HEADSETHOOK: call {@link #onPause} if {@link #onIsPlaying()} returns true, otherwise call {@link #onStart}

param
keyCode The code of the media key.
param
event The full key event.
return
Indicate whether the key has been consumed. The default implementation always returns true. This only matters for keys being dispatched here from {@link TransportMediator#dispatchKeyEvent(android.view.KeyEvent) TransportController.dispatchKeyEvent}, and determines whether the key continues on to its default key handling (which for media keys means being delivered to the current media remote control, which should be us).

        switch (keyCode) {
            case TransportMediator.KEYCODE_MEDIA_PLAY:
                onStart();
                return true;
            case TransportMediator.KEYCODE_MEDIA_PAUSE:
                onPause();
                return true;
            case KeyEvent.KEYCODE_MEDIA_STOP:
                onStop();
                return true;
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
            case KeyEvent.KEYCODE_HEADSETHOOK:
                if (onIsPlaying()) {
                    onPause();
                } else {
                    onStart();
                }
        }
        return true;
    
public booleanonMediaButtonUp(int keyCode, android.view.KeyEvent event)
Report that a media button has been released. This is like {@link KeyEvent.Callback#onKeyUp(int, android.view.KeyEvent)} but will only deliver media keys. The default implementation does nothing.

param
keyCode The code of the media key.
param
event The full key event.
return
Indicate whether the key has been consumed. The default implementation always returns true. This only matters for keys being dispatched here from {@link TransportMediator#dispatchKeyEvent(android.view.KeyEvent) TransportController.dispatchKeyEvent}, and determines whether the key continues on to its default key handling (which for media keys means being delivered to the current media remote control, which should be us).

        return true;
    
public abstract voidonPause()
Request to pause playback of the media, staying at the current playback position and other state so a later call to {@link #onStart()} will resume at the same place.

public abstract voidonSeekTo(long pos)
Request to move the current playback position.

param
pos New position to move to, in milliseconds.

public abstract voidonStart()
Request to start playback on the media, resuming from whatever current state (position etc) it is in.

public abstract voidonStop()
Request to completely stop playback of the media, clearing whatever state the player thinks is appropriate.