Search in sources :

Example 1 with MediaItem

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);
}
Also used : MediaBrowserCompat(android.support.v4.media.MediaBrowserCompat) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat) MediaDescriptionCompat(android.support.v4.media.MediaDescriptionCompat)

Example 2 with MediaItem

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);
        }
    });
}
Also used : Intent(android.content.Intent) OnItemViewClickedListener(android.support.v17.leanback.widget.OnItemViewClickedListener) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat) View(android.view.View) MediaBrowserCompat(android.support.v4.media.MediaBrowserCompat) MediaItem(android.support.v4.media.MediaBrowserCompat.MediaItem) RowPresenter(android.support.v17.leanback.widget.RowPresenter) Presenter(android.support.v17.leanback.widget.Presenter) ListRowPresenter(android.support.v17.leanback.widget.ListRowPresenter) MediaItem(android.support.v4.media.MediaBrowserCompat.MediaItem) ListRow(android.support.v17.leanback.widget.ListRow) Row(android.support.v17.leanback.widget.Row) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat) RowPresenter(android.support.v17.leanback.widget.RowPresenter) ListRowPresenter(android.support.v17.leanback.widget.ListRowPresenter)

Example 3 with MediaItem

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

Example 4 with MediaItem

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;
}
Also used : FragmentActivity(android.support.v4.app.FragmentActivity) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat)

Example 5 with MediaItem

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());
}
Also used : MediaBrowserCompat(android.support.v4.media.MediaBrowserCompat) MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) MockResources(android.test.mock.MockResources) Test(org.junit.Test)

Aggregations

MediaBrowserCompat (android.support.v4.media.MediaBrowserCompat)3 MediaSessionCompat (android.support.v4.media.session.MediaSessionCompat)3 Intent (android.content.Intent)2 MediaDescriptionCompat (android.support.v4.media.MediaDescriptionCompat)2 MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)2 MediaControllerCompat (android.support.v4.media.session.MediaControllerCompat)2 PendingIntent (android.app.PendingIntent)1 Context (android.content.Context)1 Bundle (android.os.Bundle)1 RemoteException (android.os.RemoteException)1 ListRow (android.support.v17.leanback.widget.ListRow)1 ListRowPresenter (android.support.v17.leanback.widget.ListRowPresenter)1 OnItemViewClickedListener (android.support.v17.leanback.widget.OnItemViewClickedListener)1 Presenter (android.support.v17.leanback.widget.Presenter)1 Row (android.support.v17.leanback.widget.Row)1 RowPresenter (android.support.v17.leanback.widget.RowPresenter)1 FragmentActivity (android.support.v4.app.FragmentActivity)1 MediaItem (android.support.v4.media.MediaBrowserCompat.MediaItem)1 MockResources (android.test.mock.MockResources)1 View (android.view.View)1