android.media.MediaMetadata Java Examples

The following examples show how to use android.media.MediaMetadata. 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: TrackTest.java    From scroball with MIT License 6 votes vote down vote up
@Test
public void fromMediaMetadata_translatesShortDurationsToMilliseconds() {
  long threeMinutesSeconds = 4 * 60;
  long threeMinutesMs = 4 * 60 * 1000;
  MediaMetadata millisecondsInput =
      new MediaMetadata.Builder()
          .putLong(MediaMetadata.METADATA_KEY_DURATION, threeMinutesMs)
          .build();
  MediaMetadata secondsInput =
      new MediaMetadata.Builder()
          .putLong(MediaMetadata.METADATA_KEY_DURATION, threeMinutesSeconds)
          .build();

  Track output1 = Track.fromMediaMetadata(millisecondsInput);
  Track output2 = Track.fromMediaMetadata(secondsInput);

  assertThat(output1.duration()).hasValue(threeMinutesMs);
  assertThat(output2.duration()).hasValue(threeMinutesMs);
}
 
Example #2
Source File: PlayService.java    From music_player with Open Software License 3.0 6 votes vote down vote up
@Override
        public boolean onException(Exception e, Object model, Target target, boolean isFirstResource) {
            BitmapDrawable resourceDrawable = (BitmapDrawable) getDrawable(R.drawable.default_album);
            Bitmap resource = resourceDrawable.getBitmap();
            albumNow = resource;
            mediaSession.setMetadata(new MediaMetadata.Builder()
                    .putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, albumNow)
                    .putString(MediaMetadata.METADATA_KEY_ARTIST, singerNow)
                    .putString(MediaMetadata.METADATA_KEY_TITLE, titleNow)
                    .build());
//            PlaybackState.Builder stateBuilder = new PlaybackState.Builder();
//            stateBuilder.setState(PlaybackState.STATE_PLAYING, mediaPlayer.getCurrentPosition(), 1.0f);
//            mediaSession.setPlaybackState(stateBuilder.build());
            buildNotification(MyConstant.playing);
            return false;
        }
 
Example #3
Source File: MusicPlaybackService.java    From tv-samples with Apache License 2.0 6 votes vote down vote up
private void updateMediaSessionMetaData() {
    if (mCurrentMediaItem == null) {
        throw new IllegalArgumentException(
                "mCurrentMediaItem is null in updateMediaSessionMetaData!");
    }
    MediaMetadataCompat.Builder metaDataBuilder = new MediaMetadataCompat.Builder();
    if (mCurrentMediaItem.getMediaTitle() != null) {
        metaDataBuilder.putString(MediaMetadata.METADATA_KEY_TITLE,
                mCurrentMediaItem.getMediaTitle());
    }
    if (mCurrentMediaItem.getMediaAlbumName() != null) {
        metaDataBuilder.putString(MediaMetadata.METADATA_KEY_ALBUM,
                mCurrentMediaItem.getMediaAlbumName());
    }
    if (mCurrentMediaItem.getMediaArtistName() != null) {
        metaDataBuilder.putString(MediaMetadata.METADATA_KEY_ARTIST,
                mCurrentMediaItem.getMediaArtistName());
    }
    if (mCurrentMediaItem.getMediaAlbumArtResId() != 0) {
        Bitmap albumArtBitmap = BitmapFactory.decodeResource(getResources(),
                mCurrentMediaItem.getMediaAlbumArtResId());
        metaDataBuilder.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, albumArtBitmap);
    }
    mMediaSession.setMetadata(metaDataBuilder.build());
}
 
