gpt4 book ai didi

android - 如何构建媒体项目?安卓

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:34:30 26 4
gpt4 key购买 nike

我已经阅读了官方文档,但我无法理解这种方法的作用:

@Override
public void onLoadChildren(final String parentMediaId,
final Result<List<MediaItem>> result) {

...
// Assume for example that the music catalog is already loaded/cached.

List<MediaItem> mediaItems = new ArrayList<>();

// Check if this is the root menu:
if (MY_MEDIA_ROOT_ID.equals(parentMediaId)) {

// build the MediaItem objects for the top level,
// and put them in the mediaItems list

where is documented this part? how to build the media item?

} else {

// examine the passed parentMediaId to see which submenu we're at,
// and put the children of that menu in the mediaItems list
}
...
}

如果我想在手机上播放音乐,如何根据手机音乐“构建媒体项目”?

最佳答案

我的回答有点晚了,但我希望它能帮助以后的疑惑。

在你的 Service.java 中

case MEDIA_ID_ROOT:
for (MediaMetadataCompat track : mMusicProvider.getAllMusics()) {

String mediaId = track.getDescription().getMediaId();
//Artist song
String title = String.valueOf(track.getDescription().getTitle());
//Artist name
String subTitle = String.valueOf(track.getDescription().getSubtitle());
//Artist album
String descriptin = String.valueOf(track.getDescription().getDescription());
//Song duration
Long duration = track.getLong(MediaMetadataCompat.METADATA_KEY_DURATION);

Bundle songDuration = new Bundle();
songDuration.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration);

MediaDescriptionCompat desc =
new MediaDescriptionCompat.Builder()
.setMediaId(mediaId)
.setTitle(title)
.setSubtitle(subTitle)
.setDescription(descriptin)
.setExtras(songDuration)
.build();

MediaBrowserCompat.MediaItem songList =
new MediaBrowserCompat.MediaItem(desc,
MediaBrowserCompat.MediaItem.FLAG_PLAYABLE);
mediaItems.add(songList);
}
break;

mMusicProvider 是我模型的对象

在你的 provider.java 中

// Categorized caches for music track data:                                     
private final LinkedHashMap<String, MediaMetadataCompat> mMusicListById;

public Iterable<MediaMetadataCompat> getAllMusics() {

if (mCurrentState != State.INITIALIZED || mMusicListById.isEmpty()) {
return Collections.emptyList();
}
return mMusicListById.values();
}

MediaBrowserCompat.MediaItem 基本上可以有MediaId、Title、Subtitle、Description、IconBitmap、IconUri、Bundle。在我的例子中,我需要歌曲的持续时间,所以这就是为什么我将其作为额外内容添加到 bundle 中的原因。您可以添加更多额外信息,例如 Composer 、年份、轨道编号等。

如果您不需要任何额外信息,您可以调用 getdescription()。

for (MediaMetadataCompat track : mMusicProvider.getAllMusics()) {
MediaBrowserCompat.MediaItem bItem =
new MediaBrowserCompat.MediaItem(track.getDescription(),
MediaBrowserCompat.MediaItem.FLAG_PLAYABLE);

mediaItems.add(bItem);

关于android - 如何构建媒体项目?安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44222923/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com