use of android.support.v4.media.MediaMetadataCompat in project android-UniversalMusicPlayer by googlesamples.
the class FullScreenPlayerActivity method connectToSession.
private void connectToSession(MediaSessionCompat.Token token) throws RemoteException {
MediaControllerCompat mediaController = new MediaControllerCompat(FullScreenPlayerActivity.this, token);
if (mediaController.getMetadata() == null) {
finish();
return;
}
setSupportMediaController(mediaController);
mediaController.registerCallback(mCallback);
PlaybackStateCompat state = mediaController.getPlaybackState();
updatePlaybackState(state);
MediaMetadataCompat metadata = mediaController.getMetadata();
if (metadata != null) {
updateMediaDescription(metadata.getDescription());
updateDuration(metadata);
}
updateProgress();
if (state != null && (state.getState() == PlaybackStateCompat.STATE_PLAYING || state.getState() == PlaybackStateCompat.STATE_BUFFERING)) {
scheduleSeekbarUpdate();
}
}
use of android.support.v4.media.MediaMetadataCompat 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);
}
if (!TvHelper.isTvUiMode(this)) {
mCastSessionManager = CastContext.getSharedInstance(this).getSessionManager();
mCastSessionManagerListener = new CastSessionManagerListener();
mCastSessionManager.addSessionManagerListener(mCastSessionManagerListener, CastSession.class);
}
mMediaRouter = MediaRouter.getInstance(getApplicationContext());
registerCarConnectionReceiver();
}
use of android.support.v4.media.MediaMetadataCompat in project android-UniversalMusicPlayer by googlesamples.
the class LocalPlayback method play.
@Override
public void play(QueueItem item) {
mPlayOnFocusGain = true;
tryToGetAudioFocus();
registerAudioNoisyReceiver();
String mediaId = item.getDescription().getMediaId();
boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId);
if (mediaHasChanged) {
mCurrentPosition = 0;
mCurrentMediaId = mediaId;
}
if (mState == PlaybackStateCompat.STATE_PAUSED && !mediaHasChanged && mMediaPlayer != null) {
configMediaPlayerState();
} else {
mState = PlaybackStateCompat.STATE_STOPPED;
// release everything except MediaPlayer
relaxResources(false);
MediaMetadataCompat track = mMusicProvider.getMusic(MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId()));
//noinspection ResourceType
String source = track.getString(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE);
if (source != null) {
// Escape spaces for URLs
source = source.replaceAll(" ", "%20");
}
try {
createMediaPlayerIfNeeded();
mState = PlaybackStateCompat.STATE_BUFFERING;
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(source);
// Starts preparing the media player in the background. When
// it's done, it will call our OnPreparedListener (that is,
// the onPrepared() method on this class, since we set the
// listener to 'this'). Until the media player is prepared,
// we *cannot* call start() on it!
mMediaPlayer.prepareAsync();
// If we are streaming from the internet, we want to hold a
// Wifi lock, which prevents the Wifi radio from going to
// sleep while the song is playing.
mWifiLock.acquire();
if (mCallback != null) {
mCallback.onPlaybackStatusChanged(mState);
}
} catch (IOException ex) {
LogHelper.e(TAG, ex, "Exception playing song");
if (mCallback != null) {
mCallback.onError(ex.getMessage());
}
}
}
}
use of android.support.v4.media.MediaMetadataCompat in project android-UniversalMusicPlayer by googlesamples.
the class MusicProviderTest method testFavorite.
@Test
public void testFavorite() throws Exception {
MediaMetadataCompat metadata = provider.getShuffledMusic().iterator().next();
String musicId = metadata.getDescription().getMediaId();
assertFalse(provider.isFavorite(musicId));
provider.setFavorite(musicId, true);
assertTrue(provider.isFavorite(musicId));
provider.setFavorite(musicId, false);
assertFalse(provider.isFavorite(musicId));
}
use of android.support.v4.media.MediaMetadataCompat in project android-UniversalMusicPlayer by googlesamples.
the class MusicProviderTest method testSearchBySongTitle.
@Test
public void testSearchBySongTitle() throws Exception {
int count = 0;
for (MediaMetadataCompat metadata : provider.searchMusicBySongTitle("Romantic")) {
String title = metadata.getString(MediaMetadataCompat.METADATA_KEY_TITLE);
assertTrue(title.contains("Romantic"));
count++;
}
assertEquals(2, count);
}
Aggregations