Example usage for android.support.v4.media MediaMetadataCompat getString

List of usage examples for android.support.v4.media MediaMetadataCompat getString

Introduction

In this page you can find the example usage for android.support.v4.media MediaMetadataCompat getString.

Prototype

public String getString(@TextKey String key) 

Source Link

Document

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Usage

From source file:com.example.android.uamp.playback.LocalPlayback.java

@Override
public void play(QueueItem item) {
    mPlayOnFocusGain = true;/*from w w w .  java2  s .c om*/
    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;
        relaxResources(false); // release everything except MediaPlayer
        MediaMetadataCompat track = mMusicProvider
                .getMusic(MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId()));

        //noinspection ResourceType
        String source = track.getString(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE);
        if (source != null) {
            source = source.replaceAll(" ", "%20"); // Escape spaces for URLs
        }

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

From source file:com.whitecloud.ron.musicplayer.LocalPlayback.java

@Override
public void play(QueueItem item) {
    mPlayOnFocusGain = true;//ww  w . ja  v  a 2  s. c o  m
    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;
        relaxResources(false); // release everything except MediaPlayer
        MediaMetadataCompat track = mMusicProvider
                .getMusic(MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId()));

        String source = MusicProvider
                .getTrackSource(track.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID));

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

From source file:com.bayapps.android.robophish.playback.LocalPlayback.java

public boolean playNext(QueueItem item) {
    MediaPlayer nextPlayer;// w ww .jav a  2  s.co  m
    if (mMediaPlayer == mMediaPlayerA) {
        nextPlayer = mMediaPlayerB;
    } else
        nextPlayer = mMediaPlayerA;

    String mediaId = item.getDescription().getMediaId();
    boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId);
    if (mediaHasChanged) {
        mNextMediaId = mediaId;
    }

    MediaMetadataCompat track = mMusicProvider
            .getMusic(MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId()));

    //noinspection ResourceType
    String source = track.getString(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE);

    nextPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    try {
        nextPlayer.setDataSource(source);
    } catch (IOException ex) {
        LogHelper.e(TAG, ex, "Exception playing song");
        if (mCallback != null) {
            mCallback.onError(ex.getMessage());
        }
    }

    // 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!
    nextPlayer.prepareAsync();

    mMediaPlayersSwapping = true;
    return true;
}

From source file:com.bayapps.android.robophish.playback.LocalPlayback.java

@Override
public void play(QueueItem item) {

    //we never call this if we're auto-queued due to gapless
    if (mMediaPlayersSwapping) {
        mMediaPlayersSwapping = false;/*  w  w w .  jav a2 s.com*/
    }

    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;
        relaxResources(false); // release everything except MediaPlayer
        MediaMetadataCompat track = mMusicProvider
                .getMusic(MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId()));

        //noinspection ResourceType
        String source = track.getString(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE);

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

From source file:com.example.android.mediabrowserservice.MusicService.java

private void updateMetadata() {
    if (!QueueHelper.isIndexPlayable(mCurrentIndexOnQueue, mPlayingQueue)) {
        LogHelper.e(TAG, "Can't retrieve current metadata.");
        updatePlaybackState(getResources().getString(R.string.error_no_metadata));
        return;//  ww w  . j a  va  2 s  .  co m
    }
    MediaSessionCompat.QueueItem queueItem = mPlayingQueue.get(mCurrentIndexOnQueue);
    String musicId = MediaIDHelper.extractMusicIDFromMediaID(queueItem.getDescription().getMediaId());
    MediaMetadataCompat track = mMusicProvider.getMusic(musicId);
    final String trackId = track.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID);
    if (!musicId.equals(trackId)) {
        IllegalStateException e = new IllegalStateException("track ID should match musicId.");
        LogHelper.e(TAG, "track ID should match musicId.", " musicId=", musicId, " trackId=", trackId,
                " mediaId from queueItem=", queueItem.getDescription().getMediaId(), " title from queueItem=",
                queueItem.getDescription().getTitle(), " mediaId from track=",
                track.getDescription().getMediaId(), " title from track=", track.getDescription().getTitle(),
                " source.hashcode from track=",
                track.getString(MusicProvider.CUSTOM_METADATA_TRACK_SOURCE).hashCode(), e);
        throw e;
    }
    LogHelper.d(TAG, "Updating metadata for MusicID= " + musicId);
    mSession.setMetadata(track);

    // Set the proper album artwork on the media session, so it can be shown in the
    // locked screen and in other places.
    if (track.getDescription().getIconBitmap() == null && track.getDescription().getIconUri() != null) {
        String albumUri = track.getDescription().getIconUri().toString();
        AlbumArtCache.getInstance().fetch(albumUri, new AlbumArtCache.FetchListener() {
            @Override
            public void onFetched(String artUrl, Bitmap bitmap, Bitmap icon) {
                MediaSessionCompat.QueueItem queueItem = mPlayingQueue.get(mCurrentIndexOnQueue);
                MediaMetadataCompat track = mMusicProvider.getMusic(trackId);
                track = new MediaMetadataCompat.Builder(track)

                        // set high resolution bitmap in METADATA_KEY_ALBUM_ART. This is used, for
                        // example, on the lockscreen background when the media session is active.
                        .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bitmap)

                        // set small version of the album art in the DISPLAY_ICON. This is used on
                        // the MediaDescription and thus it should be small to be serialized if
                        // necessary..
                        .putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, icon)

                        .build();

                mMusicProvider.updateMusic(trackId, track);

                // If we are still playing the same music
                String currentPlayingId = MediaIDHelper
                        .extractMusicIDFromMediaID(queueItem.getDescription().getMediaId());
                if (trackId.equals(currentPlayingId)) {
                    mSession.setMetadata(track);
                }
            }
        });
    }
}

