uri static method

UriAudioSource uri(
  1. Uri uri, {
  2. Map<String, String>? headers,
  3. dynamic tag,
})

Creates an AudioSource from a Uri with optional headers by attempting to guess the type of stream. On iOS, this uses Apple's SDK to automatically detect the stream type. On Android, the type of stream will be guessed from the extension.

If you are loading DASH or HLS streams that do not have standard "mpd" or "m3u8" extensions in their URIs, this method will fail to detect the stream type on Android. If you know in advance what type of audio stream it is, you should instantiate DashAudioSource or HlsAudioSource directly.

If headers are set, just_audio will create a cleartext local HTTP proxy on your device to forward HTTP requests with headers included.

The tag is for associating your app's own data with each audio source, e.g. title, cover art, a primary key for your DB. Such data can be conveniently retrieved from the tag while rendering the UI.

When using just_audio_background, tag must be a MediaItem, a class provided by that package. If you wish to have more control over the tag for background audio purposes, consider using the plugin audio_service instead of just_audio_background.

Implementation

static UriAudioSource uri(Uri uri,
    {Map<String, String>? headers, dynamic tag}) {
  bool hasExtension(Uri uri, String extension) =>
      uri.path.toLowerCase().endsWith('.$extension') ||
      uri.fragment.toLowerCase().endsWith('.$extension');
  if (hasExtension(uri, 'mpd')) {
    return DashAudioSource(uri, headers: headers, tag: tag);
  } else if (hasExtension(uri, 'm3u8')) {
    return HlsAudioSource(uri, headers: headers, tag: tag);
  } else {
    return ProgressiveAudioSource(uri, headers: headers, tag: tag);
  }
}