Java Code Examples for android.support.v4.media.MediaMetadataCompat#Builder

The following examples show how to use android.support.v4.media.MediaMetadataCompat#Builder . 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: RemoteControlClientLP.java    From Popeens-DSub with GNU General Public License v3.0 8 votes vote down vote up
public void setMetadata(MusicDirectory.Entry currentSong, Bitmap bitmap) {
	MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder();
	builder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, (currentSong == null) ? null : currentSong.getArtist())
			.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, (currentSong == null) ? null : currentSong.getAlbum())
			.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, (currentSong == null) ? null : currentSong.getArtist())
			.putString(MediaMetadataCompat.METADATA_KEY_TITLE, (currentSong) == null ? null : currentSong.getTitle())
			.putString(MediaMetadataCompat.METADATA_KEY_GENRE, (currentSong) == null ? null : currentSong.getGenre())
			.putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, (currentSong == null) ?
					0 : ((currentSong.getTrack() == null) ? 0 : currentSong.getTrack()))
			.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, (currentSong == null) ?
					0 : ((currentSong.getDuration() == null) ? 0 : (currentSong.getDuration() * 1000)));

	if(bitmap != null) {
		builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bitmap);
	}

	mediaSession.setMetadata(builder.build());
}
 
Example 2
Source File: MediaControllerService.java    From Noyze with Apache License 2.0 8 votes vote down vote up
@Produce
public Pair<MediaMetadataCompat, PlaybackStateCompat> produceEvent() {
    final MediaMetadataCompat metadata;
    if (null == mController.getMetadata()) {
        metadata = (new MediaMetadataCompat.Builder()).build();
    } else {
        // If the notification didn't provide an icon, add one!
        MediaMetadataCompat metadata1 = mController.getMetadata();
        String packageName = metadata1.getString(RemoteControlCompat.METADATA_KEY_PACKAGE);
        if (!metadata1.containsKey(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON) &&
                mLargeIconMap.containsKey(packageName)) {
            MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder(metadata1);
            builder.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, mLargeIconMap.get(packageName));
            metadata1 = builder.build();
        }
        metadata = metadata1;
    }
    return Pair.create(metadata, mController.getPlaybackState());
}
 
Example 3
Source File: MusicLibrary.java    From android-MediaBrowserService with Apache License 2.0 8 votes vote down vote up
public static MediaMetadataCompat getMetadata(Context context, String mediaId) {
    MediaMetadataCompat metadataWithoutBitmap = music.get(mediaId);
    Bitmap albumArt = getAlbumBitmap(context, mediaId);

    // Since MediaMetadataCompat is immutable, we need to create a copy to set the album art.
    // We don't set it initially on all items so that they don't take unnecessary memory.
    MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder();
    for (String key :
            new String[]{
                    MediaMetadataCompat.METADATA_KEY_MEDIA_ID,
                    MediaMetadataCompat.METADATA_KEY_ALBUM,
                    MediaMetadataCompat.METADATA_KEY_ARTIST,
                    MediaMetadataCompat.METADATA_KEY_GENRE,
                    MediaMetadataCompat.METADATA_KEY_TITLE
            }) {
        builder.putString(key, metadataWithoutBitmap.getString(key));
    }
    builder.putLong(
            MediaMetadataCompat.METADATA_KEY_DURATION,
            metadataWithoutBitmap.getLong(MediaMetadataCompat.METADATA_KEY_DURATION));
    builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, albumArt);
    return builder.build();
}
 
Example 4
Source File: PlaybackServiceStatusHelper.java    From odyssey with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Starts the cover fetching task. Make sure that mLastTrack is set correctly before.
 */
private void startCoverImageTask() {
    // Try to get old metadata to save image retrieval.
    MediaMetadataCompat oldData = mMediaSession.getController().getMetadata();
    MediaMetadataCompat.Builder metaDataBuilder;
    if (oldData == null) {
        metaDataBuilder = new MediaMetadataCompat.Builder();
    } else {
        metaDataBuilder = new MediaMetadataCompat.Builder(mMediaSession.getController().getMetadata());
    }
    // Reset metadata image in case covergenerator fails
    metaDataBuilder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, null);
    mMediaSession.setMetadata(metaDataBuilder.build());

    // Start the actual task based on the current track. (mLastTrack get sets before in updateStatus())
    mCoverLoader.getImage(mLastTrack, -1, -1);
}
 