From source file:com.example.android.supportv4.media.MediaBrowserServiceSupport.java

private void updateMetadata() {
    if (!QueueHelper.isIndexPlayable(mCurrentIndexOnQueue, mPlayingQueue)) {
        Log.e(TAG, "Can't retrieve current metadata.");
        updatePlaybackState(getResources().getString(R.string.error_no_metadata));
        return;//  w ww .ja v a 2s  .  c om
    }
    MediaSessionCompat.QueueItem queueItem = mPlayingQueue.get(mCurrentIndexOnQueue);
    String musicId = MediaIDHelper.extractMusicIDFromMediaID(queueItem.getDescription().getMediaId());
    MediaMetadataCompat track = mMusicProvider.getMusic(musicId);
    final String trackId = track.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID);
    if (!musicId.equals(trackId)) {
        IllegalStateException e = new IllegalStateException("track ID should match musicId.");
        Log.e(TAG,
                "track ID should match musicId. musicId=" + musicId + " trackId=" + trackId
                        + " mediaId from queueItem=" + queueItem.getDescription().getMediaId()
                        + " title from queueItem=" + queueItem.getDescription().getTitle()
                        + " mediaId from track=" + track.getDescription().getMediaId() + " title from track="
                        + track.getDescription().getTitle() + " source.hashcode from track="
                        + track.getString(MusicProvider.CUSTOM_METADATA_TRACK_SOURCE).hashCode(),
                e);
        throw e;
    }
    Log.d(TAG, "Updating metadata for MusicID= " + musicId);
    mSession.setMetadata(track);

    // Set the proper album artwork on the media session, so it can be shown in the
    // locked screen and in other places.
    if (track.getDescription().getIconBitmap() == null && track.getDescription().getIconUri() != null) {
        String albumUri = track.getDescription().getIconUri().toString();
        AlbumArtCache.getInstance().fetch(albumUri, new AlbumArtCache.FetchListener() {
            @Override
            public void onFetched(String artUrl, Bitmap bitmap, Bitmap icon) {
                MediaSessionCompat.QueueItem queueItem = mPlayingQueue.get(mCurrentIndexOnQueue);
                MediaMetadataCompat track = mMusicProvider.getMusic(trackId);
                track = new MediaMetadataCompat.Builder(track)
                        // set high resolution bitmap in METADATA_KEY_ALBUM_ART. This is used,
                        // for example, on the lockscreen background when the media session is
                        // active.
                        .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bitmap)
                        // set small version of the album art in the DISPLAY_ICON. This is used
                        // on the MediaDescription and thus it should be small to be serialized
                        // if necessary.
                        .putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, icon).build();

                mMusicProvider.updateMusic(trackId, track);

                // If we are still playing the same music
                String currentPlayingId = MediaIDHelper
                        .extractMusicIDFromMediaID(queueItem.getDescription().getMediaId());
                if (trackId.equals(currentPlayingId)) {
                    mSession.setMetadata(track);
                }
            }
        });
    }
}

From source file:com.murati.oszk.audiobook.model.MusicProvider.java

