Example usage for android.media.session MediaController getTransportControls

List of usage examples for android.media.session MediaController getTransportControls

Introduction

In this page you can find the example usage for android.media.session MediaController getTransportControls.

Prototype

public @NonNull TransportControls getTransportControls() 

Source Link

Document

Get a TransportControls instance to send transport actions to the associated session.

Usage

From source file:org.opensilk.music.playback.service.PlaybackService.java

void handleIntentCommand(@NonNull Intent intent) {
    final String action = intent.getAction();
    final String command = SERVICECMD.equals(action) ? intent.getStringExtra(CMDNAME) : null;
    Timber.v("handleIntentCommand: action = %s, command = %s", action, command);
    MediaController controller = mMediaSession.getController();
    MediaController.TransportControls controls = controller.getTransportControls();
    PlaybackState state = controller.getPlaybackState();
    if (CMDNEXT.equals(command) || NEXT_ACTION.equals(action)) {
        controls.skipToNext();//from www .j  a  va 2s . c  o  m
    } else if (CMDPREVIOUS.equals(command) || PREVIOUS_ACTION.equals(action)) {
        if (state == null || state.getPosition() < REWIND_INSTEAD_PREVIOUS_THRESHOLD) {
            controls.skipToPrevious();
        } else {
            controls.seekTo(0);
            //TODO might need play
        }
    } else if (CMDTOGGLEPAUSE.equals(command) || TOGGLEPAUSE_ACTION.equals(action)) {
        if (state == null || state.getState() != PlaybackState.STATE_PLAYING) {
            controls.pause();
        } else {
            controls.play();
        }
    } else if (CMDPAUSE.equals(command) || PAUSE_ACTION.equals(action)) {
        controls.pause();
    } else if (CMDPLAY.equals(command)) {
        controls.play();
    } else if (CMDSTOP.equals(command) || STOP_ACTION.equals(action)) {
        controls.stop();
    } else if (REPEAT_ACTION.equals(action)) {
        controls.sendCustomAction(CMD.CYCLE_REPEAT, null);
    } else if (SHUFFLE_ACTION.equals(action)) {
        controls.sendCustomAction(CMD.SHUFFLE_QUEUE, null);
    }
}