Example 5
Source File: PlaybackServiceStatusHelper.java    From odyssey with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void receiveAlbumBitmap(Bitmap bm) {
    // Try to get old metadata to save image retrieval.
    MediaMetadataCompat.Builder metaDataBuilder;
    metaDataBuilder = new MediaMetadataCompat.Builder(mMediaSession.getController().getMetadata());

    if (mHideMediaOnLockscreen) {
        metaDataBuilder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, null);
    } else {
        metaDataBuilder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bm);
    }
    mMediaSession.setMetadata(metaDataBuilder.build());
    mNotificationManager.setNotificationImage(bm);
}
 
Example 6
Source File: RemoteControlLollipop.java    From Noyze with Apache License 2.0 5 votes vote down vote up
@Override
public void onMetadataChanged(MediaMetadata metadata) {
    if (null == metadata) return;
    MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder(
            MediaMetadataCompat.fromMediaMetadata(metadata));
    builder.putString(METADATA_KEY_PACKAGE, mController.getPackageName());
    metadataChanged(builder.build());
}
 
Example 7
Source File: RemoteControlCompat.java    From Noyze with Apache License 2.0 5 votes vote down vote up
protected void metadataChanged(MediaMetadataCompat metadata) {
    // Append a timestamp to know when this event occurred.
    MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder(metadata);
    builder.putLong(METADATA_KEY_TIMESTAMP, System.currentTimeMillis());
    mMetadata = builder.build();
    if (null != mListener && null != mMetadata && null != mPlaybackState)
        mListener.onMetadataChanged(metadata);
}
 
Example 8
Source File: TtsService.java    From android-app with GNU General Public License v3.0 5 votes vote down vote up
private void setMediaSessionMetaData() {
    if (mediaSession == null) return;
    Log.v(TAG, "setMediaSessionMetaData() title: " + metaDataTitle);

    MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder()
            .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, metaDataArtist)
            .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, metaDataAlbum)
            .putString(MediaMetadataCompat.METADATA_KEY_TITLE, metaDataTitle);

    if (metaDataAlbumArt != null) {
        builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, metaDataAlbumArt);
        builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, metaDataAlbumArt);
    }

    if (!TextUtils.isEmpty(metaDataAlbumArtEffectiveUri)) {
        builder.putString(MediaMetadataCompat.METADATA_KEY_ART_URI,
                metaDataAlbumArtEffectiveUri);
        builder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI,
                metaDataAlbumArtEffectiveUri);
    }

    if (textInterface != null) {
        builder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION,
                textInterface.getTotalDuration());
    }

    mediaSession.setMetadata(builder.build());
}
 
