在 audio_service v18 中使用扩展的 MediaItem
Using an extended MediaItem in audio_service v18
我正在寻找扩展 MediaItem 以便我可以使用自定义 classes 轻松添加额外的元数据,这是我不能用 extras 属性 做的(使用 extras 也更尴尬比起我的额外信息只是 class 的一部分)。我试过扩展 MediaItem,但是当我更改我的 BaseAudioHandler 中的函数参数时,它说这是一个无效的覆盖。这是我的 class 的示例。其中大部分是从 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
的documentation:
extras property
Map<String, dynamic>? extras
final
A map of additional metadata for the media item.
The values must be of type int
, String
, bool
or double
.
因此,如果您想存储一个名为 isPreview
的额外布尔元数据,您可以这样做:
final item = MediaItem(
id: 'id1',
title: 'Song title',
album: 'Album title',
extras: {
'isPreview': true,
},
);
您可以在 extras
地图中存储的额外属性数量没有限制。
我正在寻找扩展 MediaItem 以便我可以使用自定义 classes 轻松添加额外的元数据,这是我不能用 extras 属性 做的(使用 extras 也更尴尬比起我的额外信息只是 class 的一部分)。我试过扩展 MediaItem,但是当我更改我的 BaseAudioHandler 中的函数参数时,它说这是一个无效的覆盖。这是我的 class 的示例。其中大部分是从 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
的documentation:
extras property
Map<String, dynamic>? extras
finalA map of additional metadata for the media item.
The values must be of type
int
,String
,bool
ordouble
.
因此,如果您想存储一个名为 isPreview
的额外布尔元数据,您可以这样做:
final item = MediaItem(
id: 'id1',
title: 'Song title',
album: 'Album title',
extras: {
'isPreview': true,
},
);
您可以在 extras
地图中存储的额外属性数量没有限制。