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();
}
Aggregations