android.media.MediaDescription Java Examples

The following examples show how to use android.media.MediaDescription. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AutoMediaBrowserService.java    From AndroidDemoProjects with Apache License 2.0 6 votes vote down vote up
private MediaBrowser.MediaItem generatePlayableMediaItem( Song song ) {
    if( song == null )
        return null;

    MediaDescription.Builder mediaDescriptionBuilder = new MediaDescription.Builder();
    mediaDescriptionBuilder.setMediaId( song.getuId() );

    if( !TextUtils.isEmpty( song.getTitle() ) )
        mediaDescriptionBuilder.setTitle( song.getTitle() );

    if( !TextUtils.isEmpty( song.getArtist() ) )
        mediaDescriptionBuilder.setSubtitle( song.getArtist() );

    if( !TextUtils.isEmpty( song.getThumbnailUrl() ) )
        mediaDescriptionBuilder.setIconUri( Uri.parse( song.getThumbnailUrl() ) );

    return new MediaBrowser.MediaItem( mediaDescriptionBuilder.build(), MediaBrowser.MediaItem.FLAG_PLAYABLE );
}
 
Example #2
Source File: Video.java    From tv-samples with Apache License 2.0 5 votes vote down vote up
public Video buildFromMediaDesc(MediaDescription desc) {
    return new Video(
            Long.parseLong(desc.getMediaId()),
            "", // Category - not provided by MediaDescription.
            String.valueOf(desc.getTitle()),
            String.valueOf(desc.getDescription()),
            "", // Media URI - not provided by MediaDescription.
            "", // Background Image URI - not provided by MediaDescription.
            String.valueOf(desc.getIconUri()),
            String.valueOf(desc.getSubtitle())
    );
}
 
Example #3
Source File: WearBrowserService.java    From Muzesto with GNU General Public License v3.0 5 votes vote down vote up
private void fillMediaItems(List<MediaBrowser.MediaItem> mediaItems, String mediaId, String title, Uri icon, String subTitle, int playableOrBrowsable) {
    mediaItems.add(new MediaBrowser.MediaItem(
            new MediaDescription.Builder()
                    .setMediaId(mediaId)
                    .setTitle(title)
                    .setIconUri(icon)
                    .setSubtitle(subTitle)
                    .build(), playableOrBrowsable
    ));
}
 
Example #4
Source File: Video.java    From androidtv-Leanback with Apache License 2.0 5 votes vote down vote up
public Video buildFromMediaDesc(MediaDescription desc) {
    return new Video(
            Long.parseLong(desc.getMediaId()),
            "", // Category - not provided by MediaDescription.
            String.valueOf(desc.getTitle()),
            String.valueOf(desc.getDescription()),
            "", // Media URI - not provided by MediaDescription.
            "", // Background Image URI - not provided by MediaDescription.
            String.valueOf(desc.getIconUri()),
            String.valueOf(desc.getSubtitle())
    );
}
 
Example #5
Source File: AutoMediaBrowserService.java    From AndroidDemoProjects with Apache License 2.0 5 votes vote down vote up
private MediaBrowser.MediaItem generateBrowseableMediaItemByGenre( String genre ) {
    MediaDescription.Builder mediaDescriptionBuilder = new MediaDescription.Builder();
    mediaDescriptionBuilder.setMediaId( genre );
    mediaDescriptionBuilder.setTitle( genre );
    mediaDescriptionBuilder.setIconBitmap( BitmapFactory.decodeResource( getResources(), R.drawable.folder ) );

    return new MediaBrowser.MediaItem( mediaDescriptionBuilder.build(), MediaBrowser.MediaItem.FLAG_BROWSABLE );
}
 
Example #6
Source File: MediaSessionRecord.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private String getShortMetadataString() {
    int fields = mMetadata == null ? 0 : mMetadata.size();
    MediaDescription description = mMetadata == null ? null : mMetadata
            .getDescription();
    return "size=" + fields + ", description=" + description;
}
 
