Java Code Examples for android.support.v4.media.session.MediaSessionCompat#setPlaybackState()

The following examples show how to use android.support.v4.media.session.MediaSessionCompat#setPlaybackState() . 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: MainActivity.java    From Android-9-Development-Cookbook with MIT License 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MediaSessionCompat mediaSession =
            new MediaSessionCompat(this, getApplication().getPackageName());
    mediaSession.setCallback(mMediaSessionCallback);
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);
    mediaSession.setActive(true);
    PlaybackStateCompat state = new PlaybackStateCompat.Builder()
            .setActions(PlaybackStateCompat.ACTION_PLAY |
                    PlaybackStateCompat.ACTION_PLAY_PAUSE |
                    PlaybackStateCompat.ACTION_PAUSE |
                    PlaybackStateCompat.ACTION_SKIP_TO_NEXT |
                    PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS).build();
    mediaSession.setPlaybackState(state);
}
 
Example 2
Source File: VinylCastService.java    From vinyl-cast with MIT License 5 votes vote down vote up
private void registerMediaSession() {
    // Create a MediaSessionCompat
    mediaSession = new MediaSessionCompat(this, TAG);

    // Set an initial PlaybackState with ACTION_PLAY, so media buttons can start the player
    PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder()
            .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_STOP);
    mediaSession.setPlaybackState(stateBuilder.build());

    // MediaSessionCallback() has methods that handle callbacks from a media controller
    mediaSession.setCallback(mediaSessionCallback);

    // Set the session's token so that client activities can communicate with it.
    setSessionToken(mediaSession.getSessionToken());
}
 
Example 3
Source File: MusicNotificationManager.java    From vk_music_android with GNU General Public License v3.0 5 votes vote down vote up
private void createMediaSession() {
    ComponentName receiver = new ComponentName(context.getPackageName(), RemoteReceiver.class.getName());
    mediaSession = new MediaSessionCompat(context, "MusicService", receiver, null);
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mediaSession.setPlaybackState(
            new PlaybackStateCompat.Builder()
                    .setState(PlaybackStateCompat.STATE_PLAYING, 0, 0)
                    .setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE)
                    .build()
    );

    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    audioManager.requestAudioFocus(focusChange -> {}, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    mediaSession.setActive(true);
}