Search in sources :

Example 1 with RatingCompat

use of android.support.v4.media.RatingCompat in project android-app by LISTEN-moe.

the class RadioService method initMediaSession.

private void initMediaSession() {
    mediaSession = new MediaSessionCompat(this, APP_PACKAGE_NAME, null, null);
    mediaSession.setRatingType(RatingCompat.RATING_HEART);
    mediaSession.setCallback(new MediaSessionCompat.Callback() {

        @Override
        public void onPlay() {
            play();
        }

        @Override
        public void onPause() {
            pause();
        }

        @Override
        public void onStop() {
            stop();
        }

        @Override
        public void onSkipToNext() {
        }

        @Override
        public void onSkipToPrevious() {
        }

        @Override
        public void onSeekTo(long pos) {
        }

        @Override
        public void onSetRating(RatingCompat rating) {
            favoriteCurrentSong();
        }

        @Override
        public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
            return handleIntent(mediaButtonEvent);
        }

        @Override
        public void onCustomAction(@NonNull String action, Bundle extras) {
            switch(action) {
                case TOGGLE_FAVORITE:
                    favoriteCurrentSong();
                    updateMediaSessionPlaybackState();
                    break;
                default:
                    Log.d(TAG, "Unsupported action: " + action);
                    break;
            }
        }

        @Override
        public void onPlayFromSearch(String query, Bundle extras) {
            // We don't support searching for specific things since it's just a radio stream
            // so just toggle playback
            togglePlayPause();
        }
    });
    mediaSession.setFlags(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSession.FLAG_HANDLES_MEDIA_BUTTONS);
    mediaSession.setActive(true);
}
Also used : RatingCompat(android.support.v4.media.RatingCompat) Bundle(android.os.Bundle) Intent(android.content.Intent) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat)

Example 2 with RatingCompat

use of android.support.v4.media.RatingCompat in project Douya by DreaminginCodeZH.

the class PlayMusicService method makeMediaMetadata.

private MediaMetadataCompat makeMediaMetadata(Music music, Music.Track track, int index) {
    MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder().putString(MediaMetadataCompat.METADATA_KEY_TITLE, track.title).putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, track.title);
    if (!music.artists.isEmpty()) {
        String artists = StringCompat.join(getString(R.string.item_information_delimiter_slash), music.getArtistNames());
        builder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, artists).putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, artists).putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE, artists);
    }
    if (track.duration > 0) {
        int duration = track.duration * 1000;
        builder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration);
    }
    builder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, music.title).putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_DESCRIPTION, music.title);
    String date = music.getReleaseDate();
    if (!TextUtils.isEmpty(date)) {
        builder.putString(MediaMetadataCompat.METADATA_KEY_DATE, date);
        if (date.length() > 4) {
            try {
                long year = Long.parseLong(date.substring(0, 4));
                builder.putLong(MediaMetadataCompat.METADATA_KEY_YEAR, year);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
    }
    String genre = CollectionUtils.firstOrNull(music.genres);
    if (!TextUtils.isEmpty(genre)) {
        builder.putString(MediaMetadataCompat.METADATA_KEY_GENRE, genre);
    }
    builder.putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, index).putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, music.tracks.size());
    String albumArtUri = music.cover.getLargeUrl();
    builder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI, albumArtUri).putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON_URI, albumArtUri);
    if (music.rating != null) {
        float starRating = music.rating.value / music.rating.max * 5;
        starRating = Math.max(0, Math.min(5, starRating));
        RatingCompat rating = RatingCompat.newStarRating(RatingCompat.RATING_5_STARS, starRating);
        builder.putRating(MediaMetadataCompat.METADATA_KEY_RATING, rating);
    }
    String mediaId = music.id + "#" + index;
    builder.putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, mediaId).putString(MediaMetadataCompat.METADATA_KEY_MEDIA_URI, track.previewUrl);
    return builder.build();
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) RatingCompat(android.support.v4.media.RatingCompat)

Aggregations

RatingCompat (android.support.v4.media.RatingCompat)2 Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)1 MediaSessionCompat (android.support.v4.media.session.MediaSessionCompat)1