Example #7
Source File: WearBrowserService.java    From Muzesto with GNU General Public License v3.0 4 votes vote down vote up
private void addMediaRoots(List<MediaBrowser.MediaItem> mMediaRoot) {
    mMediaRoot.add(new MediaBrowser.MediaItem(
            new MediaDescription.Builder()
                    .setMediaId(Integer.toString(TYPE_ARTIST))
                    .setTitle(getString(R.string.artists))
                    .setIconUri(Uri.parse("android.resource://" +
                            "naman14.timber/drawable/ic_empty_music2"))
                    .setSubtitle(getString(R.string.artists))
                    .build(), MediaBrowser.MediaItem.FLAG_BROWSABLE
    ));

    mMediaRoot.add(new MediaBrowser.MediaItem(
            new MediaDescription.Builder()
                    .setMediaId(Integer.toString(TYPE_ALBUM))
                    .setTitle(getString(R.string.albums))
                    .setIconUri(Uri.parse("android.resource://" +
                            "naman14.timber/drawable/ic_empty_music2"))
                    .setSubtitle(getString(R.string.albums))
                    .build(), MediaBrowser.MediaItem.FLAG_BROWSABLE
    ));

    mMediaRoot.add(new MediaBrowser.MediaItem(
            new MediaDescription.Builder()
                    .setMediaId(Integer.toString(TYPE_SONG))
                    .setTitle(getString(R.string.songs))
                    .setIconUri(Uri.parse("android.resource://" +
                            "naman14.timber/drawable/ic_empty_music2"))
                    .setSubtitle(getString(R.string.songs))
                    .build(), MediaBrowser.MediaItem.FLAG_BROWSABLE
    ));


    mMediaRoot.add(new MediaBrowser.MediaItem(
            new MediaDescription.Builder()
                    .setMediaId(Integer.toString(TYPE_PLAYLIST))
                    .setTitle(getString(R.string.playlists))
                    .setIconUri(Uri.parse("android.resource://" +
                            "naman14.timber/drawable/ic_empty_music2"))
                    .setSubtitle(getString(R.string.playlists))
                    .build(), MediaBrowser.MediaItem.FLAG_BROWSABLE
    ));

}
 
Example #8
Source File: MediaItemViewHolder.java    From android-music-player with Apache License 2.0 4 votes vote down vote up
static View setupView(Activity activity, View convertView, ViewGroup parent,
                                MediaDescription description, int state) {

    if (sColorStateNotPlaying == null || sColorStatePlaying == null) {
        initializeColorStateLists(activity);
    }

    MediaItemViewHolder holder;

    Integer cachedState = STATE_INVALID;

    if (convertView == null) {
        convertView = LayoutInflater.from(activity)
                .inflate(R.layout.media_list_item, parent, false);
        holder = new MediaItemViewHolder();
        holder.mImageView = (ImageView) convertView.findViewById(R.id.play_eq);
        holder.mTitleView = (TextView) convertView.findViewById(R.id.title);
        holder.mDescriptionView = (TextView) convertView.findViewById(R.id.description);
        convertView.setTag(holder);
    } else {
        holder = (MediaItemViewHolder) convertView.getTag();
        cachedState = (Integer) convertView.getTag(R.id.tag_mediaitem_state_cache);
    }

    holder.mTitleView.setText(description.getTitle());
    holder.mDescriptionView.setText(description.getSubtitle());

    // If the state of convertView is different, we need to adapt the view to the
    // new state.
    if (cachedState == null || cachedState != state) {
        switch (state) {
            case STATE_PLAYABLE:
                holder.mImageView.setImageDrawable(
                    activity.getDrawable(R.drawable.ic_play_arrow_black_36dp));
                holder.mImageView.setImageTintList(sColorStateNotPlaying);
                holder.mImageView.setVisibility(View.VISIBLE);
                break;
            case STATE_PLAYING:
                AnimationDrawable animation = (AnimationDrawable)
                    activity.getDrawable(R.drawable.ic_equalizer_white_36dp);
                holder.mImageView.setImageDrawable(animation);
                holder.mImageView.setImageTintList(sColorStatePlaying);
                holder.mImageView.setVisibility(View.VISIBLE);
                animation.start();
                break;
            case STATE_PAUSED:
                holder.mImageView.setImageDrawable(
                    activity.getDrawable(R.drawable.ic_equalizer1_white_36dp));
                holder.mImageView.setImageTintList(sColorStateNotPlaying);
                holder.mImageView.setVisibility(View.VISIBLE);
                break;
            default:
                holder.mImageView.setVisibility(View.GONE);
        }
        convertView.setTag(R.id.tag_mediaitem_state_cache, state);
    }

    return convertView;
}
 
