Android Q Beta 2在音乐通知中添加了可寻址进度条,那么我们如何在Android Q操作系统中使用编程与Spotify和SoundCloud实现呢?
您需要向媒体会话添加元数据,如持续时间和播放状态:
MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder();
builder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, currentEpisode.getPodcast().getArtistName());
builder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, currentEpisode.getPodcast().getCollectionName());
builder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, currentEpisode.getTitle());
builder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI, currentEpisode.getImageUrl());
builder.putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, currentEpisode.id);
builder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, player.getDuration());
mediaSessionCompat.setMetadata(builder.build());
PlaybackStateCompat playbackStateCompat = new PlaybackStateCompat.Builder()
.setActions(
PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE |
PlaybackStateCompat.ACTION_FAST_FORWARD | PlaybackStateCompat.ACTION_REWIND |
PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID | PlaybackStateCompat.ACTION_PAUSE |
PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS | PlaybackStateCompat.ACTION_SEEK_TO)
.setState(state, position, speedRate)
.build();
mediaSessionCompat.setPlaybackState(playbackStateCompat);
PlaybackStateCompat.ACTION_SEEK_TO 可以让您具备进度调整的能力。