Example #4
Source File: MusicLibrary.java    From android-music-player with Apache License 2.0 6 votes vote down vote up
private static void createMediaMetadata(String mediaId, String title, String artist,
            String album, String genre, long duration, int musicResId, int albumArtResId,
            String albumArtResName) {
    music.put(mediaId,
            new MediaMetadata.Builder()
            .putString(MediaMetadata.METADATA_KEY_MEDIA_ID, mediaId)
            .putString(MediaMetadata.METADATA_KEY_ALBUM, album)
            .putString(MediaMetadata.METADATA_KEY_ARTIST, artist)
            .putLong(MediaMetadata.METADATA_KEY_DURATION, duration * 1000)
            .putString(MediaMetadata.METADATA_KEY_GENRE, genre)
            .putString(MediaMetadata.METADATA_KEY_ALBUM_ART_URI, getAlbumArtUri(albumArtResName))
            .putString(MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI, getAlbumArtUri(albumArtResName))
            .putString(MediaMetadata.METADATA_KEY_TITLE, title)
            .build());
    albumRes.put(mediaId, albumArtResId);
    musicRes.put(mediaId, musicResId);
}
 
Example #5
Source File: MediaService.java    From AsteroidOSSync with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onMetadataChanged(MediaMetadata metadata) {
    super.onMetadataChanged(metadata);

    if (metadata != null) {
        mDevice.write(mediaArtistCharac,
                getTextAsBytes(metadata, MediaMetadata.METADATA_KEY_ARTIST),
                MediaService.this);

        mDevice.write(mediaAlbumCharac,
                getTextAsBytes(metadata, MediaMetadata.METADATA_KEY_ALBUM),
                MediaService.this);

        mDevice.write(mediaTitleCharac,
                getTextAsBytes(metadata, MediaMetadata.METADATA_KEY_TITLE),
                MediaService.this);
    }
}
 
Example #6
Source File: TrackTest.java    From scroball with MIT License 6 votes vote down vote up
@Test
public void fromMediaMetadata() {
  MediaMetadata input =
      new MediaMetadata.Builder()
          .putString(MediaMetadata.METADATA_KEY_TITLE, "Title")
          .putString(MediaMetadata.METADATA_KEY_ARTIST, "Artist")
          .putString(MediaMetadata.METADATA_KEY_ALBUM, "Album")
          .putString(MediaMetadata.METADATA_KEY_ALBUM_ARTIST, "Album Artist")
          .putBitmap(MediaMetadata.METADATA_KEY_ART, EMPTY_BITMAP)
          .putLong(MediaMetadata.METADATA_KEY_DURATION, 10000)
          .build();

  Track output = Track.fromMediaMetadata(input);

  assertThat(output.track()).isEqualTo("Title");
  assertThat(output.artist()).isEqualTo("Artist");
  assertThat(output.albumArtist()).hasValue("Album Artist");
  assertThat(output.art()).isPresent();
  assertThat(output.duration()).hasValue((long) 10000);
}
 
Example #7
Source File: TrackTest.java    From scroball with MIT License 6 votes vote down vote up
@Test
public void fromMediaMetadata_generatesInvalidTrackWhenNoArtistOrTitle() {
  MediaMetadata empty = new MediaMetadata.Builder().build();
  MediaMetadata noTitle =
      new MediaMetadata.Builder().putString(MediaMetadata.METADATA_KEY_ARTIST, "Artist").build();
  MediaMetadata noArtist =
      new MediaMetadata.Builder().putString(MediaMetadata.METADATA_KEY_TITLE, "Title").build();

  Track output1 = Track.fromMediaMetadata(empty);
  Track output2 = Track.fromMediaMetadata(noTitle);
  Track output3 = Track.fromMediaMetadata(noArtist);

  assertThat(output1.isValid()).isFalse();
  assertThat(output2.isValid()).isFalse();
  assertThat(output3.isValid()).isFalse();
}
 
