gpt4 book ai didi

android - MediaControllerCompat 的 getMetadata 返回 null

转载 作者:行者123 更新时间:2023-12-05 06:22:10 25 4
gpt4 key购买 nike

我正在尝试使用 this 在 Android 中制作一个音频应用程序视频作为指南,我在使用音频的元数据创建通知时遇到了问题。

这是我用来从设备中提取音频文件的代码:

public void loadData() {
ContentResolver contentResolver = context.getContentResolver();

String selection = Media.IS_MUSIC + "!= 0";
String[] projection = {
Media._ID,
Media.ARTIST,
Media.TITLE,
Media.DISPLAY_NAME,
Media.DURATION,
Media.ALBUM
};

Cursor cursor = contentResolver.query(
Media.EXTERNAL_CONTENT_URI, projection, selection, null, null
);

if(cursor == null || !cursor.moveToFirst()) return;
else {
int size = cursor.getCount();
metadata = new ArrayList<>(size);
mediaItems = new ArrayList<>(size);
}

int
idIndex = cursor.getColumnIndex(Media._ID),
artistIndex = cursor.getColumnIndex(Media.ARTIST),
titleIndex = cursor.getColumnIndex(Media.TITLE),
displayNameIndex = cursor.getColumnIndex(Media.DISPLAY_NAME),
durationIndex = cursor.getColumnIndex(Media.DURATION),
albumIndex = cursor.getColumnIndex(Media.ALBUM);

do {
String mediaId = cursor.getString(idIndex);
MediaMetadataCompat data = new MediaMetadataCompat.Builder()
.putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, cursor.getString(idIndex))
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, cursor.getString(albumIndex))
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, cursor.getString(artistIndex))
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, cursor.getString(titleIndex))
.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, cursor.getString(displayNameIndex))
.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, cursor.getLong(durationIndex))
.build();

metadata.add(data);
mediaItems.add(new MediaBrowserCompat.MediaItem(
data.getDescription(),
MediaItem.FLAG_PLAYABLE
));
} while(cursor.moveToNext());

cursor.close();
}

public List<MediaItem> getMediaItems() {
return mediaItems;
}

这是我的 MediaBrowserService 的 onLoadChildren 代码:

@Override
public void onLoadChildren(@NonNull String parentId,
@NonNull Result<List<MediaBrowserCompat.MediaItem>> result
) {
if(parentId.equals(MEDIA_ROOT_ID)) {
result.sendResult(source.getMediaItems());
}
}

最后,抛出错误的代码(我在创建 MediaSession 和 setActive(true) 后调用 createNotifcation()):

private void createNotification() {
NotificationCompat.Action playAction = new NotificationCompat.Action(
android.R.drawable.ic_media_play,
getString(R.string.play),
MediaButtonReceiver.buildMediaButtonPendingIntent(
ExoMusicService.this,
PlaybackStateCompat.ACTION_PLAY
)
);
NotificationCompat.Action pauseAction = new NotificationCompat.Action(
android.R.drawable.ic_media_pause,
getString(R.string.pause),
MediaButtonReceiver.buildMediaButtonPendingIntent(
ExoMusicService.this,
PlaybackStateCompat.ACTION_PAUSE
)
);
MediaControllerCompat controller = mediaSession.getController();
Log.i(LOG_TAG, "createNotification: " + (controller == null)); // false
MediaMetadataCompat metadata = controller.getMetadata();
Log.i(LOG_TAG, "createNotification: " + (metadata == null)); // true
MediaDescriptionCompat description = metadata.getDescription();

notification = new NotificationCompat.Builder(ExoMusicService.this, PLAYBACK_CHANNEL_ID)
.setContentTitle(description.getTitle())
.setContentText(description.getSubtitle())
.setContentIntent(controller.getSessionActivity())
.setStyle(
new MediaStyle()
.setShowActionsInCompactView(0)
.setMediaSession(mediaSession.getSessionToken())
.setShowCancelButton(true)
)
.setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(
ExoMusicService.this,
PlaybackStateCompat.ACTION_STOP
))
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.addAction(playerManager.isPlaying()? pauseAction : playAction)
.setSmallIcon(R.drawable.icon)
.build();
}

如果我没理解错的话,我相信这就是与此问题相关的所有代码。知道我可能会忽略什么吗?

最佳答案

在显示通知之前,您应该调用 mediaSession.setMetadata(/* 您的元数据 */)。

元数据应包含有关正在播放(或准备、暂停等)的当前音乐项目的信息。

请引用documentation了解详情。

关于android - MediaControllerCompat 的 getMetadata 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59367310/

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