Example 9
Source File: ListPlaylistAdapter.java    From leanback-assistant with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a {@link MediaDescriptionCompat} into {@link MediaMetadataCompat}. Only a title,
 * description, and media id will be converted. A subclass should override this method if their
 * {@link #map(Object)} method stores more data in the mapped MediaDescriptionCompat.
 *
 * @param item to be converted.
 * @return A converted MediaMetadataCompact from MediaDescriptionCompat.
 */
@NonNull
public MediaMetadataCompat mapToMetadata(MediaDescriptionCompat item) {
    MediaMetadataCompat.Builder builder =
            new MediaMetadataCompat.Builder()
                    .putText(MediaMetadataCompat.METADATA_KEY_TITLE, item.getTitle())
                    .putText(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, item.getTitle())
                    .putText(
                            MediaMetadataCompat.METADATA_KEY_DISPLAY_DESCRIPTION,
                            item.getDescription())
                    .putBitmap(
                            MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, item.getIconBitmap())
                    .putText(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, item.getMediaId());

    if (item.getIconUri() != null) {
        builder.putString(
                MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON_URI,
                item.getIconUri().toString());
    }

    Bundle extras = item.getExtras();
    if (extras != null) {
        String backgroundArtUri = extras.getString(EXTRA_BACKGROUND_ART);
        if (backgroundArtUri != null) {
            builder.putString(MediaMetadataCompat.METADATA_KEY_ART_URI, backgroundArtUri);
        }
    }

    return builder.build();
}
 
Example 10
Source File: MusicControlModule.java    From react-native-music-control with MIT License 5 votes vote down vote up
@ReactMethod
synchronized public void resetNowPlaying() {
    if(!init) return;
    if(artworkThread != null && artworkThread.isAlive()) artworkThread.interrupt();
    artworkThread = null;

    md = new MediaMetadataCompat.Builder();

    if (notification != null) notification.hide();
    session.setActive(false);
}
 
Example 11
Source File: PlaybackService.java    From VCL-Android with Apache License 2.0 5 votes vote down vote up
protected void updateMetadata() {
    MediaWrapper media = getCurrentMedia();
    if (media == null || mMediaSession == null)
        return;
    String title = media.getNowPlaying();
    if (title == null)
        title = media.getTitle();
    Bitmap cover = AudioUtil.getCover(this, media, 512);
    MediaMetadataCompat.Builder bob = new MediaMetadataCompat.Builder();
    bob.putString(MediaMetadataCompat.METADATA_KEY_TITLE, title)
        .putString(MediaMetadataCompat.METADATA_KEY_GENRE, Util.getMediaGenre(this, media))
            .putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, media.getTrackNumber())
            .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, Util.getMediaReferenceArtist(this, media))
            .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, Util.getMediaAlbum(this, media))
            .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, media.getLength());
    if (cover != null && cover.getConfig() != null) //In case of format not supported
            bob.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, cover.copy(cover.getConfig(), false));
    mMediaSession.setMetadata(bob.build());

    //Send metadata to Pebble watch
    if (mPebbleEnabled) {
        final Intent i = new Intent("com.getpebble.action.NOW_PLAYING");
        i.putExtra("artist", Util.getMediaArtist(this, media));
        i.putExtra("album", Util.getMediaAlbum(this, media));
        i.putExtra("track", media.getTitle());
        sendBroadcast(i);
    }
}
 
Example 12
Source File: MediaSessionManager.java    From react-native-jw-media-player with MIT License 5 votes vote down vote up
@Override
public void onBitmapReady(Bitmap bitmap) {
	if (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_ART,
														   bitmap)
												.build());
	}
}
 
Example 13
Source File: MusicService.java    From Orin with GNU General Public License v3.0 4 votes vote down vote up
private void updateMediaSessionMetaData() {
    final Song song = getCurrentSong();

    if (song.id == -1) {
        mediaSession.setMetadata(null);
        return;
    }

    final MediaMetadataCompat.Builder metaData = new MediaMetadataCompat.Builder()
            .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, song.artistName)
            .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, song.artistName)
            .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, song.albumName)
            .putString(MediaMetadataCompat.METADATA_KEY_TITLE, song.title)
            .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, song.duration)
            .putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, getPosition() + 1)
            .putLong(MediaMetadataCompat.METADATA_KEY_YEAR, song.year)
            .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, null);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        metaData.putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, getPlayingQueue().size());
    }

    if (PreferenceUtil.getInstance(this).albumArtOnLockscreen()) {
        final Point screenSize = Util.getScreenSize(MusicService.this);
        final BitmapRequestBuilder<?, Bitmap> request = SongGlideRequest.Builder.from(Glide.with(MusicService.this), song)
                .checkIgnoreMediaStore(MusicService.this)
                .asBitmap().build();
        if (PreferenceUtil.getInstance(this).blurredAlbumArt()) {
            request.transform(new BlurTransformation.Builder(MusicService.this).build());
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                request.into(new SimpleTarget<Bitmap>(screenSize.x, screenSize.y) {
                    @Override
                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
                        super.onLoadFailed(e, errorDrawable);
                        mediaSession.setMetadata(metaData.build());
                    }

                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        metaData.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, copy(resource));
                        mediaSession.setMetadata(metaData.build());
                    }
                });
            }
        });
    } else {
        mediaSession.setMetadata(metaData.build());
    }
}
 