Example #8
Source File: MusicPlaybackService.java    From leanback-showcase with Apache License 2.0 6 votes vote down vote up
private void updateMediaSessionMetaData() {
    if (mCurrentMediaItem == null) {
        throw new IllegalArgumentException(
                "mCurrentMediaItem is null in updateMediaSessionMetaData!");
    }
    MediaMetadataCompat.Builder metaDataBuilder = new MediaMetadataCompat.Builder();
    if (mCurrentMediaItem.getMediaTitle() != null) {
        metaDataBuilder.putString(MediaMetadata.METADATA_KEY_TITLE,
                mCurrentMediaItem.getMediaTitle());
    }
    if (mCurrentMediaItem.getMediaAlbumName() != null) {
        metaDataBuilder.putString(MediaMetadata.METADATA_KEY_ALBUM,
                mCurrentMediaItem.getMediaAlbumName());
    }
    if (mCurrentMediaItem.getMediaArtistName() != null) {
        metaDataBuilder.putString(MediaMetadata.METADATA_KEY_ARTIST,
                mCurrentMediaItem.getMediaArtistName());
    }
    if (mCurrentMediaItem.getMediaAlbumArtResId() != 0) {
        Bitmap albumArtBitmap = BitmapFactory.decodeResource(getResources(),
                mCurrentMediaItem.getMediaAlbumArtResId());
        metaDataBuilder.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, albumArtBitmap);
    }
    mMediaSession.setMetadata(metaDataBuilder.build());
}
 
Example #9
Source File: MusicLibrary.java    From android-music-player with Apache License 2.0 6 votes vote down vote up
public static MediaMetadata getMetadata(Context ctx, String mediaId) {
    MediaMetadata metadataWithoutBitmap = music.get(mediaId);
    Bitmap albumArt = getAlbumBitmap(ctx, mediaId);

    // Since MediaMetadata 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
    MediaMetadata.Builder builder = new MediaMetadata.Builder();
    for (String key: new String[]{MediaMetadata.METADATA_KEY_MEDIA_ID,
            MediaMetadata.METADATA_KEY_ALBUM, MediaMetadata.METADATA_KEY_ARTIST,
            MediaMetadata.METADATA_KEY_GENRE, MediaMetadata.METADATA_KEY_TITLE}) {
        builder.putString(key, metadataWithoutBitmap.getString(key));
    }
    builder.putLong(MediaMetadata.METADATA_KEY_DURATION,
            metadataWithoutBitmap.getLong(MediaMetadata.METADATA_KEY_DURATION));
    builder.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, albumArt);
    return builder.build();
}
 
Example #10
Source File: MusicLibrary.java    From android-music-player with Apache License 2.0 6 votes vote down vote up
private static void createMediaMetadata(String mediaId, String title, String artist,
            String album, String genre, long duration, int musicResId, int albumArtResId,
            String albumArtResName) {
    music.put(mediaId,
            new MediaMetadata.Builder()
            .putString(MediaMetadata.METADATA_KEY_MEDIA_ID, mediaId)
            .putString(MediaMetadata.METADATA_KEY_ALBUM, album)
            .putString(MediaMetadata.METADATA_KEY_ARTIST, artist)
            .putLong(MediaMetadata.METADATA_KEY_DURATION, duration * 1000)
            .putString(MediaMetadata.METADATA_KEY_GENRE, genre)
            .putString(MediaMetadata.METADATA_KEY_ALBUM_ART_URI, getAlbumArtUri(albumArtResName))
            .putString(MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI, getAlbumArtUri(albumArtResName))
            .putString(MediaMetadata.METADATA_KEY_TITLE, title)
            .build());
    albumRes.put(mediaId, albumArtResId);
    musicRes.put(mediaId, musicResId);
}
 
Example #11
Source File: MusicLibrary.java    From android-music-player with Apache License 2.0 6 votes vote down vote up
public static MediaMetadata getMetadata(Context ctx, String mediaId) {
    MediaMetadata metadataWithoutBitmap = music.get(mediaId);
    Bitmap albumArt = getAlbumBitmap(ctx, mediaId);

    // Since MediaMetadata 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
    MediaMetadata.Builder builder = new MediaMetadata.Builder();
    for (String key: new String[]{MediaMetadata.METADATA_KEY_MEDIA_ID,
            MediaMetadata.METADATA_KEY_ALBUM, MediaMetadata.METADATA_KEY_ARTIST,
            MediaMetadata.METADATA_KEY_GENRE, MediaMetadata.METADATA_KEY_TITLE}) {
        builder.putString(key, metadataWithoutBitmap.getString(key));
    }
    builder.putLong(MediaMetadata.METADATA_KEY_DURATION,
            metadataWithoutBitmap.getLong(MediaMetadata.METADATA_KEY_DURATION));
    builder.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, albumArt);
    return builder.build();
}
 
