Search in sources :

Example 6 with QueueItem

use of android.support.v4.media.session.MediaSessionCompat.QueueItem in project Shuttle by timusus.

the class MediaSessionManager method updateMediaSession.

private void updateMediaSession(final String action) {
    int playState = playbackManager.isPlaying() ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED;
    long playbackActions = getMediaSessionActions();
    QueueItem currentQueueItem = queueManager.getCurrentQueueItem();
    PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder();
    builder.setActions(playbackActions);
    switch(queueManager.shuffleMode) {
        case QueueManager.ShuffleMode.OFF:
            builder.addCustomAction(new PlaybackStateCompat.CustomAction.Builder(SHUFFLE_ACTION, context.getString(R.string.btn_shuffle_on), R.drawable.ic_shuffle_off_circled).build());
            break;
        case QueueManager.ShuffleMode.ON:
            builder.addCustomAction(new PlaybackStateCompat.CustomAction.Builder(SHUFFLE_ACTION, context.getString(R.string.btn_shuffle_off), R.drawable.ic_shuffle_on_circled).build());
            break;
    }
    builder.setState(playState, playbackManager.getSeekPosition(), 1.0f);
    if (currentQueueItem != null) {
        builder.setActiveQueueItemId((long) currentQueueItem.hashCode());
    }
    PlaybackStateCompat playbackState = builder.build();
    if (action.equals(InternalIntents.PLAY_STATE_CHANGED) || action.equals(InternalIntents.POSITION_CHANGED) || action.equals(SHUFFLE_ACTION)) {
        mediaSession.setPlaybackState(playbackState);
    } else if (action.equals(InternalIntents.META_CHANGED) || action.equals(InternalIntents.QUEUE_CHANGED)) {
        if (currentQueueItem != null) {
            MediaMetadataCompat.Builder metaData = new MediaMetadataCompat.Builder().putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, String.valueOf(currentQueueItem.getSong().id)).putString(MediaMetadataCompat.METADATA_KEY_ARTIST, currentQueueItem.getSong().artistName).putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, currentQueueItem.getSong().albumArtistName).putString(MediaMetadataCompat.METADATA_KEY_ALBUM, currentQueueItem.getSong().albumName).putString(MediaMetadataCompat.METADATA_KEY_TITLE, currentQueueItem.getSong().name).putLong(MediaMetadataCompat.METADATA_KEY_DURATION, currentQueueItem.getSong().duration).putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, (long) (queueManager.queuePosition + 1)).putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, null).putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, (long) (queueManager.getCurrentPlaylist().size()));
            // If we're in car mode, don't wait for the artwork to load before setting session metadata.
            if (CarHelper.isCarUiMode(context)) {
                mediaSession.setMetadata(metaData.build());
            }
            mediaSession.setPlaybackState(playbackState);
            mediaSession.setQueue(QueueItemKt.toMediaSessionQueueItems(queueManager.getCurrentPlaylist()));
            mediaSession.setQueueTitle(context.getString(R.string.menu_queue));
            if (settingsManager.showLockscreenArtwork() || CarHelper.isCarUiMode(context)) {
                updateMediaSessionArtwork(metaData);
            } else {
                mediaSession.setMetadata(metaData.build());
            }
        }
    }
}
Also used : PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) QueueItem(com.simplecity.amp_library.ui.screens.queue.QueueItem) SuppressLint(android.annotation.SuppressLint)

Example 7 with QueueItem

use of android.support.v4.media.session.MediaSessionCompat.QueueItem in project android-UniversalMusicPlayer by googlesamples.

the class QueueHelper method isQueueItemPlaying.

/**
     * Determine if queue item matches the currently playing queue item
     *
     * @param context for retrieving the {@link MediaControllerCompat}
     * @param queueItem to compare to currently playing {@link MediaSessionCompat.QueueItem}
     * @return boolean indicating whether queue item matches currently playing queue item
     */