Example 14
Source File: MusicService.java    From MusicPlayer with GNU General Public License v3.0 4 votes vote down vote up
private void updateMediaSessionMetaData() {
    final Song song = getCurrentSong();

    if (song.id == -1) {
        mediaSession.setMetadata(null);
        return;
    }

    final MediaMetadataCompat.Builder metaData = new MediaMetadataCompat.Builder()
            .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, song.artistName)
            .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, song.artistName)
            .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, song.albumName)
            .putString(MediaMetadataCompat.METADATA_KEY_TITLE, song.title)
            .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, song.duration)
            .putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, getPosition() + 1)
            .putLong(MediaMetadataCompat.METADATA_KEY_YEAR, song.year)
            .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, null);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        metaData.putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, getPlayingQueue().size());
    }

    if (PreferenceUtil.getInstance(this).albumArtOnLockscreen()) {
        final Point screenSize = Util.getScreenSize(MusicService.this);
        final RequestBuilder<Bitmap> request = SongGlideRequest.Builder.from(Glide.with(MusicService.this), song)
                .checkIgnoreMediaStore(MusicService.this)
                .asBitmap().build();
        if (PreferenceUtil.getInstance(this).blurredAlbumArt()) {
           request.transform(new BlurTransformation.Builder(MusicService.this).build());
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                request.into(new SimpleTarget<Bitmap>(screenSize.x, screenSize.y) {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        metaData.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART,copy(resource));
                        mediaSession.setMetadata(metaData.build());
                    }

                    @Override
                    public void onLoadFailed(@Nullable Drawable errorDrawable) {
                        super.onLoadFailed(errorDrawable);
                        mediaSession.setMetadata(metaData.build());
                    }
                });
            }
        });
    } else {
        mediaSession.setMetadata(metaData.build());
    }
}
 
Example 15
Source File: Playback.java    From react-native-streaming-audio-player with MIT License 4 votes vote down vote up
public void playFromUri(Uri uri, Bundle bundle) {
    mPlayOnFocusGain = true;
    tryToGetAudioFocus();
    registerAudioNoisyReceiver();

    mState = PlaybackStateCompat.STATE_STOPPED;
    relaxResources(true);

    try {
        createMediaPlayerIfNeeded();

        mState = PlaybackStateCompat.STATE_BUFFERING;
        mCurrentPosition = 0;

        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setDataSource(uri.toString());

        // 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.onPlaybackStateChanged(mState);

            MediaMetadataCompat.Builder metaBuilder = new MediaMetadataCompat.Builder();
            metaBuilder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, bundle.getString(MediaMetadataCompat.METADATA_KEY_ARTIST));
            metaBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, bundle.getString(MediaMetadataCompat.METADATA_KEY_TITLE));
            metaBuilder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI, bundle.getString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI));
            mCallback.onMediaMetadataChanged(metaBuilder.build());
        }

    } catch (IOException ex) {
        Log.e(TAG, ex +  "Exception playing song");
        if (mCallback != null) {
            mCallback.onError(ex.getMessage());
        }
    }
}
 
Example 16
Source File: MusicService.java    From RetroMusicPlayer with GNU General Public License v3.0 4 votes vote down vote up
private void updateMediaSessionMetaData() {
    final Song song = getCurrentSong();

    if (song.id == -1) {
        mediaSession.setMetadata(null);
        return;
    }

    final MediaMetadataCompat.Builder metaData = new MediaMetadataCompat.Builder()
            .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, song.artistName)
            .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, song.artistName)
            .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, song.albumName)
            .putString(MediaMetadataCompat.METADATA_KEY_TITLE, song.title)
            .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, song.duration)
            .putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, getPosition() + 1)
            .putLong(MediaMetadataCompat.METADATA_KEY_YEAR, song.year)
            .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, null);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        metaData.putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, getPlayingQueue().size());
    }

    if (PreferenceUtil.getInstance(this).albumArtOnLockscreen()) {
        final Point screenSize = Util.getScreenSize(MusicService.this);
        final BitmapRequestBuilder<?, Bitmap> request = SongGlideRequest.Builder.from(Glide.with(MusicService.this), song)
                .checkIgnoreMediaStore(MusicService.this)
                .asBitmap().build();
        if (PreferenceUtil.getInstance(this).blurredAlbumArt()) {
            request.transform(new BlurTransformation.Builder(MusicService.this).build());
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                request.into(new SimpleTarget<Bitmap>(screenSize.x, screenSize.y) {
                    @Override
                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
                        super.onLoadFailed(e, errorDrawable);
                        mediaSession.setMetadata(metaData.build());
                    }

                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        metaData.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, copy(resource));
                        mediaSession.setMetadata(metaData.build());
                    }
                });
            }
        });
    } else {
        mediaSession.setMetadata(metaData.build());
    }
}
 