Example #12
Source File: AvrcpService5.java    From Botifier with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void showNotify(String artist, String album, String title, int tracknr) {
       PlaybackState state = new PlaybackState.Builder()
               .setActions(
                       PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PLAY_PAUSE |
                               PlaybackState.ACTION_PLAY_FROM_MEDIA_ID | PlaybackState.ACTION_PAUSE |
                               PlaybackState.ACTION_SKIP_TO_NEXT | PlaybackState.ACTION_SKIP_TO_PREVIOUS)
               .setState(PlaybackState.STATE_PLAYING, 1, 1, SystemClock.elapsedRealtime())
               .build();
       MediaMetadata metadata = new MediaMetadata.Builder()
               .putString(MediaMetadata.METADATA_KEY_TITLE, title)
               .putString(MediaMetadata.METADATA_KEY_ARTIST, artist)
               .putString(MediaMetadata.METADATA_KEY_ALBUM_ARTIST, artist)
               .putString(MediaMetadata.METADATA_KEY_ALBUM, album)
               .putLong(MediaMetadata.METADATA_KEY_NUM_TRACKS, tracknr)
               .putLong(MediaMetadata.METADATA_KEY_DURATION, 10)
               .build();
       mMediaSession.setActive(true);
       mMediaSession.setMetadata(metadata);
       mMediaSession.setPlaybackState(state);
	int timeout = getTimeout();
	if (timeout != 0) {
		mHandler.removeMessages(HANDLER_WHAT_CLEAR);
		mHandler.sendEmptyMessageDelayed(HANDLER_WHAT_CLEAR, timeout * 1000);
	}
}
 
Example #13
Source File: MusicLibrary.java    From io2015-codelabs with Apache License 2.0 6 votes vote down vote up
public static MediaMetadata getMetadata(Context ctx, String mediaId) {
    MediaMetadata metadataWithoutBitmap = music.get(mediaId);
    Bitmap albumArt = getAlbumBitmap(ctx, mediaId);

    // Since MediaMetadata 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
    MediaMetadata.Builder builder = new MediaMetadata.Builder();
    for (String key: new String[]{MediaMetadata.METADATA_KEY_MEDIA_ID,
            MediaMetadata.METADATA_KEY_ALBUM, MediaMetadata.METADATA_KEY_ARTIST,
            MediaMetadata.METADATA_KEY_GENRE, MediaMetadata.METADATA_KEY_TITLE}) {
        builder.putString(key, metadataWithoutBitmap.getString(key));
    }
    builder.putLong(MediaMetadata.METADATA_KEY_DURATION,
            metadataWithoutBitmap.getLong(MediaMetadata.METADATA_KEY_DURATION));
    builder.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, albumArt);
    return builder.build();
}
 
Example #14
Source File: MusicLibrary.java    From io2015-codelabs with Apache License 2.0 6 votes vote down vote up
private static void createMediaMetadata(String mediaId, String title, String artist,
            String album, String genre, long duration, int musicResId, int albumArtResId,
            String albumArtResName) {
    music.put(mediaId,
            new MediaMetadata.Builder()
            .putString(MediaMetadata.METADATA_KEY_MEDIA_ID, mediaId)
            .putString(MediaMetadata.METADATA_KEY_ALBUM, album)
            .putString(MediaMetadata.METADATA_KEY_ARTIST, artist)
            .putLong(MediaMetadata.METADATA_KEY_DURATION, duration * 1000)
            .putString(MediaMetadata.METADATA_KEY_GENRE, genre)
            .putString(MediaMetadata.METADATA_KEY_ALBUM_ART_URI, getAlbumArtUri(albumArtResName))
            .putString(MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI, getAlbumArtUri(albumArtResName))
            .putString(MediaMetadata.METADATA_KEY_TITLE, title)
            .build());
    albumRes.put(mediaId, albumArtResId);
    musicRes.put(mediaId, musicResId);
}
 
