Search in sources :

Example 1 with PlaybackManager

use of com.example.android.uamp.playback.PlaybackManager in project android-UniversalMusicPlayer by googlesamples.

the class MusicService method onCreate.

/*
     * (non-Javadoc)
     * @see android.app.Service#onCreate()
     */
@Override
public void onCreate() {
    super.onCreate();
    LogHelper.d(TAG, "onCreate");
    mMusicProvider = new MusicProvider();
    // To make the app more responsive, fetch and cache catalog information now.
    // This can help improve the response time in the method
    // {@link #onLoadChildren(String, Result<List<MediaItem>>) onLoadChildren()}.
    mMusicProvider.retrieveMediaAsync(null);
    mPackageValidator = new PackageValidator(this);
    QueueManager queueManager = new QueueManager(mMusicProvider, getResources(), new QueueManager.MetadataUpdateListener() {

        @Override
        public void onMetadataChanged(MediaMetadataCompat metadata) {
            mSession.setMetadata(metadata);
        }

        @Override
        public void onMetadataRetrieveError() {
            mPlaybackManager.updatePlaybackState(getString(R.string.error_no_metadata));
        }

        @Override
        public void onCurrentQueueIndexUpdated(int queueIndex) {
            mPlaybackManager.handlePlayRequest();
        }

        @Override
        public void onQueueUpdated(String title, List<MediaSessionCompat.QueueItem> newQueue) {
            mSession.setQueue(newQueue);
            mSession.setQueueTitle(title);
        }
    });
    LocalPlayback playback = new LocalPlayback(this, mMusicProvider);
    mPlaybackManager = new PlaybackManager(this, getResources(), mMusicProvider, queueManager, playback);
    // Start a new MediaSession
    mSession = new MediaSessionCompat(this, "MusicService");
    setSessionToken(mSession.getSessionToken());
    mSession.setCallback(mPlaybackManager.getMediaSessionCallback());
    mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    Context context = getApplicationContext();
    Intent intent = new Intent(context, NowPlayingActivity.class);
    PendingIntent pi = PendingIntent.getActivity(context, 99, /*request code*/
    intent, PendingIntent.FLAG_UPDATE_CURRENT);
    mSession.setSessionActivity(pi);
    mSessionExtras = new Bundle();
    CarHelper.setSlotReservationFlags(mSessionExtras, true, true, true);
    WearHelper.setSlotReservationFlags(mSessionExtras, true, true);
    WearHelper.setUseBackgroundFromTheme(mSessionExtras, true);
    mSession.setExtras(mSessionExtras);
    mPlaybackManager.updatePlaybackState(null);
    try {
        mMediaNotificationManager = new MediaNotificationManager(this);
    } catch (RemoteException e) {
        throw new IllegalStateException("Could not create a MediaNotificationManager", e);
    }
    int playServicesAvailable = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);
    if (!TvHelper.isTvUiMode(this) && playServicesAvailable == ConnectionResult.SUCCESS) {
        mCastSessionManager = CastContext.getSharedInstance(this).getSessionManager();
        mCastSessionManagerListener = new CastSessionManagerListener();
        mCastSessionManager.addSessionManagerListener(mCastSessionManagerListener, CastSession.class);
    }
    mMediaRouter = MediaRouter.getInstance(getApplicationContext());
    registerCarConnectionReceiver();
}
Also used : Context(android.content.Context) CastContext(com.google.android.gms.cast.framework.CastContext) MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) Bundle(android.os.Bundle) PlaybackManager(com.example.android.uamp.playback.PlaybackManager) LocalPlayback(com.example.android.uamp.playback.LocalPlayback) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat) QueueManager(com.example.android.uamp.playback.QueueManager) PendingIntent(android.app.PendingIntent) MusicProvider(com.example.android.uamp.model.MusicProvider) RemoteException(android.os.RemoteException)

Aggregations

PendingIntent (android.app.PendingIntent)1 Context (android.content.Context)1 Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 RemoteException (android.os.RemoteException)1 MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)1 MediaSessionCompat (android.support.v4.media.session.MediaSessionCompat)1 MusicProvider (com.example.android.uamp.model.MusicProvider)1 LocalPlayback (com.example.android.uamp.playback.LocalPlayback)1 PlaybackManager (com.example.android.uamp.playback.PlaybackManager)1 QueueManager (com.example.android.uamp.playback.QueueManager)1 CastContext (com.google.android.gms.cast.framework.CastContext)1