Example 17
Source File: MusicService.java    From VinylMusicPlayer with GNU General Public License v3.0 4 votes vote down vote up
private void updateMediaSessionMetaData() {
    final Song song = getCurrentSong();

    if (song.id == -1) {
        mediaSession.setMetadata(null);
        return;
    }

    final MediaMetadataCompat.Builder metaData = new MediaMetadataCompat.Builder()
            .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, song.artistName)
            .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, song.artistName)
            .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, song.albumName)
            .putString(MediaMetadataCompat.METADATA_KEY_TITLE, song.title)
            .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, song.duration)
            .putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, getPosition() + 1)
            .putLong(MediaMetadataCompat.METADATA_KEY_YEAR, song.year)
            .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, null);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        metaData.putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, getPlayingQueue().size());
    }

    if (PreferenceUtil.getInstance().albumArtOnLockscreen()) {
        final Point screenSize = Util.getScreenSize(MusicService.this);
        GlideRequest request = GlideApp.with(MusicService.this)
                .asBitmap()
                .load(VinylGlideExtension.getSongModel(song))
                .transition(VinylGlideExtension.getDefaultTransition())
                .songOptions(song);
        if (PreferenceUtil.getInstance().blurredAlbumArt()) {
            request.transform(new BlurTransformation.Builder(MusicService.this).build());
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                request.into(new VinylSimpleTarget<Bitmap>(screenSize.x, screenSize.y) {
                    @Override
                    public void onLoadFailed(@Nullable Drawable errorDrawable) {
                        super.onLoadFailed(errorDrawable);
                        mediaSession.setMetadata(metaData.build());
                    }

                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> glideAnimation) {
                        metaData.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, copy(resource));
                        mediaSession.setMetadata(metaData.build());
                    }
                });
            }
        });
    } else {
        mediaSession.setMetadata(metaData.build());
    }
}
 
