Flutter Sound

repository·master·Indexed 21 days ago

https://github.com/canardoux/flutter_sound

A comprehensive audio library for Flutter providing high-level APIs for playing and recording audio across iOS, Android, and Web. It supports various playback sources (Dart buffers, assets, files, remote URLs, and streams) and recording destinations. Key features include real-time stream processing with PCM Float32 and Int16 formats, a FlutterSoundHelper utility for WAVE and Raw PCM conversion, and the FlutterSoundPlayer for managing audio playback lifecycles.

Tokens
7.1K
Snippets
31
Records
37
Agent score
76%

What's inside flutter_sound

  1. Overview of Flutter Sound capabilities

    master
    Flutter Sound is a library package designed for audio playback and recording across iOS, Android, and Web platforms. It provides a comprehensive set of tools for handling audio files, playing various sources, and recording to multiple destinations.
  2. How Flutter Sound and Streams work together

    master

    A core feature of Flutter Sound is its deep integration with Dart Streams, allowing for real-time audio processing:

    • Recording to Streams: You can record audio directly into a Dart stream of audio data using PCM Float32 or PCM Int16 formats. This is useful for processing live audio in Dart or streaming it to a remote host.
    • Playback from Streams: You can play audio from a Dart stream of PCM Float32 or PCM Int16 data. This enables playing live audio generated within Dart (e.g., via a sequencer or sound generator) or received from a remote host.
  3. Understanding the τ (Tau) family of audio projects

    master

    The Tau family consists of several related Flutter plugins for different stages of audio development and platform support:

    • Flutter Sound 9.x: The current legacy plugin, maintained for long-term use.
    • Etau: An Alpha-stage implementation of the W3C Web Audio API for Flutter. It uses a node-chain model (Source Node $\rightarrow$ Processing Nodes $\rightarrow$ Destination Node) and is released under GPL v3.
    • Taudio: The successor to Flutter Sound (version 10.0). It is a complete rewrite that maintains compatibility with the Flutter Sound 9.x API while adding a wrapper over Etau. It is also released under GPL v3.
    • Tauweb / Tauwar: Specific implementations of Etau for Web and Mobile respectively.
  4. Customize the iOS launch screen assets

    master

    To change the image displayed during the app's launch on iOS, you can either replace the image files directly in the file system or use Xcode.

    Option 1: File System Replace the existing image files located in the example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory with your own assets.

    Option 2: Xcode (Recommended for visual management)

    1. Open the iOS workspace in Xcode using the command: open ios/Runner.xcworkspace.
    2. In the Project Navigator, navigate to Runner/Assets.xcassets.
    3. Locate the LaunchImage asset set and drag/drop your desired images into it.
    open ios/Runner.xcworkspace
  5. Use FlutterSoundHelper for audio file and buffer manipulation

    master

    The FlutterSoundHelper is a singleton utility class used for handling audio files and buffers, specifically for converting between WAVE and Raw PCM formats. You can access it using the FlutterSoundHelper() factory constructor.

    It also includes a built-in logger for debugging, which can be configured using setLogLevel(Level theNewLogLevel).

    // Access the singleton
    var helper = FlutterSoundHelper();
    
    // Configure logging
    helper.setLogLevel(Level.info);
    
    // Use the logger
    helper.logger.d('An information');
  6. Manage FlutterSoundRecorder lifecycle

    master

    The recorder follows a specific state machine. You must manage its lifecycle to avoid resource leaks or errors.

    • openRecorder({bool isBGService = false}): Must be called before any recording operations. If isBGService is true, it configures the recorder for background service use.
    • closeRecorder(): Releases all system resources and deletes temporary files created during recording. Always call this when the recorder is no longer needed (e.g., in a Flutter dispose() method).
    • isRecording / isStopped / isPaused: Boolean getters to check the current recorderState.

    Important: If you are using a widget, ensure you clean up the recorder to prevent memory leaks and device resource locking.

    @override
    void dispose() {
      if (myRecorder != null) {
        myRecorder!.closeRecorder();
        myRecorder = null;
      }
      super.dispose();
    }
  7. Monitor recording progress with RecordingDisposition

    master

    The RecordingDisposition class holds real-time details about an active recording. You can access these details by subscribing to the dispositionStream (available on the recorder instance).

    It provides:

    • duration: The total duration of the recording at that point in time.
    • decibels: The volume of the audio being captured (ranges from 0 to 120).
    // Example of how RecordingDisposition is structured
    final disposition = RecordingDisposition(Duration(seconds: 5), 85.0);
    print(disposition.toString()); // 'duration: 0:00:05.000000 decibels: 85.0'
    
    // Use the zero constructor for initial StreamBuilder values
    final initialValue = RecordingDisposition.zero();
  8. Use FlutterSoundRecorder to record audio

    master

    The FlutterSoundRecorder class allows you to record audio to either a file or a Dart stream.

    Basic Workflow

    1. Instantiate: Create a new FlutterSoundRecorder instance.
    2. Open: Call openRecorder() to initialize the recorder and acquire system resources.
    3. Start: Call startRecorder() specifying a destination (file or stream).
    4. Control (Optional): Use pauseRecorder() and resumeRecorder() to manage the recording state.
    5. Stop: Call stopRecorder() to finish recording and retrieve the file URL.
    6. Close: Call closeRecorder() to release resources. It is highly recommended to call this in your widget's dispose() method.

    Destinations

    • File: Provide a path via toFile in startRecorder().
    • Streams: Provide a StreamSink via toStream, toStreamFloat32, or toStreamInt16 in startRecorder().
    // 1. Instantiate
    FlutterSoundRecorder myRecorder = FlutterSoundRecorder();
    
    // 2. Open
    await myRecorder.openRecorder();
    
    // 3. Start recording to a file
    await myRecorder.startRecorder(toFile: 'path/to/recording.aac', codec: Codec.aacADTS);
    
    // 4. Stop recording
    String? url = await myRecorder.stopRecorder();
    print('Recorded file at: $url');
    
    // 5. Close
    await myRecorder.closeRecorder();
  9. Use FlutterSoundPlayer for audio playback

    master

    A FlutterSoundPlayer is an object used to play audio from various sources including files, remote URLs, internal buffers, or Dart streams. You can have multiple instances of the player running simultaneously, each controlling its own source.

    Basic Lifecycle

    1. Instantiate: Create a new FlutterSoundPlayer().
    2. Open: Call openPlayer() to allocate system resources.
    3. Play: Use startPlayer() to begin playback.
    4. Control: Use verbs like pausePlayer(), resumePlayer(), or setVolume() (available in subsequent segments).
    5. Stop: Call stopPlayer() to end playback.
    6. Release: Call closePlayer() to free all resources. This will automatically call stopPlayer() if necessary.
    FlutterSoundPlayer myPlayer = FlutterSoundPlayer();
    await myPlayer.openPlayer();
    // ... play audio ...
    await myPlayer.closePlayer();
  10. License information for Flutter Sound

    master

    Flutter Sound is published under the MPL-2.0 License (Mozilla Public License 2.0).

    • Weak Copyleft: If you modify the Flutter Sound source code itself, you must publish those modifications under the MPL license.
    • App Licensing: You are free to publish your application using any license you choose, including proprietary or closed-source licenses.