我希望扩展MediaItem,这样我就可以很容易地添加额外的元数据与自定义类,我不能与extras属性(使用extras也比如果我的额外信息只是类的一部分更尴尬)。我试过扩展MediaItem,但是当我在BaseAudioHandler中更改函数参数时,它说这是一个无效的覆盖。这是我班上的一个例子。大部分是从MediaItem:
复制粘贴过来的class CustomMediaItem extends MediaItem {
final CustomClass customValue;
CustomMediaItem({
/// A unique id.
required final String id,
/// The title of this media item.
required final String title,
/// The album this media item belongs to.
final String? album,
/// The artist of this media item.
final String? artist,
/// The genre of this media item.
final String? genre,
/// The duration of this media item.
final Duration? duration,
/// The artwork for this media item as a uri.
final Uri? artUri,
/// Whether this is playable (i.e. not a folder).
final bool? playable = true,
/// Override the default title for display purposes.
final String? displayTitle,
/// Override the default subtitle for display purposes.
final String? displaySubtitle,
/// Override the default description for display purposes.
final String? displayDescription,
/// The rating of the media item.
final Rating? rating,
/// A map of additional metadata for the media item.
///
/// The values must be of type `int`, `String`, `bool` or `double`.
final Map<String, dynamic>? extras,
required this.customClass,
}) : super(
id: id,
title: title,
album: album,
artist: artist,
genre: genre,
duration: duration,
artUri: artUri,
playable: playable,
displayTitle: displayTitle,
displaySubtitle: displaySubtitle,
displayDescription: displayDescription,
rating: rating,
extras: extras,
);
}
当我试图替换我的BaseAudioHandler参数,我得到这个:
'MusicPlayerBackgroundTask.addQueueItem' ('Future<void> Function(CustomMediaItem)') isn't a valid override of 'BaseAudioHandler.addQueueItem' ('Future<void> Function(MediaItem)').dart(invalid_override)
我也尝试创建自己的AudioHandler,但它给出了相同的错误。
使用为此目的而设计的extras
属性。摘自MediaItem
的文档:
附加属性
Map<String, dynamic>? extras
最后媒体项的附加元数据映射。
取值必须为
int
、String
、bool
或double
类型。
所以如果你想存储一个额外的布尔元数据叫做isPreview
,你可以这样做:
final item = MediaItem(
id: 'id1',
title: 'Song title',
album: 'Album title',
extras: {
'isPreview': true,
},
);
您可以在extras
映射中存储多少额外的属性是没有限制的。