Example 18
Source File: MediaEventResponder.java    From Noyze with Apache License 2.0 4 votes vote down vote up
public static Pair<MediaMetadataCompat, PlaybackStateCompat> respond(Context context, Intent intent) {
    if (null == context || null == intent) return null;
    String mAction = intent.getAction();
    Bundle extras = intent.getExtras();
    if (null == extras) extras = Bundle.EMPTY; // In case we've got nothing.
    MediaMetadataCompat.Builder mBuilder = null;
    PlaybackStateCompat.Builder pBuilder = null;

    int state = PlaybackStateCompat.STATE_NONE;
    long position = 0;

    LOGI("MediaEventResponder", mAction + ", extras=" + Utils.bundle2string(intent.getExtras()));
    if (mAction.startsWith("com.amazon.mp3")) {
        mBuilder = new MediaMetadataCompat.Builder();
        pBuilder = new PlaybackStateCompat.Builder();
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, extras.getString("com.amazon.mp3.artist"));
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, extras.getString("com.amazon.mp3.track"));
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, extras.getString("com.amazon.mp3.album"));
        state = (isPlaying(extras.getInt("com.amazon.mp3.playstate")) ?
                PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_STOPPED);
    } else if (mAction.startsWith("com.sonyericsson")) {
        mBuilder = new MediaMetadataCompat.Builder();
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, extras.getString("ARTIST_NAME"));
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, extras.getString("TRACK_NAME"));
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, extras.getString("ALBUM_NAME"));
    } else {
        // This is the default case, standard API check.
        mBuilder = new MediaMetadataCompat.Builder();
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, extras.getString("artist"));
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, extras.getString("album"));
        if (extras.containsKey("title"))
            mBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, extras.getString("title"));
        else if (extras.containsKey("track"))
            mBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, extras.getString("track"));
    }

    // Try the many ways to interpret the play state.
    if (null == pBuilder) {
        pBuilder = new PlaybackStateCompat.Builder();
        String extraKey = null;
        if (extras.containsKey("playstate"))
            extraKey = "playstate";
        else if (extras.containsKey("isPlaying"))
            extraKey = "isPlaying";
        else if (extras.containsKey("playing"))
            extraKey = "playing";
        else if (extras.containsKey("state"))
            extraKey = "state";

        // We still haven't given up, check the action.
        if (TextUtils.isEmpty(extraKey)) {
            boolean bState = false;
            if (mAction.endsWith("endofplayback"))
                bState = false;
            else if (mAction.endsWith("playbackcomplete"))
                bState = false;
            else if (mAction.endsWith("ACTION_PLAYBACK_PAUSE")) // SEMC Legacy
                bState = false;
            else if (mAction.endsWith("ACTION_PAUSED")) // SEMC
                bState = false;
            else if (mAction.endsWith("ACTION_TRACK_STARTED")) // SEMC Legacy
                bState = true;
            else if (mAction.endsWith("ACTION_PLAYBACK_PLAY")) // SEMC
                bState = true;
            state = (bState ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_STOPPED);
        } else {
            state = (extras.getBoolean(extraKey) ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_STOPPED);
        }
    }

    // Some extras we might want to use... might.
    if (extras.containsKey("duration"))
        mBuilder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, extras.getLong("duration"));
    if (extras.containsKey("position"))
        position = extras.getLong("position");

    // Attempt to figure out what app is playing music.
    pBuilder.setState(state, position, 1.0f);
    mBuilder.putString(RemoteControlCompat.METADATA_KEY_PACKAGE, packageForAction(mAction));

    // Workaround for Google Play Music... not the best :(
    if (extras.containsKey("previewPlayType") && extras.containsKey("supportsRating") &&
            extras.containsKey("currentContainerId"))
        mBuilder.putString(RemoteControlCompat.METADATA_KEY_PACKAGE, "com.google.android.music");

    // Workaround for Poweramp... should be pretty specific.
    if (extras.containsKey("com.maxmpz.audioplayer.source"))
        mBuilder.putString(RemoteControlCompat.METADATA_KEY_PACKAGE, "com.maxmpz.audioplayer");

    return Pair.create(mBuilder.build(), pBuilder.build());
}
 
