Search in sources :

Example 1 with MediaMetadataCompat

use of android.support.v4.media.MediaMetadataCompat in project Shuttle by timusus.

the class VideoCastManager method updateMediaSessionMetadata.

/*
     * On ICS and JB, lock screen metadata is one liner: Title - Album Artist - Album. On KitKat, it
     * has two lines: Title , Album Artist - Album
     */
private void updateMediaSessionMetadata() {
    if ((mMediaSessionCompat == null) || !isFeatureEnabled(CastConfiguration.FEATURE_LOCKSCREEN)) {
        return;
    }
    try {
        MediaInfo info = getRemoteMediaInformation();
        if (info == null) {
            return;
        }
        final MediaMetadata mm = info.getMetadata();
        MediaMetadataCompat currentMetadata = mMediaSessionCompat.getController().getMetadata();
        MediaMetadataCompat.Builder newBuilder = currentMetadata == null ? new MediaMetadataCompat.Builder() : new MediaMetadataCompat.Builder(currentMetadata);
        MediaMetadataCompat metadata = newBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, mm.getString(MediaMetadata.KEY_TITLE)).putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, mContext.getResources().getString(R.string.ccl_casting_to_device, getDeviceName())).putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, mm.getString(MediaMetadata.KEY_TITLE)).putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE, mm.getString(MediaMetadata.KEY_SUBTITLE)).putLong(MediaMetadataCompat.METADATA_KEY_DURATION, info.getStreamDuration()).build();
        mMediaSessionCompat.setMetadata(metadata);
        Uri iconUri = mm.hasImages() ? mm.getImages().get(0).getUrl() : null;
        if (iconUri == null) {
            Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.album_art_placeholder);
            mMediaSessionCompat.setMetadata(newBuilder.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, bm).build());
        } else {
            if (mMediaSessionIconFetchTask != null) {
                mMediaSessionIconFetchTask.cancel(true);
            }
            mMediaSessionIconFetchTask = new FetchBitmapTask() {

                @Override
                protected void onPostExecute(Bitmap bitmap) {
                    if (bitmap != null && mMediaSessionCompat != null) {
                        MediaMetadataCompat currentMetadata = mMediaSessionCompat.getController().getMetadata();
                        MediaMetadataCompat.Builder newBuilder = currentMetadata == null ? new MediaMetadataCompat.Builder() : new MediaMetadataCompat.Builder(currentMetadata);
                        mMediaSessionCompat.setMetadata(newBuilder.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, bitmap).build());
                    }
                    mMediaSessionIconFetchTask = null;
                }
            };
            mMediaSessionIconFetchTask.execute(iconUri);
        }
    } catch (NotFoundException e) {
        LOGE(TAG, "Failed to update Media Session due to resource not found", e);
    } catch (TransientNetworkDisconnectionException | NoConnectionException e) {
        LOGE(TAG, "Failed to update Media Session due to network issues", e);
    }
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) NoConnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.NoConnectionException) Builder(com.google.android.gms.cast.Cast.CastOptions.Builder) NotFoundException(android.content.res.Resources.NotFoundException) FetchBitmapTask(com.google.android.libraries.cast.companionlibrary.utils.FetchBitmapTask) Uri(android.net.Uri) Bitmap(android.graphics.Bitmap) MediaInfo(com.google.android.gms.cast.MediaInfo) MediaMetadata(com.google.android.gms.cast.MediaMetadata) TransientNetworkDisconnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException)

Example 2 with MediaMetadataCompat

use of android.support.v4.media.MediaMetadataCompat in project android-UniversalMusicPlayer by googlesamples.

the class PlaybackControlsFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_playback_controls, container, false);
    mPlayPause = (ImageButton) rootView.findViewById(R.id.play_pause);
    mPlayPause.setEnabled(true);
    mPlayPause.setOnClickListener(mButtonListener);
    mTitle = (TextView) rootView.findViewById(R.id.title);
    mSubtitle = (TextView) rootView.findViewById(R.id.artist);
    mExtraInfo = (TextView) rootView.findViewById(R.id.extra_info);
    mAlbumArt = (ImageView) rootView.findViewById(R.id.album_art);
    rootView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), FullScreenPlayerActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            MediaControllerCompat controller = ((FragmentActivity) getActivity()).getSupportMediaController();
            MediaMetadataCompat metadata = controller.getMetadata();
            if (metadata != null) {
                intent.putExtra(MusicPlayerActivity.EXTRA_CURRENT_MEDIA_DESCRIPTION, metadata.getDescription());
            }
            startActivity(intent);
        }
    });
    return rootView;
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) Intent(android.content.Intent) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat) ImageView(android.widget.ImageView) TextView(android.widget.TextView) View(android.view.View)

