setMetadata method

void setMetadata(
  1. AudioMetadata metadata
)

Set the media metadata for display in the OS's background audio system.

Different OS versions have different behaviors governing when and how these are displayed. For best results, call this right when the intended Audio starts playback.

Implementation

void setMetadata(AudioMetadata metadata) async {
  try {
    final Map<String, dynamic> metadataMap = <String, dynamic>{};
    // Only add non-null values into [metadataMap].
    if (metadata.id != null) {
      metadataMap[metadataIdKey] = metadata.id;
    }
    if (metadata.title != null) {
      metadataMap[metadataTitleKey] = metadata.title;
    }
    if (metadata.album != null) {
      metadataMap[metadataAlbumKey] = metadata.album;
    }
    if (metadata.artist != null) {
      metadataMap[metadataArtistKey] = metadata.artist;
    }
    if (metadata.genre != null) {
      metadataMap[metadataGenreKey] = metadata.genre;
    }
    if (metadata.durationSeconds != null) {
      metadataMap[metadataDurationSecondsKey] = metadata.durationSeconds;
    }
    if (metadata.artBytes != null) {
      metadataMap[metadataArtBytesKey] = metadata.artBytes;
    }

    await audioMethodChannel.invokeMethod<dynamic>(
        setMetadataMethod, metadataMap);
  } on PlatformException catch (e) {
    _logger.severe('setMetadata error, category: ', e);
  }
}