UnicornsOnLSD
UnicornsOnLSD

Reputation: 773

Using an extended MediaItem in audio_service v18

I'm looking to extend MediaItem so that I can easily add extra metadata with custom classes, which I can't do with the extras property (using extras is also more awkward than if my extra info was just part of the class). I've tried extending MediaItem, but when I change the function arguments in my BaseAudioHandler, it says that it is an invalid override. Here is an example of my class. Most of it is copy-pasted from 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,
        );
}

When I try to replace an argument in my BaseAudioHandler, I get this:

'MusicPlayerBackgroundTask.addQueueItem' ('Future<void> Function(CustomMediaItem)') isn't a valid override of 'BaseAudioHandler.addQueueItem' ('Future<void> Function(MediaItem)').dart(invalid_override)

I also tried creating my own AudioHandler, but it gave the same error.

Upvotes: 1

Views: 500

Answers (1)

Ryan Heise
Ryan Heise

Reputation: 2786

Use the extras property designed for that purpose. From the documentation of MediaItem:

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.

So if you wanted to store an extra boolean metadata called isPreview, you would do something like:

final item = MediaItem(
  id: 'id1',
  title: 'Song title',
  album: 'Album title',
  extras: {
    'isPreview': true,
  },
);

There is no limit to how many extra properties you can store in the extras map.

Upvotes: 2

Related Questions