Java Code Examples for android.support.v4.media.session.PlaybackStateCompat#ACTION_PLAY_PAUSE

The following examples show how to use android.support.v4.media.session.PlaybackStateCompat#ACTION_PLAY_PAUSE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PlaybackManager.java    From Melophile with Apache License 2.0 6 votes vote down vote up
private long getAvailableActions() {
  long actions =
          PlaybackStateCompat.ACTION_PLAY_PAUSE |
                  PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID |
                  PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH |
                  PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
                  PlaybackStateCompat.ACTION_SKIP_TO_NEXT;
  if (playback.isPlaying()) {
    actions |= PlaybackStateCompat.ACTION_PAUSE;
  } else {
    actions |= PlaybackStateCompat.ACTION_PLAY;
  }
  //
  if (isRepeat) {
    actions |= PlaybackStateCompat.ACTION_SET_REPEAT_MODE;
  }
  //
  if (isShuffle) {
    actions |= PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE_ENABLED;
  }
  return actions;
}
 
Example 2
Source File: MusicControlNotification.java    From react-native-music-control with MIT License 6 votes vote down vote up
/**
 * Code taken from newer version of the support library located in PlaybackStateCompat.toKeyCode
 * Replace this to PlaybackStateCompat.toKeyCode when React Native updates the support library
 */
private int toKeyCode(long action) {
    if (action == PlaybackStateCompat.ACTION_PLAY) {
        return KeyEvent.KEYCODE_MEDIA_PLAY;
    } else if (action == PlaybackStateCompat.ACTION_PAUSE) {
        return KeyEvent.KEYCODE_MEDIA_PAUSE;
    } else if (action == PlaybackStateCompat.ACTION_SKIP_TO_NEXT) {
        return KeyEvent.KEYCODE_MEDIA_NEXT;
    } else if (action == PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) {
        return KeyEvent.KEYCODE_MEDIA_PREVIOUS;
    } else if (action == PlaybackStateCompat.ACTION_STOP) {
        return KeyEvent.KEYCODE_MEDIA_STOP;
    } else if (action == PlaybackStateCompat.ACTION_FAST_FORWARD) {
        return KeyEvent.KEYCODE_MEDIA_FAST_FORWARD;
    } else if (action == PlaybackStateCompat.ACTION_REWIND) {
        return KeyEvent.KEYCODE_MEDIA_REWIND;
    } else if (action == PlaybackStateCompat.ACTION_PLAY_PAUSE) {
        return KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE;
    }
    return KeyEvent.KEYCODE_UNKNOWN;
}
 
Example 3
Source File: WebViewPhoneFragment.java    From carstream-android-auto with Apache License 2.0 5 votes vote down vote up
private long getAvailableActions() {
        long actions =
                PlaybackStateCompat.ACTION_PLAY_PAUSE |
                        PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID |
                        PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH |
                        PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
                        PlaybackStateCompat.ACTION_SKIP_TO_NEXT;
//        if (true) {
//            actions |= PlaybackStateCompat.ACTION_PAUSE;
//        } else {
//            actions |= PlaybackStateCompat.ACTION_PLAY;
//        }
        return actions;
    }
 
Example 4
Source File: MyMediaBrowserService.java    From carstream-android-auto with Apache License 2.0 5 votes vote down vote up
private long getAvailableActions() {
    long actions =
            PlaybackStateCompat.ACTION_PLAY_PAUSE |
                    PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID |
                    PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH |
                    PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
                    PlaybackStateCompat.ACTION_SKIP_TO_NEXT;
    return actions;
}
 
Example 5
Source File: AudioPlayerService.java    From react-native-audio-streaming-player with MIT License 5 votes vote down vote up
/**
 * Update the current media player state, optionally showing an error message.
 */
public void updatePlaybackState() {
    long position = PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN;
    if (mPlayback != null ) {
        position = mPlayback.getCurrentPosition();
    }
    long actions =
            PlaybackStateCompat.ACTION_PLAY_PAUSE |
            PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
            PlaybackStateCompat.ACTION_SKIP_TO_NEXT;

    if (mPlayback != null && mPlayback.isPlaying()) {
        actions |= PlaybackStateCompat.ACTION_PAUSE;
    } else {
        actions |= PlaybackStateCompat.ACTION_PLAY;
    }

    int state = mPlayback.getState();

    //noinspection ResourceType
    PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder()
            .setActions(actions)
            .setState(state, position, 1.0f, SystemClock.elapsedRealtime());

    mMediaSession.setPlaybackState(stateBuilder.build());

    Intent intent = new Intent("change-playback-state-event");
    intent.putExtra("state", state);
    LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
}
 
Example 6
Source File: VideoFragment.java    From leanback-homescreen-channels with Apache License 2.0 5 votes vote down vote up
@PlaybackStateCompat.Actions
private long getAvailableActions() {
    long actions = PlaybackStateCompat.ACTION_PLAY_PAUSE
            | PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID
            | PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH;
    if (mMediaPlayerGlue.isPlaying()) {
        actions |= PlaybackStateCompat.ACTION_PAUSE;
    } else {
        actions |= PlaybackStateCompat.ACTION_PLAY;
    }
    return actions;
}
 
Example 7
Source File: AudioPlayerService.java    From react-native-streaming-audio-player with MIT License 5 votes vote down vote up
/**
 * Update the current media player state, optionally showing an error message.
 */