Example #9
Source File: MediaNotificationManager.java    From android-music-player with Apache License 2.0 4 votes vote down vote up
public void update(MediaMetadata metadata, PlaybackState state, MediaSession.Token token) {
    if (state == null || state.getState() == PlaybackState.STATE_STOPPED ||
            state.getState() == PlaybackState.STATE_NONE) {
        mService.stopForeground(true);
        try {
            mService.unregisterReceiver(this);
        } catch (IllegalArgumentException ex) {
            // ignore receiver not registered
        }
        mService.stopSelf();
        return;
    }
    if (metadata == null) {
        return;
    }
    boolean isPlaying = state.getState() == PlaybackState.STATE_PLAYING;
    Notification.Builder notificationBuilder = new Notification.Builder(mService);
    MediaDescription description = metadata.getDescription();

    notificationBuilder
            .setStyle(new Notification.MediaStyle()
                    .setMediaSession(token)
                    .setShowActionsInCompactView(0, 1, 2))
            .setColor(mService.getApplication().getResources().getColor(R.color.notification_bg))
            .setSmallIcon(R.drawable.ic_notification)
            .setVisibility(Notification.VISIBILITY_PUBLIC)
            .setContentIntent(createContentIntent())
            .setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle())
            .setLargeIcon(MusicLibrary.getAlbumBitmap(mService, description.getMediaId()))
            .setOngoing(isPlaying)
            .setWhen(isPlaying ? System.currentTimeMillis() - state.getPosition() : 0)
            .setShowWhen(isPlaying)
            .setUsesChronometer(isPlaying);

    // If skip to next action is enabled
    if ((state.getActions() & PlaybackState.ACTION_SKIP_TO_PREVIOUS) != 0) {
        notificationBuilder.addAction(mPrevAction);
    }

    notificationBuilder.addAction(isPlaying ? mPauseAction : mPlayAction);

    // If skip to prev action is enabled
    if ((state.getActions() & PlaybackState.ACTION_SKIP_TO_NEXT) != 0) {
        notificationBuilder.addAction(mNextAction);
    }

    Notification notification = notificationBuilder.build();

    if (isPlaying && !mStarted) {
        mService.startService(new Intent(mService.getApplicationContext(), MusicService.class));
        mService.startForeground(NOTIFICATION_ID, notification);
        mStarted = true;
    } else {
        if (!isPlaying) {
            mService.stopForeground(false);
            mStarted = false;
        }
        mNotificationManager.notify(NOTIFICATION_ID, notification);
    }
}
 
Example #10
Source File: MediaItemViewHolder.java    From android-music-player with Apache License 2.0 4 votes vote down vote up
static View setupView(Activity activity, View convertView, ViewGroup parent,
                                MediaDescription description, int state) {

    if (sColorStateNotPlaying == null || sColorStatePlaying == null) {
        initializeColorStateLists(activity);
    }

    MediaItemViewHolder holder;

    Integer cachedState = STATE_INVALID;

    if (convertView == null) {
        convertView = LayoutInflater.from(activity)
                .inflate(R.layout.media_list_item, parent, false);
        holder = new MediaItemViewHolder();
        holder.mImageView = (ImageView) convertView.findViewById(R.id.play_eq);
        holder.mTitleView = (TextView) convertView.findViewById(R.id.title);
        holder.mDescriptionView = (TextView) convertView.findViewById(R.id.description);
        convertView.setTag(holder);
    } else {
        holder = (MediaItemViewHolder) convertView.getTag();
        cachedState = (Integer) convertView.getTag(R.id.tag_mediaitem_state_cache);
    }

    holder.mTitleView.setText(description.getTitle());
    holder.mDescriptionView.setText(description.getSubtitle());

    // If the state of convertView is different, we need to adapt the view to the
    // new state.
    if (cachedState == null || cachedState != state) {
        switch (state) {
            case STATE_PLAYABLE:
                holder.mImageView.setImageDrawable(
                    activity.getDrawable(R.drawable.ic_play_arrow_black_36dp));
                holder.mImageView.setImageTintList(sColorStateNotPlaying);
                holder.mImageView.setVisibility(View.VISIBLE);
                break;
            case STATE_PLAYING:
                AnimationDrawable animation = (AnimationDrawable)
                    activity.getDrawable(R.drawable.ic_equalizer_white_36dp);
                holder.mImageView.setImageDrawable(animation);
                holder.mImageView.setImageTintList(sColorStatePlaying);
                holder.mImageView.setVisibility(View.VISIBLE);
                animation.start();
                break;
            case STATE_PAUSED:
                holder.mImageView.setImageDrawable(
                    activity.getDrawable(R.drawable.ic_equalizer1_white_36dp));
                holder.mImageView.setImageTintList(sColorStateNotPlaying);
                holder.mImageView.setVisibility(View.VISIBLE);
                break;
            default:
                holder.mImageView.setVisibility(View.GONE);
        }
        convertView.setTag(R.id.tag_mediaitem_state_cache, state);
    }

    return convertView;
}
 
