Example usage for android.support.v4.media.session MediaSessionCompat FLAG_HANDLES_QUEUE_COMMANDS

List of usage examples for android.support.v4.media.session MediaSessionCompat FLAG_HANDLES_QUEUE_COMMANDS

Introduction

In this page you can find the example usage for android.support.v4.media.session MediaSessionCompat FLAG_HANDLES_QUEUE_COMMANDS.

Prototype

int FLAG_HANDLES_QUEUE_COMMANDS

To view the source code for android.support.v4.media.session MediaSessionCompat FLAG_HANDLES_QUEUE_COMMANDS.

Click Source Link

Document

Sets this flag on the session to indicate that it handles queue management commands through its Callback .

Usage

From source file:com.scooter1556.sms.android.service.MediaService.java

@RequiresApi(api = Build.VERSION_CODES.O)
@Override// ww  w .j a  v a 2  s .  c  o m
public void onCreate() {
    super.onCreate();

    Log.d(TAG, "onCreate()");

    restService = RESTService.getInstance();

    // Retrieve preferences if they exist
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    // Load default settings
    PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

    // Initialise database
    ConnectionDatabase db = new ConnectionDatabase(this);

    // Check connection
    long id = sharedPreferences.getLong("Connection", -1);

    if (id >= 0) {
        Connection connection = db.getConnection(id);
        restService.setConnection(connection);
        isConnected = true;
    } else {
        isConnected = false;
    }

    queueManager = new QueueManager(getApplicationContext(), new QueueManager.MetadataUpdateListener() {
        @Override
        public void onMetadataChanged(MediaMetadataCompat metadata) {
            Log.d(TAG, "onMetadataChanged()");

            mediaSession.setMetadata(metadata);
        }

        @Override
        public void onMetadataRetrieveError() {
            Log.d(TAG, "onMetadataRetrieveError()");

            playbackManager.updatePlaybackState(getString(R.string.error_no_metadata));
        }

        @Override
        public void onCurrentQueueIndexUpdated(int queueIndex) {
            Log.d(TAG, "onCurrentQueueIndexUpdated(" + queueIndex + ")");

            playbackManager.handlePlayRequest();
        }

        @Override
        public void onQueueUpdated(List<MediaSessionCompat.QueueItem> newQueue) {
            Log.d(TAG, "onQueueUpdated()");

            mediaSession.setQueue(newQueue);
            mediaSession.setQueueTitle("Now Playing");
        }
    });

    // Initialise playback manager
    playbackManager = PlaybackManager.getInstance();
    playbackManager.initialise(getApplicationContext(), this, queueManager);

    // Start a new Media Session
    mediaSession = new MediaSessionCompat(this, MediaService.class.getSimpleName());
    mediaSession.setCallback(playbackManager.getMediaSessionCallback());
    mediaSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS
                    | MediaSessionCompat.FLAG_HANDLES_QUEUE_COMMANDS);

    Context context = getApplicationContext();
    Intent intent = new Intent(context, NowPlayingActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 99, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    mediaSession.setSessionActivity(pendingIntent);

    mediaSessionExtras = new Bundle();
    AutoUtils.setSlotReservationFlags(mediaSessionExtras, true, true, true);
    mediaSession.setExtras(mediaSessionExtras);

    setSessionToken(mediaSession.getSessionToken());

    try {
        mediaNotificationManager = new MediaNotificationManager(this);
    } catch (RemoteException e) {
        throw new IllegalStateException("Could not create a MediaNotificationManager", e);
    }

    if (!TVUtils.isTvUiMode(this)) {
        castSessionManager = CastContext.getSharedInstance(this).getSessionManager();
        castSessionManagerListener = new CastSessionManagerListener();
        castSessionManager.addSessionManagerListener(castSessionManagerListener, CastSession.class);
    }

    mediaRouter = MediaRouter.getInstance(getApplicationContext());

    registerCarConnectionReceiver();

    // Register connectivity receiver
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    this.registerReceiver(connectivityChangeReceiver, intentFilter);
}