Example 19
Source File: MediaEventResponder.java    From Noyze with Apache License 2.0 4 votes vote down vote up
public static Pair<MediaMetadataCompat, PlaybackStateCompat> respond(Context context, Intent intent) {
    if (null == context || null == intent) return null;
    String mAction = intent.getAction();
    Bundle extras = intent.getExtras();
    if (null == extras) extras = Bundle.EMPTY; // In case we've got nothing.
    MediaMetadataCompat.Builder mBuilder = null;
    PlaybackStateCompat.Builder pBuilder = null;

    int state = PlaybackStateCompat.STATE_NONE;
    long position = 0;

    LOGI("MediaEventResponder", mAction + ", extras=" + Utils.bundle2string(intent.getExtras()));
    if (mAction.startsWith("com.amazon.mp3")) {
        mBuilder = new MediaMetadataCompat.Builder();
        pBuilder = new PlaybackStateCompat.Builder();
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, extras.getString("com.amazon.mp3.artist"));
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, extras.getString("com.amazon.mp3.track"));
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, extras.getString("com.amazon.mp3.album"));
        state = (isPlaying(extras.getInt("com.amazon.mp3.playstate")) ?
                PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_STOPPED);
    } else if (mAction.startsWith("com.sonyericsson")) {
        mBuilder = new MediaMetadataCompat.Builder();
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, extras.getString("ARTIST_NAME"));
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, extras.getString("TRACK_NAME"));
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, extras.getString("ALBUM_NAME"));
    } else {
        // This is the default case, standard API check.
        mBuilder = new MediaMetadataCompat.Builder();
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, extras.getString("artist"));
        mBuilder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, extras.getString("album"));
        if (extras.containsKey("title"))
            mBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, extras.getString("title"));
        else if (extras.containsKey("track"))
            mBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, extras.getString("track"));
    }

    // Try the many ways to interpret the play state.
    if (null == pBuilder) {
        pBuilder = new PlaybackStateCompat.Builder();
        String extraKey = null;
        if (extras.containsKey("playstate"))
            extraKey = "playstate";
        else if (extras.containsKey("isPlaying"))
            extraKey = "isPlaying";
        else if (extras.containsKey("playing"))
            extraKey = "playing";
        else if (extras.containsKey("state"))
            extraKey = "state";

        // We still haven't given up, check the action.
        if (TextUtils.isEmpty(extraKey)) {
            boolean bState = false;
            if (mAction.endsWith("endofplayback"))
                bState = false;
            else if (mAction.endsWith("playbackcomplete"))
                bState = false;
            else if (mAction.endsWith("ACTION_PLAYBACK_PAUSE")) // SEMC Legacy
                bState = false;
            else if (mAction.endsWith("ACTION_PAUSED")) // SEMC
                bState = false;
            else if (mAction.endsWith("ACTION_TRACK_STARTED")) // SEMC Legacy
                bState = true;
            else if (mAction.endsWith("ACTION_PLAYBACK_PLAY")) // SEMC
                bState = true;
            state = (bState ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_STOPPED);
        } else {
            state = (extras.getBoolean(extraKey) ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_STOPPED);
        }
    }

    // Some extras we might want to use... might.
    if (extras.containsKey("duration"))
        mBuilder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, extras.getLong("duration"));
    if (extras.containsKey("position"))
        position = extras.getLong("position");

    // Attempt to figure out what app is playing music.
    pBuilder.setState(state, position, 1.0f);
    mBuilder.putString(RemoteControlCompat.METADATA_KEY_PACKAGE, packageForAction(mAction));

    // Workaround for Google Play Music... not the best :(
    if (extras.containsKey("previewPlayType") && extras.containsKey("supportsRating") &&
            extras.containsKey("currentContainerId"))
        mBuilder.putString(RemoteControlCompat.METADATA_KEY_PACKAGE, "com.google.android.music");

    // Workaround for Poweramp... should be pretty specific.
    if (extras.containsKey("com.maxmpz.audioplayer.source"))
        mBuilder.putString(RemoteControlCompat.METADATA_KEY_PACKAGE, "com.maxmpz.audioplayer");

    return Pair.create(mBuilder.build(), pBuilder.build());
}
 
Example 20
Source File: MusicService.java    From Phonograph with GNU General Public License v3.0 4 votes vote down vote up
private void updateMediaSessionMetaData() {
    final Song song = getCurrentSong();

    if (song.id == -1) {
        mediaSession.setMetadata(null);
        return;
    }

    final MediaMetadataCompat.Builder metaData = new MediaMetadataCompat.Builder()
            .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, song.artistName)
            .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, song.artistName)
            .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, song.albumName)
            .putString(MediaMetadataCompat.METADATA_KEY_TITLE, song.title)
            .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, song.duration)
            .putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, getPosition() + 1)
            .putLong(MediaMetadataCompat.METADATA_KEY_YEAR, song.year)
            .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, null);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        metaData.putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, getPlayingQueue().size());
    }

    if (PreferenceUtil.getInstance(this).albumArtOnLockscreen()) {
        final Point screenSize = Util.getScreenSize(MusicService.this);
        final BitmapRequestBuilder<?, Bitmap> request = SongGlideRequest.Builder.from(Glide.with(MusicService.this), song)
                .checkIgnoreMediaStore(MusicService.this)
                .asBitmap().build();
        if (PreferenceUtil.getInstance(this).blurredAlbumArt()) {
            request.transform(new BlurTransformation.Builder(MusicService.this).build());
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                request.into(new SimpleTarget<Bitmap>(screenSize.x, screenSize.y) {
                    @Override
                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
                        super.onLoadFailed(e, errorDrawable);
                        mediaSession.setMetadata(metaData.build());
                    }

                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        metaData.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, copy(resource));
                        mediaSession.setMetadata(metaData.build());
                    }
                });
            }
        });
    } else {
        mediaSession.setMetadata(metaData.build());
    }
}