Example #11
Source File: MediaItemViewHolder.java    From io2015-codelabs with Apache License 2.0 4 votes vote down vote up
static View setupView(Activity activity, View convertView, ViewGroup parent,
                                MediaDescription description, int state) {

    if (sColorStateNotPlaying == null || sColorStatePlaying == null) {
        initializeColorStateLists(activity);
    }

    MediaItemViewHolder holder;

    Integer cachedState = STATE_INVALID;

    if (convertView == null) {
        convertView = LayoutInflater.from(activity)
                .inflate(R.layout.media_list_item, parent, false);
        holder = new MediaItemViewHolder();
        holder.mImageView = (ImageView) convertView.findViewById(R.id.play_eq);
        holder.mTitleView = (TextView) convertView.findViewById(R.id.title);
        holder.mDescriptionView = (TextView) convertView.findViewById(R.id.description);
        convertView.setTag(holder);
    } else {
        holder = (MediaItemViewHolder) convertView.getTag();
        cachedState = (Integer) convertView.getTag(R.id.tag_mediaitem_state_cache);
    }

    holder.mTitleView.setText(description.getTitle());
    holder.mDescriptionView.setText(description.getSubtitle());

    // If the state of convertView is different, we need to adapt the view to the
    // new state.
    if (cachedState == null || cachedState != state) {
        switch (state) {
            case STATE_PLAYABLE:
                holder.mImageView.setImageDrawable(
                    activity.getDrawable(R.drawable.ic_play_arrow_black_36dp));
                holder.mImageView.setImageTintList(sColorStateNotPlaying);
                holder.mImageView.setVisibility(View.VISIBLE);
                break;
            case STATE_PLAYING:
                AnimationDrawable animation = (AnimationDrawable)
                    activity.getDrawable(R.drawable.ic_equalizer_white_36dp);
                holder.mImageView.setImageDrawable(animation);
                holder.mImageView.setImageTintList(sColorStatePlaying);
                holder.mImageView.setVisibility(View.VISIBLE);
                animation.start();
                break;
            case STATE_PAUSED:
                holder.mImageView.setImageDrawable(
                    activity.getDrawable(R.drawable.ic_equalizer1_white_36dp));
                holder.mImageView.setImageTintList(sColorStateNotPlaying);
                holder.mImageView.setVisibility(View.VISIBLE);
                break;
            default:
                holder.mImageView.setVisibility(View.GONE);
        }
        convertView.setTag(R.id.tag_mediaitem_state_cache, state);
    }

    return convertView;
}
 