Example #15
Source File: MusicLibrary.java    From io2015-codelabs with Apache License 2.0 6 votes vote down vote up
public static MediaMetadata getMetadata(Context ctx, String mediaId) {
    MediaMetadata metadataWithoutBitmap = music.get(mediaId);
    Bitmap albumArt = getAlbumBitmap(ctx, mediaId);

    // Since MediaMetadata 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
    MediaMetadata.Builder builder = new MediaMetadata.Builder();
    for (String key: new String[]{MediaMetadata.METADATA_KEY_MEDIA_ID,
            MediaMetadata.METADATA_KEY_ALBUM, MediaMetadata.METADATA_KEY_ARTIST,
            MediaMetadata.METADATA_KEY_GENRE, MediaMetadata.METADATA_KEY_TITLE}) {
        builder.putString(key, metadataWithoutBitmap.getString(key));
    }
    builder.putLong(MediaMetadata.METADATA_KEY_DURATION,
            metadataWithoutBitmap.getLong(MediaMetadata.METADATA_KEY_DURATION));
    builder.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, albumArt);
    return builder.build();
}
 
Example #16
Source File: MusicLibrary.java    From io2015-codelabs with Apache License 2.0 6 votes vote down vote up
private static void createMediaMetadata(String mediaId, String title, String artist,
            String album, String genre, long duration, int musicResId, int albumArtResId,
            String albumArtResName) {
    music.put(mediaId,
            new MediaMetadata.Builder()
            .putString(MediaMetadata.METADATA_KEY_MEDIA_ID, mediaId)
            .putString(MediaMetadata.METADATA_KEY_ALBUM, album)
            .putString(MediaMetadata.METADATA_KEY_ARTIST, artist)
            .putLong(MediaMetadata.METADATA_KEY_DURATION, duration * 1000)
            .putString(MediaMetadata.METADATA_KEY_GENRE, genre)
            .putString(MediaMetadata.METADATA_KEY_ALBUM_ART_URI, getAlbumArtUri(albumArtResName))
            .putString(MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI, getAlbumArtUri(albumArtResName))
            .putString(MediaMetadata.METADATA_KEY_TITLE, title)
            .build());
    albumRes.put(mediaId, albumArtResId);
    musicRes.put(mediaId, musicResId);
}
 
Example #17
Source File: MediaController2Lollipop.java    From AcDisplay with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void run(@NonNull MediaController2 mc) {
    final MediaController2Lollipop mcl = (MediaController2Lollipop) mc;
    final MediaController source = mcl.mMediaController;

    if (source != null && mToken.equals(source.getSessionToken())) {
        long now = SystemClock.elapsedRealtime();

        final MediaMetadata metadata = source.getMetadata();
        final PlaybackState playbackState = source.getPlaybackState();

        long delta = SystemClock.elapsedRealtime() - now;
        Log.i(TAG, "Got the new metadata & playback state in " + delta + " millis. "
                + "The media controller is " + source.getPackageName());

        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mcl.updateMetadata(metadata);
                mcl.updatePlaybackState(playbackState);
            }
        });
    }
}
 
Example #18
Source File: MusicLibrary.java    From io2015-codelabs with Apache License 2.0 5 votes vote down vote up
public static List<MediaBrowser.MediaItem> getMediaItems() {
    List<MediaBrowser.MediaItem> result = new ArrayList<>();
    for (MediaMetadata metadata: music.values()) {
        result.add(new MediaBrowser.MediaItem(metadata.getDescription(),
                MediaBrowser.MediaItem.FLAG_PLAYABLE));
    }
    return result;
}
 
Example #19
Source File: MusicService.java    From io2015-codelabs with Apache License 2.0 5 votes vote down vote up
@Override
public void onPlayFromMediaId(String mediaId, Bundle extras) {
    mSession.setActive(true);
    MediaMetadata metadata = MusicLibrary.getMetadata(MusicService.this, mediaId);
    mSession.setMetadata(metadata);
    mPlayback.play(metadata);
}
 
