use of android.support.v4.media.MediaBrowserCompat.MediaItem in project android-UniversalMusicPlayer by googlesamples.
the class CardPresenter method onBindViewHolder.
@Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
MediaDescriptionCompat description;
final CardViewHolder cardViewHolder = (CardViewHolder) viewHolder;
// Determine description and playing state of item based on instance type
cardViewHolder.setState(MediaItemViewHolder.STATE_NONE);
if (item instanceof MediaBrowserCompat.MediaItem) {
MediaBrowserCompat.MediaItem mediaItem = (MediaBrowserCompat.MediaItem) item;
LogHelper.d(TAG, "onBindViewHolder MediaItem: ", mediaItem.toString());
description = mediaItem.getDescription();
cardViewHolder.setState(MediaItemViewHolder.getMediaItemState(mContext, mediaItem));
} else if (item instanceof MediaSessionCompat.QueueItem) {
MediaSessionCompat.QueueItem queueItem = (MediaSessionCompat.QueueItem) item;
LogHelper.d(TAG, "onBindViewHolder QueueItem: ", queueItem.toString());
description = queueItem.getDescription();
if (QueueHelper.isQueueItemPlaying(mContext, queueItem)) {
cardViewHolder.setState(MediaItemViewHolder.getStateFromController(mContext));
}
} else {
throw new IllegalArgumentException("Object must be MediaItem or QueueItem, not " + item.getClass().getSimpleName());
}
cardViewHolder.setupCardView(mContext, description);
}
use of android.support.v4.media.MediaBrowserCompat.MediaItem in project android-UniversalMusicPlayer by googlesamples.
the class TvBrowseFragment method setupEventListeners.
private void setupEventListeners() {
setOnItemViewClickedListener(new OnItemViewClickedListener() {
@Override
public void onItemClicked(Presenter.ViewHolder viewHolder, Object clickedItem, RowPresenter.ViewHolder viewHolder2, Row row) {
if (clickedItem instanceof MediaBrowserCompat.MediaItem) {
MediaItem item = (MediaItem) clickedItem;
if (item.isPlayable()) {
LogHelper.w(TAG, "Ignoring click on PLAYABLE MediaItem in" + "TvBrowseFragment. mediaId=", item.getMediaId());
return;
}
Intent intent = new Intent(getActivity(), TvVerticalGridActivity.class);
intent.putExtra(TvBrowseActivity.SAVED_MEDIA_ID, item.getMediaId());
intent.putExtra(TvBrowseActivity.BROWSE_TITLE, item.getDescription().getTitle());
startActivity(intent);
} else if (clickedItem instanceof MediaSessionCompat.QueueItem) {
MediaSessionCompat.QueueItem item = (MediaSessionCompat.QueueItem) clickedItem;
MediaControllerCompat mediaController = getActivity().getSupportMediaController();
if (!QueueHelper.isQueueItemPlaying(getActivity(), item)) {
mediaController.getTransportControls().skipToQueueItem(item.getQueueId());
}
Intent intent = new Intent(getActivity(), TvPlaybackActivity.class);
startActivity(intent);
}
}
});
setOnSearchClickedListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LogHelper.d(TAG, "In-app search");
// TODO: implement in-app search
Intent intent = new Intent(getActivity(), TvBrowseActivity.class);
startActivity(intent);
}
});
}
use of android.support.v4.media.MediaBrowserCompat.MediaItem 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.MediaBrowserCompat.MediaItem in project android-UniversalMusicPlayer by googlesamples.
the class MediaIDHelper method isMediaItemPlaying.
/**
* Determine if media item is playing (matches the currently playing media item).
*
* @param context for retrieving the {@link MediaControllerCompat}
* @param mediaItem to compare to currently playing {@link MediaBrowserCompat.MediaItem}
* @return boolean indicating whether media item matches currently playing media item
*/
public static boolean isMediaItemPlaying(Context context, MediaBrowserCompat.MediaItem mediaItem) {
// Media item is considered to be playing or paused based on the controller's current
// media id
MediaControllerCompat controller = ((FragmentActivity) context).getSupportMediaController();
if (controller != null && controller.getMetadata() != null) {
String currentPlayingMediaId = controller.getMetadata().getDescription().getMediaId();
String itemMusicId = MediaIDHelper.extractMusicIDFromMediaID(mediaItem.getDescription().getMediaId());
if (currentPlayingMediaId != null && TextUtils.equals(currentPlayingMediaId, itemMusicId)) {
return true;
}
}
return false;
}
use of android.support.v4.media.MediaBrowserCompat.MediaItem in project android-UniversalMusicPlayer by googlesamples.
the class MusicProviderTest method testGetChildren.
@Test
public void testGetChildren() throws Exception {
MockResources resources = new MockResources() {
@NonNull
@Override
public String getString(int id) throws NotFoundException {
return "";
}
@NonNull
@Override
public String getString(int id, Object... formatArgs) throws NotFoundException {
return "";
}
};
// test an invalid root
List<MediaBrowserCompat.MediaItem> invalid = provider.getChildren("INVALID_MEDIA_ID", resources);
assertEquals(0, invalid.size());
// test level 1 (list of category types - only "by genre" for now)
List<MediaBrowserCompat.MediaItem> level1 = provider.getChildren(MediaIDHelper.MEDIA_ID_ROOT, resources);
assertEquals(1, level1.size());
// test level 2 (list of genres)
int genreCount = 0;
for (String ignored : provider.getGenres()) {
genreCount++;
}
List<MediaBrowserCompat.MediaItem> level2 = provider.getChildren(level1.get(0).getMediaId(), resources);
assertEquals(genreCount, level2.size());
// test level 3 (list of music for a given genre)
List<MediaBrowserCompat.MediaItem> level3 = provider.getChildren(level2.get(0).getMediaId(), resources);
String genre = MediaIDHelper.extractBrowseCategoryValueFromMediaID(level2.get(0).getMediaId());
for (MediaBrowserCompat.MediaItem mediaItem : level3) {
assertTrue(mediaItem.isPlayable());
assertFalse(mediaItem.isBrowsable());
MediaMetadataCompat metadata = provider.getMusic(MediaIDHelper.extractMusicIDFromMediaID(mediaItem.getMediaId()));
assertEquals(genre, metadata.getString(MediaMetadataCompat.METADATA_KEY_GENRE));
}
// test an invalid level 4
List<MediaBrowserCompat.MediaItem> invalidLevel4 = provider.getChildren(level3.get(0).getMediaId(), resources);
assertTrue(invalidLevel4.isEmpty());
}
Aggregations