Example #12
Source File: MediaNotificationManager.java    From io2015-codelabs with Apache License 2.0 4 votes vote down vote up
public void update(MediaMetadata metadata, PlaybackState state, MediaSession.Token token) {
    if (state == null || state.getState() == PlaybackState.STATE_STOPPED ||
            state.getState() == PlaybackState.STATE_NONE) {
        mService.stopForeground(true);
        try {
            mService.unregisterReceiver(this);
        } catch (IllegalArgumentException ex) {
            // ignore receiver not registered
        }
        mService.stopSelf();
        return;
    }
    if (metadata == null) {
        return;
    }
    boolean isPlaying = state.getState() == PlaybackState.STATE_PLAYING;
    Notification.Builder notificationBuilder = new Notification.Builder(mService);
    MediaDescription description = metadata.getDescription();

    notificationBuilder
            .setStyle(new Notification.MediaStyle()
                    .setMediaSession(token)
                    .setShowActionsInCompactView(0, 1, 2))
            .setColor(mService.getApplication().getResources().getColor(R.color.notification_bg))
            .setSmallIcon(R.drawable.ic_notification)
            .setVisibility(Notification.VISIBILITY_PUBLIC)
            .setContentIntent(createContentIntent())
            .setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle())
            .setLargeIcon(MusicLibrary.getAlbumBitmap(mService, description.getMediaId()))
            .setOngoing(isPlaying)
            .setWhen(isPlaying ? System.currentTimeMillis() - state.getPosition() : 0)
            .setShowWhen(isPlaying)
            .setUsesChronometer(isPlaying);

    // If skip to next action is enabled
    if ((state.getActions() & PlaybackState.ACTION_SKIP_TO_PREVIOUS) != 0) {
        notificationBuilder.addAction(mPrevAction);
    }

    notificationBuilder.addAction(isPlaying ? mPauseAction : mPlayAction);

    // If skip to prev action is enabled
    if ((state.getActions() & PlaybackState.ACTION_SKIP_TO_NEXT) != 0) {
        notificationBuilder.addAction(mNextAction);
    }

    Notification notification = notificationBuilder.build();

    if (isPlaying && !mStarted) {
        mService.startService(new Intent(mService.getApplicationContext(), MusicService.class));
        mService.startForeground(NOTIFICATION_ID, notification);
        mStarted = true;
    } else {
        if (!isPlaying) {
            mService.stopForeground(false);
            mStarted = false;
        }
        mNotificationManager.notify(NOTIFICATION_ID, notification);
    }
}
 
Example #13
Source File: MediaItemViewHolder.java    From io2015-codelabs with Apache License 2.0 4 votes vote down vote up
static View setupView(Activity activity, View convertView, ViewGroup parent,
                                MediaDescription description, int state) {

    if (sColorStateNotPlaying == null || sColorStatePlaying == null) {
        initializeColorStateLists(activity);
    }

    MediaItemViewHolder holder;

    Integer cachedState = STATE_INVALID;

    if (convertView == null) {
        convertView = LayoutInflater.from(activity)
                .inflate(R.layout.media_list_item, parent, false);
        holder = new MediaItemViewHolder();
        holder.mImageView = (ImageView) convertView.findViewById(R.id.play_eq);
        holder.mTitleView = (TextView) convertView.findViewById(R.id.title);
        holder.mDescriptionView = (TextView) convertView.findViewById(R.id.description);
        convertView.setTag(holder);
    } else {
        holder = (MediaItemViewHolder) convertView.getTag();
        cachedState = (Integer) convertView.getTag(R.id.tag_mediaitem_state_cache);
    }

    holder.mTitleView.setText(description.getTitle());
    holder.mDescriptionView.setText(description.getSubtitle());

    // If the state of convertView is different, we need to adapt the view to the
    // new state.
    if (cachedState == null || cachedState != state) {
        switch (state) {
            case STATE_PLAYABLE:
                holder.mImageView.setImageDrawable(
                    activity.getDrawable(R.drawable.ic_play_arrow_black_36dp));
                holder.mImageView.setImageTintList(sColorStateNotPlaying);
                holder.mImageView.setVisibility(View.VISIBLE);
                break;
            case STATE_PLAYING:
                AnimationDrawable animation = (AnimationDrawable)
                    activity.getDrawable(R.drawable.ic_equalizer_white_36dp);
                holder.mImageView.setImageDrawable(animation);
                holder.mImageView.setImageTintList(sColorStatePlaying);
                holder.mImageView.setVisibility(View.VISIBLE);
                animation.start();
                break;
            case STATE_PAUSED:
                holder.mImageView.setImageDrawable(
                    activity.getDrawable(R.drawable.ic_equalizer1_white_36dp));
                holder.mImageView.setImageTintList(sColorStateNotPlaying);
                holder.mImageView.setVisibility(View.VISIBLE);
                break;
            default:
                holder.mImageView.setVisibility(View.GONE);
        }
        convertView.setTag(R.id.tag_mediaitem_state_cache, state);
    }

    return convertView;
}