audioplayers

repository·main·Indexed 24 days ago

https://github.com/bluefireteam/audioplayers

A Flutter plugin for playing audio across Android, iOS, macOS, Windows, Linux, and web. It supports multiple audio sources including URLs, assets, device files, and byte arrays. The library uses a federated plugin architecture with endorsed implementations for each platform, including GStreamer for Linux and an optional ExoPlayer implementation for Android via the audioplayers_android_exo package.

Tokens
12.7K
Snippets
29
Records
88
Agent score
80%

What's inside audioplayers

  1. Explore the AudioPlayer Example capabilities

    main

    The audioplayers example application demonstrates the core features of the plugin through several functional tabs:

    • Src (Sources): Demonstrates how to play audio from different origins:
      • Url: Remote audio from the internet.
      • Asset: Bundled app assets.
      • Device File: Local files from a specified device path.
      • Byte Array: Audio data from a byte array.
    • Ctrl (Controls): Manages playback properties like volume, balance, and rate.
    • Stream: Monitors and displays real-time stream updates and properties.
    • Ctx (Context): Customizes the audio context specifically for mobile devices.
    • Log: Provides a view of system logs.

    The example also includes a PlayerWidget, which serves as a pre-built, simple audio player interface for quick integration.

  2. How AudioPlayer works

    main

    An AudioPlayer instance acts like a single playback device (e.g., a boombox) that can play one audio source at a time. You can instantiate multiple AudioPlayer objects to play multiple audios simultaneously or to manage different audio sources independently.

    final player = AudioPlayer();
  3. How to implement a new platform for audioplayers

    main

    To add support for a new platform, you must implement the AudioplayersPlatformInterface and the GlobalAudioplayersPlatformInterface.

    1. Extend AudioplayersPlatformInterface with your platform-specific logic.
    2. Register your implementation by assigning it to the instance property of both interfaces during plugin registration.

    Note: This package is typically a dependency of the main audioplayers plugin and does not need to be added manually to your pubspec.yaml unless you are building a platform implementation.

  4. Android SDK Support and Limitations

    main

    The audioplayers plugin sets minSdk to 19, but official support and testing are only guaranteed for Android SDK >= 23.

    Key platform-specific behaviors for Android:

    • Byte Arrays: Supported natively on SDK >= 23.
    • Playback Rate: Supported on SDK >= 23.
    • Low Latency Mode (LLM): Supported on SDK >= 21, except when using the audioplayers_android_exo implementation.
    • Advanced Controls: Features like duck audio, respect silence, stay awake, and playing route are supported on Android except when in Low Latency Mode (LLM).
  5. Configure audio sessions with AudioContext

    main
    Audio session configuration (flags and parameters previously spread across various methods) is now unified into a single AudioContext configuration object. You can apply this configuration globally or per player (per-player configuration is currently only available on Android).
  6. Out-of-Scope Features for audioplayers

    main

    The following functionalities are not provided by the audioplayers package:

    • Audio Recording: Use a package like audio_recorder for recording from the microphone.
    • Playlists: You must implement playlist logic yourself (e.g., playing multiple audios in sequence).
    • Music Metadata: The library does not extract metadata (author, track, etc.) from files like MP3s.
    • UI/Interfaces: The library is for audio playback logic; use Flutter to build your own user interfaces.
  7. How AudioCache works for local assets

    main

    Since Flutter does not natively support playing audio directly from assets, the AudioCache class handles this by copying assets to a temporary folder on the device to be played as local files. It maintains a cache of these files to allow for immediate replay without the delay of re-copying.

    By default, AudioPlayer uses a shared global instance of AudioCache with a prefix of "/assets" (following Flutter conventions).

    final player = AudioPlayer();
    await player.play(AssetSource('audio/my-audio.wav'));
  8. Use Sources instead of AudioCache

    main

    The AudioCache API has been deprecated for general playback. Instead, use the Source sealed class to define where audio is coming from. The AudioPlayer now accepts these Source objects directly via setSource().

    Available Source types:

    • UrlSource: For remote audio from an internet URL.
    • DeviceFileSource: For files located on the user's device.
    • AssetSource: For audio files bundled within your app's assets directory. Using AssetSource automatically handles caching via AudioCache internally.
    • BytesSource: For passing raw audio bytes directly (platform support varies).
  9. Understand audio format and encoding support

    main

    The audioplayers package acts as a centralized interface for native audio players on each platform. It does not parse audio bytes itself; instead, it relies on the underlying platform's native support.

    Important:

    • If an audio file fails to play, verify that the actual encoding/format is supported by the platform (not just the file extension).
    • Do not open issues regarding encoding or format compatibility unless you believe it is a specific audioplayers bug.

    Platform Support Links:

  10. Quickstart with AudioPlayer

    main

    To play audio in a Flutter application, import the audioplayers package, instantiate an AudioPlayer, and use the play method with a UrlSource (or other source types).

    import 'package:audioplayers/audioplayers.dart';
    // ...
    final player = AudioPlayer();
    await player.play(UrlSource('https://example.com/my-audio.wav'));
  11. Configure AudioCache prefix

    main

    You can customize how assets are resolved by changing the AudioCache prefix. You can apply changes globally for all players or locally to a specific player instance.

    Global configuration: Set AudioCache.instance to a new AudioCache with a custom prefix (e.g., an empty string to remove the default /assets prefix).

    Per-player configuration: Assign a new AudioCache instance to the player.audioCache property. This is useful when loading assets from different packages.