private MediaBrowserCompat.MediaItem createEbookItem(String ebook, Resources resources) {
    //TODO: canonize ebook mediaid and title conversion
    if (ebook.startsWith(MEDIA_ID_BY_EBOOK)) {
        ebook = getCategoryValueFromMediaID(ebook);
    }/* w w  w  . j a v  a  2 s .  co m*/

    MediaMetadataCompat metadata = getTracksByEbook(ebook).iterator().next();

    MediaDescriptionCompat description = new MediaDescriptionCompat.Builder()
            .setMediaId(createMediaID(null, MEDIA_ID_BY_EBOOK, ebook)).setTitle(ebook)
            .setSubtitle(metadata.getString(MediaMetadataCompat.METADATA_KEY_WRITER))
            //TODO: Fix Album art
            .setIconUri(Uri.parse(metadata.getString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI)))
            //TODO: fix default image
            // .setIconBitmap(BitmapHelper.convertDrawabletoUri(R.drawable.ic_navigate_books))
            .build();
    return new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_BROWSABLE);
}

From source file:com.example.android.mediabrowserservice.MusicService.java

private void setCustomAction(PlaybackStateCompat.Builder stateBuilder) {
    MediaMetadataCompat currentMusic = getCurrentPlayingMusic();
    if (currentMusic != null) {
        // Set appropriate "Favorite" icon on Custom action:
        String musicId = currentMusic.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID);
        int favoriteIcon = R.drawable.ic_star_off;
        if (mMusicProvider.isFavorite(musicId)) {
            favoriteIcon = R.drawable.ic_star_on;
        }//from  ww w  . j a  va  2 s  .c o  m
        LogHelper.d(TAG, "updatePlaybackState, setting Favorite custom action of music ", musicId,
                " current favorite=", mMusicProvider.isFavorite(musicId));
        stateBuilder.addCustomAction(CUSTOM_ACTION_THUMBS_UP, getString(R.string.favorite), favoriteIcon);
    }
}

From source file:rocks.stalin.android.app.playback.RemotePlayback.java

@Override
public void play(QueueItem item) {
    mPlayOnFocusGain = true;/*from   w w w .j  ava  2s  .  c  o  m*/
    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 && mediaPlayer != null) {
        configMediaPlayerState();
    } else {
        mState = PlaybackStateCompat.STATE_STOPPED;
        relaxResources(false); // release everything except MediaPlayer
        MediaMetadataCompat track = mMusicProvider
                .getMusic(MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId()));

        String source = null;
        Uri uriSource = null;
        //noinspection ResourceType
        if (track.containsKey(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE)) {
            source = track.getString(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE);
            if (source != null) {
                source = source.replaceAll(" ", "%20"); // Escape spaces for URLs
            }
        } else {
            uriSource = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    Long.parseLong(track.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID)));
        }

        try {
            createMediaPlayerIfNeeded();

            mState = PlaybackStateCompat.STATE_BUFFERING;

            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            if (uriSource != null) {
                mediaPlayer.setDataSource(mContext, uriSource);
            } else {
                mediaPlayer.setDataSource(source);
                // 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();
            }

            // 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!
            mediaPlayer.prepareAsync();

            if (mCallback != null) {
                mCallback.onPlaybackStatusChanged(mState);
            }

        } catch (IOException ex) {
            LogHelper.e(TAG, ex, "Exception playing song");
            if (mCallback != null) {
                mCallback.onError(ex.getMessage());
            }
        }
    }
}

From source file:com.example.android.supportv4.media.MediaBrowserServiceSupport.java

private void setCustomAction(PlaybackStateCompat.Builder stateBuilder) {
    MediaMetadataCompat currentMusic = getCurrentPlayingMusic();
    if (currentMusic != null) {
        // Set appropriate "Favorite" icon on Custom action:
        String musicId = currentMusic.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID);
        int favoriteIcon = R.drawable.ic_star_off;
        if (mMusicProvider.isFavorite(musicId)) {
            favoriteIcon = R.drawable.ic_star_on;
        }//from w  w  w . j a va 2 s  . c o m
        Log.d(TAG, "updatePlaybackState, setting Favorite custom action of music " + musicId
                + " current favorite=" + mMusicProvider.isFavorite(musicId));
        stateBuilder.addCustomAction(CUSTOM_ACTION_THUMBS_UP, getString(R.string.favorite), favoriteIcon);
    }
}