public static boolean isQueueItemPlaying(Context context, MediaSessionCompat.QueueItem queueItem) {
    // Queue item is considered to be playing or paused based on both the controller's
    // current media id and the controller's active queue item id
    MediaControllerCompat controller = ((FragmentActivity) context).getSupportMediaController();
    if (controller != null && controller.getPlaybackState() != null) {
        long currentPlayingQueueId = controller.getPlaybackState().getActiveQueueItemId();
        String currentPlayingMediaId = controller.getMetadata().getDescription().getMediaId();
        String itemMusicId = MediaIDHelper.extractMusicIDFromMediaID(queueItem.getDescription().getMediaId());
        if (queueItem.getQueueId() == currentPlayingQueueId && 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 8 with QueueItem

use of android.support.v4.media.session.MediaSessionCompat.QueueItem in project android-UniversalMusicPlayer by googlesamples.

the class LocalPlayback method play.

@Override
public void play(QueueItem item) {
    mPlayOnFocusGain = true;
    tryToGetAudioFocus();
    registerAudioNoisyReceiver();
    String mediaId = item.getDescription().getMediaId();
    boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId);
    if (mediaHasChanged) {
        mCurrentMediaId = mediaId;
    }
    if (mediaHasChanged || mExoPlayer == null) {
        // release everything except the player
        releaseResources(false);
        MediaMetadataCompat track = mMusicProvider.getMusic(MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId()));
        String source = track.getString(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE);
        if (source != null) {
            // Escape spaces for URLs
            source = source.replaceAll(" ", "%20");
        }
        if (mExoPlayer == null) {
            mExoPlayer = ExoPlayerFactory.newSimpleInstance(mContext, new DefaultTrackSelector(), new DefaultLoadControl());
            mExoPlayer.addListener(mEventListener);
        }
        // Android "O" makes much greater use of AudioAttributes, especially
        // with regards to AudioFocus. All of UAMP's tracks are music, but
        // if your content includes spoken word such as audiobooks or podcasts
        // then the content type should be set to CONTENT_TYPE_SPEECH for those
        // tracks.
        final AudioAttributes audioAttributes = new AudioAttributes.Builder().setContentType(CONTENT_TYPE_MUSIC).setUsage(USAGE_MEDIA).build();
        mExoPlayer.setAudioAttributes(audioAttributes);
        // Produces DataSource instances through which media data is loaded.
        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(mContext, Util.getUserAgent(mContext, "uamp"), null);
        // Produces Extractor instances for parsing the media data.
        ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
        // The MediaSource represents the media to be played.
        MediaSource mediaSource = new ExtractorMediaSource(Uri.parse(source), dataSourceFactory, extractorsFactory, null, null);
        // Prepares media to play (happens on background thread) and triggers
        // {@code onPlayerStateChanged} callback when the stream is ready to play.
        mExoPlayer.prepare(mediaSource);
        // 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();
    }
    configurePlayerState();
}
Also used : DefaultExtractorsFactory(com.google.android.exoplayer2.extractor.DefaultExtractorsFactory) MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) AudioAttributes(com.google.android.exoplayer2.audio.AudioAttributes) ExtractorMediaSource(com.google.android.exoplayer2.source.ExtractorMediaSource) DefaultLoadControl(com.google.android.exoplayer2.DefaultLoadControl) DataSource(com.google.android.exoplayer2.upstream.DataSource) ExtractorMediaSource(com.google.android.exoplayer2.source.ExtractorMediaSource) MediaSource(com.google.android.exoplayer2.source.MediaSource) ExtractorsFactory(com.google.android.exoplayer2.extractor.ExtractorsFactory) DefaultExtractorsFactory(com.google.android.exoplayer2.extractor.DefaultExtractorsFactory) DefaultDataSourceFactory(com.google.android.exoplayer2.upstream.DefaultDataSourceFactory) DefaultTrackSelector(com.google.android.exoplayer2.trackselection.DefaultTrackSelector)

Example 9 with QueueItem

use of android.support.v4.media.session.MediaSessionCompat.QueueItem in project react-native-track-player by react-native-kit.

the class Track method toQueueItem.

public QueueItem toQueueItem() {
    if (queueItem != null)
        return queueItem;
    MediaDescriptionCompat desc = new MediaDescriptionCompat.Builder().setMediaId(id).setMediaUri(url).setTitle(title).setSubtitle(artist).setDescription(description).setIconUri(artwork).build();
    queueItem = new QueueItem(desc, queueId);
    return queueItem;
}
Also used : QueueItem(android.support.v4.media.session.MediaSessionCompat.QueueItem) MediaDescriptionCompat(android.support.v4.media.MediaDescriptionCompat)

Example 10 with QueueItem

use of android.support.v4.media.session.MediaSessionCompat.QueueItem in project react-native-track-player by react-native-kit.

the class Metadata method updateQueue.

public void updateQueue(Playback playback) {
    List<QueueItem> items = new ArrayList<>();
    for (Track track : playback.getQueue()) {
        items.add(track.toQueueItem());
    }
    session.setQueue(items);
}
Also used : ArrayList(java.util.ArrayList) QueueItem(android.support.v4.media.session.MediaSessionCompat.QueueItem) Track(guichaguri.trackplayer.logic.track.Track)

Aggregations

MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)4 PlaybackStateCompat (android.support.v4.media.session.PlaybackStateCompat)3 MediaDescriptionCompat (android.support.v4.media.MediaDescriptionCompat)2 MediaControllerCompat (android.support.v4.media.session.MediaControllerCompat)2 MediaSessionCompat (android.support.v4.media.session.MediaSessionCompat)2 QueueItem (android.support.v4.media.session.MediaSessionCompat.QueueItem)2 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.Test)2 SuppressLint (android.annotation.SuppressLint)1 FragmentActivity (android.support.v4.app.FragmentActivity)1 MediaBrowserCompat (android.support.v4.media.MediaBrowserCompat)1 DefaultLoadControl (com.google.android.exoplayer2.DefaultLoadControl)1 AudioAttributes (com.google.android.exoplayer2.audio.AudioAttributes)1 DefaultExtractorsFactory (com.google.android.exoplayer2.extractor.DefaultExtractorsFactory)1 ExtractorsFactory (com.google.android.exoplayer2.extractor.ExtractorsFactory)1 ExtractorMediaSource (com.google.android.exoplayer2.source.ExtractorMediaSource)1 MediaSource (com.google.android.exoplayer2.source.MediaSource)1 DefaultTrackSelector (com.google.android.exoplayer2.trackselection.DefaultTrackSelector)1 DataSource (com.google.android.exoplayer2.upstream.DataSource)1