public void updatePlaybackState() {
    long position = PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN;
    if (mPlayback != null ) {
        position = mPlayback.getCurrentPosition();
    }
    long actions =
            PlaybackStateCompat.ACTION_PLAY_PAUSE |
            PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
            PlaybackStateCompat.ACTION_SKIP_TO_NEXT;

    if (mPlayback != null && mPlayback.isPlaying()) {
        actions |= PlaybackStateCompat.ACTION_PAUSE;
    } else {
        actions |= PlaybackStateCompat.ACTION_PLAY;
    }

    int state = mPlayback.getState();

    //noinspection ResourceType
    PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder()
            .setActions(actions)
            .setState(state, position, 1.0f, SystemClock.elapsedRealtime());

    mMediaSession.setPlaybackState(stateBuilder.build());

    Intent intent = new Intent("change-playback-state-event");
    intent.putExtra("state", state);
    LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
}
 
Example 8
Source File: PlaybackManager.java    From klingar with Apache License 2.0 5 votes vote down vote up
private long getAvailableActions() {
  long actions = PlaybackStateCompat.ACTION_PLAY_PAUSE
      | PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID
      | PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH
      | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS
      | PlaybackStateCompat.ACTION_SKIP_TO_NEXT;
  if (playback.isPlaying()) {
    actions |= PlaybackStateCompat.ACTION_PAUSE;
  } else {
    actions |= PlaybackStateCompat.ACTION_PLAY;
  }
  return actions;
}
 
Example 9
Source File: MediaPlayerAdapter.java    From android-MediaBrowserService with Apache License 2.0 5 votes vote down vote up
/**
 * Set the current capabilities available on this session. Note: If a capability is not
 * listed in the bitmask of capabilities then the MediaSession will not handle it. For
 * example, if you don't want ACTION_STOP to be handled by the MediaSession, then don't
 * included it in the bitmask that's returned.
 */
@PlaybackStateCompat.Actions
private long getAvailableActions() {
    long actions = PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID
                   | PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH
                   | PlaybackStateCompat.ACTION_SKIP_TO_NEXT
                   | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS;
    switch (mState) {
        case PlaybackStateCompat.STATE_STOPPED:
            actions |= PlaybackStateCompat.ACTION_PLAY
                       | PlaybackStateCompat.ACTION_PAUSE;
            break;
        case PlaybackStateCompat.STATE_PLAYING:
            actions |= PlaybackStateCompat.ACTION_STOP
                       | PlaybackStateCompat.ACTION_PAUSE
                       | PlaybackStateCompat.ACTION_SEEK_TO;
            break;
        case PlaybackStateCompat.STATE_PAUSED:
            actions |= PlaybackStateCompat.ACTION_PLAY
                       | PlaybackStateCompat.ACTION_STOP;
            break;
        default:
            actions |= PlaybackStateCompat.ACTION_PLAY
                       | PlaybackStateCompat.ACTION_PLAY_PAUSE
                       | PlaybackStateCompat.ACTION_STOP
                       | PlaybackStateCompat.ACTION_PAUSE;
    }
    return actions;
}
 
Example 10
Source File: MusicControlModule.java    From react-native-music-control with MIT License 4 votes vote down vote up
@ReactMethod
synchronized public void enableControl(String control, boolean enable, ReadableMap options) {
    init();

    Map<String, Integer> skipOptions = new HashMap<String, Integer>();

    long controlValue;
    switch(control) {
        case "skipForward":
            if (options.hasKey("interval"))
                skipOptions.put("skipForward", options.getInt("interval"));
            controlValue = PlaybackStateCompat.ACTION_FAST_FORWARD;
            break;
        case "skipBackward":
            if (options.hasKey("interval"))
                skipOptions.put("skipBackward", options.getInt("interval"));
            controlValue = PlaybackStateCompat.ACTION_REWIND;
            break;
        case "nextTrack":
            controlValue = PlaybackStateCompat.ACTION_SKIP_TO_NEXT;
            break;
        case "previousTrack":
            controlValue = PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS;
            break;
        case "play":
            controlValue = PlaybackStateCompat.ACTION_PLAY;
            break;
        case "pause":
            controlValue = PlaybackStateCompat.ACTION_PAUSE;
            break;
        case "togglePlayPause":
            controlValue = PlaybackStateCompat.ACTION_PLAY_PAUSE;
            break;
        case "stop":
            controlValue = PlaybackStateCompat.ACTION_STOP;
            break;
        case "seek":
            controlValue = PlaybackStateCompat.ACTION_SEEK_TO;
            break;
        case "setRating":
            controlValue = PlaybackStateCompat.ACTION_SET_RATING;
            break;
        case "volume":
            volume = volume.create(enable, null, null);
            if(remoteVolume) session.setPlaybackToRemote(volume);
            return;
        case "remoteVolume":
            remoteVolume = enable;
            if(enable) {
                session.setPlaybackToRemote(volume);
            } else {
                session.setPlaybackToLocal(AudioManager.STREAM_MUSIC);
            }
            return;
        case "closeNotification":
            if(enable) {
                if (options.hasKey("when")) {
                    if ("always".equals(options.getString("when"))) {
                        this.notificationClose = notificationClose.ALWAYS;
                    }else if ("paused".equals(options.getString("when"))) {
                        this.notificationClose = notificationClose.PAUSED;
                    }else {
                        this.notificationClose = notificationClose.NEVER;
                    }
                }
                return;
            }
        default:
            // Unknown control type, let's just ignore it
            return;
    }

    if(enable) {
        controls |= controlValue;
    } else {
        controls &= ~controlValue;
    }

    notification.updateActions(controls, skipOptions);
    pb.setActions(controls);

    state = pb.build();
    session.setPlaybackState(state);

    updateNotificationMediaStyle();

    if(session.isActive()) {
        notification.show(nb, isPlaying);
    }
}