Example #20
Source File: MusicPlayerActivity.java    From io2015-codelabs with Apache License 2.0 5 votes vote down vote up
private void updateMetadata(MediaMetadata metadata) {
    mCurrentMetadata = metadata;
    mTitle.setText(metadata == null ? "" : metadata.getDescription().getTitle());
    mSubtitle.setText(metadata == null ? "" : metadata.getDescription().getSubtitle());
    mAlbumArt.setImageBitmap(metadata == null ? null : MusicLibrary.getAlbumBitmap(this,
            metadata.getDescription().getMediaId()));
    mBrowserAdapter.notifyDataSetChanged();
}
 
Example #21
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 #22
Source File: MusicBrowserService.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private void handlePlayRequest() {
    delayedStopHandler.removeCallbacksAndMessages(null);
    if (!serviceStarted) {
        try {
            startService(new Intent(getApplicationContext(), MusicBrowserService.class));
        } catch (Throwable e) {
            FileLog.e(e);
        }
        serviceStarted = true;
    }

    if (!mediaSession.isActive()) {
        mediaSession.setActive(true);
    }

    MessageObject messageObject = MediaController.getInstance().getPlayingMessageObject();
    if (messageObject == null) {
        return;
    }
    MediaMetadata.Builder builder = new MediaMetadata.Builder();
    builder.putLong(MediaMetadata.METADATA_KEY_DURATION, messageObject.getDuration() * 1000);
    builder.putString(MediaMetadata.METADATA_KEY_ARTIST, messageObject.getMusicAuthor());
    builder.putString(MediaMetadata.METADATA_KEY_TITLE, messageObject.getMusicTitle());
    AudioInfo audioInfo = MediaController.getInstance().getAudioInfo();
    if (audioInfo != null) {
        Bitmap bitmap = audioInfo.getCover();
        if (bitmap != null) {
            builder.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, bitmap);
        }
    }
    mediaSession.setMetadata(builder.build());
}
 
Example #23
Source File: ClementineMediaSessionV21.java    From Android-Remote with GNU General Public License v3.0 5 votes vote down vote up
private void updateMetaData() {
    MySong song = App.Clementine.getCurrentSong();
    if (song != null && song.getArt() != null) {
        MediaMetadata.Builder builder = new MediaMetadata.Builder();
        builder.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, song.getArt());
        builder.putString(MediaMetadata.METADATA_KEY_ALBUM, song.getAlbum());
        builder.putString(MediaMetadata.METADATA_KEY_TITLE, song.getTitle());
        builder.putString(MediaMetadata.METADATA_KEY_ALBUM_ARTIST, song.getAlbumartist());
        builder.putString(MediaMetadata.METADATA_KEY_ARTIST, song.getArtist());

        mMediaSession.setMetadata(builder.build());
    }
}
 
Example #24
Source File: MusicService.java    From android-music-player with Apache License 2.0 5 votes vote down vote up
@Override
public void onPlayFromMediaId(String mediaId, Bundle extras) {
    mSession.setActive(true);
    MediaMetadata metadata = MusicLibrary.getMetadata(MusicService.this, mediaId);
    mSession.setMetadata(metadata);
    mPlayback.play(metadata);
}
 
Example #25
Source File: MusicPlayerActivity.java    From android-music-player with Apache License 2.0 5 votes vote down vote up
private void updateMetadata(MediaMetadata metadata) {
    mCurrentMetadata = metadata;
    mTitle.setText(metadata == null ? "" : metadata.getDescription().getTitle());
    mSubtitle.setText(metadata == null ? "" : metadata.getDescription().getSubtitle());
    mAlbumArt.setImageBitmap(metadata == null ? null : MusicLibrary.getAlbumBitmap(this,
            metadata.getDescription().getMediaId()));
    mBrowserAdapter.notifyDataSetChanged();
}
 
