Flutter Sound
repository·master·Indexed 21 days ago
https://github.com/canardoux/flutter_soundA 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.
What's inside flutter_sound
- 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.
How Flutter Sound and Streams work together
masterA 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 Float32orPCM Int16formats. 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 Float32orPCM Int16data. This enables playing live audio generated within Dart (e.g., via a sequencer or sound generator) or received from a remote host.
- Recording to Streams: You can record audio directly into a Dart stream of audio data using
Supported playback and recording sources
masterFlutter Sound supports a wide variety of input and output methods:
Playback Sources:
- Dart buffers
- Assets
- Files
- Remote URLs
- Dart Streams
Recording Destinations:
- Dart buffers
- Files
- Dart Streams
Understanding the τ (Tau) family of audio projects
masterThe
Taufamily 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
Etaufor Web and Mobile respectively.
SDK requirements for Flutter Sound
masterTo use Flutter Sound, ensure your target platforms meet the following minimum SDK requirements:
- iOS: 10.0 or later
- Android: 21 or later
- Web: Supported on main browsers including Google Chrome, Firefox, and Safari.
Customize the iOS launch screen assets
masterTo 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)
- Open the iOS workspace in Xcode using the command:
open ios/Runner.xcworkspace. - In the Project Navigator, navigate to
Runner/Assets.xcassets. - Locate the
LaunchImageasset set and drag/drop your desired images into it.
open ios/Runner.xcworkspace- Open the iOS workspace in Xcode using the command:
Use FlutterSoundHelper for audio file and buffer manipulation
masterThe
FlutterSoundHelperis 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 theFlutterSoundHelper()factory constructor.It also includes a built-in
loggerfor debugging, which can be configured usingsetLogLevel(Level theNewLogLevel).// Access the singleton var helper = FlutterSoundHelper(); // Configure logging helper.setLogLevel(Level.info); // Use the logger helper.logger.d('An information');Manage FlutterSoundRecorder lifecycle
masterThe 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. IfisBGServiceis 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 Flutterdispose()method).isRecording/isStopped/isPaused: Boolean getters to check the currentrecorderState.
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(); }Monitor recording progress with RecordingDisposition
masterThe
RecordingDispositionclass holds real-time details about an active recording. You can access these details by subscribing to thedispositionStream(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();Use FlutterSoundRecorder to record audio
masterThe
FlutterSoundRecorderclass allows you to record audio to either a file or a Dart stream.Basic Workflow
- Instantiate: Create a new
FlutterSoundRecorderinstance. - Open: Call
openRecorder()to initialize the recorder and acquire system resources. - Start: Call
startRecorder()specifying a destination (file or stream). - Control (Optional): Use
pauseRecorder()andresumeRecorder()to manage the recording state. - Stop: Call
stopRecorder()to finish recording and retrieve the file URL. - Close: Call
closeRecorder()to release resources. It is highly recommended to call this in your widget'sdispose()method.
Destinations
- File: Provide a path via
toFileinstartRecorder(). - Streams: Provide a
StreamSinkviatoStream,toStreamFloat32, ortoStreamInt16instartRecorder().
// 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();- Instantiate: Create a new
Use FlutterSoundPlayer for audio playback
masterA
FlutterSoundPlayeris 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
- Instantiate: Create a new
FlutterSoundPlayer(). - Open: Call
openPlayer()to allocate system resources. - Play: Use
startPlayer()to begin playback. - Control: Use verbs like
pausePlayer(),resumePlayer(), orsetVolume()(available in subsequent segments). - Stop: Call
stopPlayer()to end playback. - Release: Call
closePlayer()to free all resources. This will automatically callstopPlayer()if necessary.
FlutterSoundPlayer myPlayer = FlutterSoundPlayer(); await myPlayer.openPlayer(); // ... play audio ... await myPlayer.closePlayer();- Instantiate: Create a new
License information for Flutter Sound
masterFlutter 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.