Example 3 with MediaMetadataCompat

use of android.support.v4.media.MediaMetadataCompat in project android-UniversalMusicPlayer by googlesamples.

the class MusicProvider method updateMusicArt.

public synchronized void updateMusicArt(String musicId, Bitmap albumArt, Bitmap icon) {
    MediaMetadataCompat metadata = getMusic(musicId);
    metadata = new MediaMetadataCompat.Builder(metadata).putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, albumArt).putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, icon).build();
    MutableMediaMetadata mutableMetadata = mMusicListById.get(musicId);
    if (mutableMetadata == null) {
        throw new IllegalStateException("Unexpected error: Inconsistent data structures in " + "MusicProvider");
    }
    mutableMetadata.metadata = metadata;
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat)

Example 4 with MediaMetadataCompat

use of android.support.v4.media.MediaMetadataCompat in project android-UniversalMusicPlayer by googlesamples.

the class MusicProvider method retrieveMedia.

private synchronized void retrieveMedia() {
    try {
        if (mCurrentState == State.NON_INITIALIZED) {
            mCurrentState = State.INITIALIZING;
            Iterator<MediaMetadataCompat> tracks = mSource.iterator();
            while (tracks.hasNext()) {
                MediaMetadataCompat item = tracks.next();
                String musicId = item.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID);
                mMusicListById.put(musicId, new MutableMediaMetadata(musicId, item));
            }
            buildListsByGenre();
            mCurrentState = State.INITIALIZED;
        }
    } finally {
        if (mCurrentState != State.INITIALIZED) {
            // Something bad happened, so we reset state to NON_INITIALIZED to allow
            // retries (eg if the network connection is temporary unavailable)
            mCurrentState = State.NON_INITIALIZED;
        }
    }
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat)

Example 5 with MediaMetadataCompat

use of android.support.v4.media.MediaMetadataCompat in project android-UniversalMusicPlayer by googlesamples.

the class MusicProvider method createMediaItem.

private MediaBrowserCompat.MediaItem createMediaItem(MediaMetadataCompat metadata) {
    // Since mediaMetadata fields are immutable, we need to create a copy, so we
    // can set a hierarchy-aware mediaID. We will need to know the media hierarchy
    // when we get a onPlayFromMusicID call, so we can create the proper queue based
    // on where the music was selected from (by artist, by genre, random, etc)
    String genre = metadata.getString(MediaMetadataCompat.METADATA_KEY_GENRE);
    String hierarchyAwareMediaID = MediaIDHelper.createMediaID(metadata.getDescription().getMediaId(), MEDIA_ID_MUSICS_BY_GENRE, genre);
    MediaMetadataCompat copy = new MediaMetadataCompat.Builder(metadata).putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, hierarchyAwareMediaID).build();
    return new MediaBrowserCompat.MediaItem(copy.getDescription(), MediaBrowserCompat.MediaItem.FLAG_PLAYABLE);
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat)

Aggregations

MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)29 Test (org.junit.Test)13 MediaSessionCompat (android.support.v4.media.session.MediaSessionCompat)8 Bitmap (android.graphics.Bitmap)5 PlaybackStateCompat (android.support.v4.media.session.PlaybackStateCompat)4 Uri (android.net.Uri)3 Bundle (android.os.Bundle)3 MediaControllerCompat (android.support.v4.media.session.MediaControllerCompat)3 ArrayList (java.util.ArrayList)3 PendingIntent (android.app.PendingIntent)2 Context (android.content.Context)2 Intent (android.content.Intent)2 RemoteException (android.os.RemoteException)2 View (android.view.View)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 MediaInfo (com.google.android.gms.cast.MediaInfo)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 JSONObject (org.json.JSONObject)2 Configuration (android.content.res.Configuration)1