Example #26
Source File: MediaSessionRecord.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private PlaybackState getStateWithUpdatedPosition() {
    PlaybackState state;
    long duration = -1;
    synchronized (mLock) {
        state = mPlaybackState;
        if (mMetadata != null && mMetadata.containsKey(MediaMetadata.METADATA_KEY_DURATION)) {
            duration = mMetadata.getLong(MediaMetadata.METADATA_KEY_DURATION);
        }
    }
    PlaybackState result = null;
    if (state != null) {
        if (state.getState() == PlaybackState.STATE_PLAYING
                || state.getState() == PlaybackState.STATE_FAST_FORWARDING
                || state.getState() == PlaybackState.STATE_REWINDING) {
            long updateTime = state.getLastPositionUpdateTime();
            long currentTime = SystemClock.elapsedRealtime();
            if (updateTime > 0) {
                long position = (long) (state.getPlaybackSpeed()
                        * (currentTime - updateTime)) + state.getPosition();
                if (duration >= 0 && position > duration) {
                    position = duration;
                } else if (position < 0) {
                    position = 0;
                }
                PlaybackState.Builder builder = new PlaybackState.Builder(state);
                builder.setState(state.getState(), position, state.getPlaybackSpeed(),
                        currentTime);
                result = builder.build();
            }
        }
    }
    return result == null ? state : result;
}
 
Example #27
Source File: MusicLibrary.java    From android-music-player with Apache License 2.0 5 votes vote down vote up
public static List<MediaBrowser.MediaItem> getMediaItems() {
    List<MediaBrowser.MediaItem> result = new ArrayList<>();
    for (MediaMetadata metadata: music.values()) {
        result.add(new MediaBrowser.MediaItem(metadata.getDescription(),
                MediaBrowser.MediaItem.FLAG_PLAYABLE));
    }
    return result;
}
 
Example #28
Source File: MusicPlayerActivity.java    From android-music-player with Apache License 2.0 5 votes vote down vote up
private void updateMetadata(MediaMetadata metadata) {
    mCurrentMetadata = metadata;
    mTitle.setText(metadata == null ? "" : metadata.getDescription().getTitle());
    mSubtitle.setText(metadata == null ? "" : metadata.getDescription().getSubtitle());
    mAlbumArt.setImageBitmap(metadata == null ? null : MusicLibrary.getAlbumBitmap(this,
            metadata.getDescription().getMediaId()));
    mBrowserAdapter.notifyDataSetChanged();
}
 
Example #29
Source File: MusicPlayerActivity.java    From android-music-player with Apache License 2.0 5 votes vote down vote up
private void onMediaItemSelected(MediaBrowser.MediaItem item) {
    if (item.isPlayable()) {
        // ------------ CHANGE 2 - REMOVE FOLLOWING LINES FOR PLAYBACK ON A SERVICE
        MediaMetadata metadata = MusicLibrary.getMetadata(this, item.getMediaId());
        mPlaybackManager.play(metadata);
        updateMetadata(metadata);

        /* ------------ CHANGE 2 - UNCOMMENT FOLLOWING LINES FOR PLAYBACK ON A SERVICE
        getMediaController().getTransportControls().playFromMediaId(item.getMediaId(), null);
        // ------------ CHANGE 2 - END OF PLAYBACK ON A SERVICE SNIPPET */
    }
}
 
Example #30
Source File: MusicBrowserService.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private void handlePlayRequest() {
    delayedStopHandler.removeCallbacksAndMessages(null);
    if (!serviceStarted) {
        try {
            startService(new Intent(getApplicationContext(), MusicBrowserService.class));
        } catch (Throwable e) {
            FileLog.e(e);
        }
        serviceStarted = true;
    }

    if (!mediaSession.isActive()) {
        mediaSession.setActive(true);
    }

    MessageObject messageObject = MediaController.getInstance().getPlayingMessageObject();
    if (messageObject == null) {
        return;
    }
    MediaMetadata.Builder builder = new MediaMetadata.Builder();
    builder.putLong(MediaMetadata.METADATA_KEY_DURATION, messageObject.getDuration() * 1000);
    builder.putString(MediaMetadata.METADATA_KEY_ARTIST, messageObject.getMusicAuthor());
    builder.putString(MediaMetadata.METADATA_KEY_TITLE, messageObject.getMusicTitle());
    AudioInfo audioInfo = MediaController.getInstance().getAudioInfo();
    if (audioInfo != null) {
        Bitmap bitmap = audioInfo.getCover();
        if (bitmap != null) {
            builder.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, bitmap);
        }
    }
    mediaSession.setMetadata(builder.build());
}