List of usage examples for android.support.v4.media.session MediaSessionCompat MediaSessionCompat
private MediaSessionCompat(Context context, MediaSessionImpl impl)
From source file:com.murati.oszk.audiobook.MusicService.java
@Override public void onCreate() { super.onCreate(); LogHelper.d(TAG, "onCreate"); mMusicProvider = new MusicProvider(this.getApplicationContext()); // 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 /* Callback */); mPackageValidator = new PackageValidator(this); QueueManager queueManager = new QueueManager(mMusicProvider, getResources(), new QueueManager.MetadataUpdateListener() { @Override//from w w w. j av a 2 s. com 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); } if (!TvHelper.isTvUiMode(this)) { mCastSessionManager = CastContext.getSharedInstance(this).getSessionManager(); mCastSessionManagerListener = new CastSessionManagerListener(); mCastSessionManager.addSessionManagerListener(mCastSessionManagerListener, CastSession.class); } mMediaRouter = MediaRouter.getInstance(getApplicationContext()); registerCarConnectionReceiver(); }
From source file:com.example.android.uamp.MusicService.java
@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 /* Callback */); mPackageValidator = new PackageValidator(this); QueueManager queueManager = new QueueManager(mMusicProvider, getResources(), new QueueManager.MetadataUpdateListener() { @Override//from w ww . j a v a 2s . co m 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(); }
From source file:com.example.androidcodes.testmediaplayer.MusicService.java
@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 /* Callback */); mPackageValidator = new PackageValidator(this); QueueManager queueManager = new QueueManager(mMusicProvider, getResources(), new QueueManager.MetadataUpdateListener() { @Override//from w ww. j ava 2 s .co m 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); } if (!TvHelper.isTvUiMode(this)) { mCastSessionManager = CastContext.getSharedInstance(this).getSessionManager(); mCastSessionManagerListener = new CastSessionManagerListener(); mCastSessionManager.addSessionManagerListener(mCastSessionManagerListener, CastSession.class); } mMediaRouter = MediaRouter.getInstance(getApplicationContext()); registerCarConnectionReceiver(); }
From source file:com.scooter1556.sms.android.service.MediaService.java
@RequiresApi(api = Build.VERSION_CODES.O) @Override//from ww w. j av a 2 s. c om 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); }
From source file:com.example.chu.googleplaylibrary.MusicService.java
@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 /* Callback */); mPackageValidator = new PackageValidator(this); QueueManager queueManager = new QueueManager(mMusicProvider, getResources(), new QueueManager.MetadataUpdateListener() { @Override/*w w w . jav a 2 s .c o m*/ 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); VideoCastManager.getInstance().addVideoCastConsumer(mCastConsumer); mMediaRouter = MediaRouter.getInstance(getApplicationContext()); registerCarConnectionReceiver(); }
From source file:rocks.stalin.android.app.MusicService.java
@Override public void onCreate() { super.onCreate(); LogHelper.d(TAG, "onCreate"); timeProvider = new LocalOffsetService(); executorService = ServiceLocator.getInstance().getService(TaskExecutor.class); scheduler = ServiceLocator.getInstance().getService(TaskScheduler.class); mMusicProvider = new MusicProvider(new ExternalStorageSource(getApplicationContext())); //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 /* Callback */); mPackageValidator = new PackageValidator(this); QueueManager queueManager = new QueueManager(mMusicProvider, getResources(), new QueueManager.MetadataUpdateListener() { @Override/*from w w w . j av a2s .c o m*/ 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) { //TODO: This causes mediaPlayer the mediaplayer to get restarted twice on song change //There's currently a race condition there -JJ 21/05-2017 //mPlaybackManager.handlePlayRequest(); } @Override public void onQueueUpdated(String title, List<MediaSessionCompat.QueueItem> newQueue) { mSession.setQueue(newQueue); mSession.setQueueTitle(title); } }); LogHelper.i(TAG, "Starting manager"); //TODO: extract port number somewhere else, possible even not set it TCPServerConnectionFactory connectionFactory = new TCPServerConnectionFactory(8009, executorService); connectionFactory.setListener(this); executorService.submit(connectionFactory); WifiP2pManager rawManager = getSystemService(WifiP2pManager.class); WifiP2pManager.Channel channel = rawManager.initialize(this, getMainLooper(), null); WifiP2PManagerFacade manager = new WifiP2PManagerFacade(rawManager, channel); server = new WifiP2pServiceAnnouncer(manager, 8009, executorService); server.start(); remotePlayback = new RemotePlayback(this, mMusicProvider, timeProvider, scheduler); mPlaybackManager = new PlaybackManager(this, getResources(), mMusicProvider, queueManager, remotePlayback); // 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(); mSession.setExtras(mSessionExtras); mPlaybackManager.updatePlaybackState(null); try { mMediaNotificationManager = new MediaNotificationManager(this); } catch (RemoteException e) { throw new IllegalStateException("Could not create a MediaNotificationManager", e); } mCastSessionManager = CastContext.getSharedInstance(this).getSessionManager(); mCastSessionManagerListener = new CastSessionManagerListener(); mCastSessionManager.addSessionManagerListener(mCastSessionManagerListener, CastSession.class); mMediaRouter = MediaRouter.getInstance(getApplicationContext()); }
From source file:com.pi.android.brainbeats.MusicService.java
@Override public void onCreate() { super.onCreate(); LogHelper.d(TAG, "onCreate"); mMusicProvider = new MusicProvider(getApplicationContext()); // 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 /* Callback */); mPackageValidator = new PackageValidator(this); QueueManager queueManager = new QueueManager(mMusicProvider, getResources(), new QueueManager.MetadataUpdateListener() { @Override/*w w w. java 2s . c o m*/ 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); } VideoCastManager.getInstance().addVideoCastConsumer(mCastConsumer); mMediaRouter = MediaRouter.getInstance(getApplicationContext()); registerCarConnectionReceiver(); }
From source file:com.example.android.AudioArchive.MusicService.java
@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 /* Callback */); mPackageValidator = new PackageValidator(this); QueueManager queueManager = new QueueManager(mMusicProvider, getResources(), new QueueManager.MetadataUpdateListener() { @Override/*from w w w .java 2s .c om*/ 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); } VideoCastManager.getInstance().addVideoCastConsumer(mCastConsumer); mMediaRouter = MediaRouter.getInstance(getApplicationContext()); registerCarConnectionReceiver(); }
From source file:com.bayapps.android.robophish.MusicService.java
@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 /* Callback */); mPackageValidator = new PackageValidator(this); QueueManager queueManager = new QueueManager(mMusicProvider, getResources(), new QueueManager.MetadataUpdateListener() { @Override//from ww w . j a v a 2 s.c o m 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); } VideoCastManager.getInstance().addVideoCastConsumer(mCastConsumer); mMediaRouter = MediaRouter.getInstance(getApplicationContext()); registerCarConnectionReceiver(); }
From source file:com.example.hp.smartstor.CloudMusicManager.uamp.MusicService.java
@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 /* Callback */); mPackageValidator = new com.example.hp.smartstor.CloudMusicManager.uamp.PackageValidator(this); QueueManager queueManager = new QueueManager(mMusicProvider, getResources(), new QueueManager.MetadataUpdateListener() { @Override//from w w w . j a va 2 s . co m 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 com.example.hp.smartstor.CloudMusicManager.uamp.MediaNotificationManager( this); } catch (RemoteException e) { throw new IllegalStateException("Could not create a MediaNotificationManager", e); } VideoCastManager.getInstance().addVideoCastConsumer(mCastConsumer); mMediaRouter = MediaRouter.getInstance(getApplicationContext()); registerCarConnectionReceiver(); }