Loading Audio
Learn how to load and manage audio sources in flutter_soloud
Overview
flutter_soloud provides multiple ways to load audio content:
- 📂 Local files
- 📦 Assets
- 🌐 Network URLs
- 💾 Memory buffers
- 🌊 Generated waveforms
Audio Source Management
Each loaded sound returns an AudioSource
that must be managed:
// Load and store the audio source
final sound = await SoLoud.instance.loadFile('path/to/sound.mp3');
// Use the sound multiple times
final handle1 = await SoLoud.instance.play(sound);
final handle2 = await SoLoud.instance.play(sound);
// Clean up when no longer needed
await SoLoud.instance.disposeSource(sound);
Listening for Handle Events
You can listen for events related to playback handles using the sound.soundEvents
stream. This allows you to react to changes such as when a sound finishes playing (or stopped) or is disposed.
sound.soundEvents.listen((event) {
debugPrint('Sound event: $event');
});
Each event
is a record containing the following:
SoundEventType
– the type of event (see below)AudioSource
– the source associated with the eventSoundHandle
– the handle for the specific playback instance
The SoundEventType
enum includes:
/// Types of sound events
enum SoundEventType {
/// The handle has reached the end of playback and is no longer valid
handleIsNoMoreValid,
/// The audio source has been disposed
soundDisposed,
}
Listen for all handles to finish and cleanup
Listen to allInstancesFinished
to automatically dispose when all instances complete:
final sound = await SoLoud.instance.loadFile('path/to/sound.mp3');
sound.allInstancesFinished.first.then((_) {
SoLoud.instance.disposeSource(sound);
});
await SoLoud.instance.play(sound);
Loading Methods
You can load audio in several ways, depending on your source and platform requirements. Below are the available methods:
From Files
Load audio directly from a file path.
final sound = await SoLoud.instance.loadFile(
'/path/to/sound.mp3',
mode: LoadMode.memory, // Default
);
loadMem()
instead when targeting web.Memory modes:
LoadMode.memory
– Loads the entire file into RAM (better performance for small files)LoadMode.disk
– Streams from disk (lower memory usage, suitable for large files)
From Memory
Load audio from a byte buffer, such as data read from a file, asset, network or self-made wav file.
final bytes = await File('sound.mp3').readAsBytes();
final sound = await SoLoud.instance.loadMem(
'reference_name.mp3',
bytes,
mode: LoadMode.memory,
);
From Assets
Load audio from bundled assets in your Flutter project.
final sound = await SoLoud.instance.loadAsset(
'assets/sound.mp3',
mode: LoadMode.memory,
assetBundle: rootBundle, // Optional
);
From Network
Load audio directly from a network URL.
final sound = await SoLoud.instance.loadUrl(
'https://example.com/sound.mp3',
mode: LoadMode.memory,
httpClient: client, // Optional custom client
);
loadAsset
or loadUrl
, a temporary file is created on the device. On mobile platforms, the operating system may automatically clear the cache without notice. If this happens after loading the sound but before playing it, a crash may occur because the file is no longer available. This is a known but rare issue. To prevent this, consider using loadMem()
with LoadMode.memory
mode.Format Support
Supported audio formats:
Format | Extension | Description |
---|---|---|
MP3 | .mp3 | Most common compressed format |
WAV | .wav | Uncompressed PCM audio |
OGG | .ogg | Free compressed format |
FLAC | .flac | Lossless compression |
Memory Management
- Reuse loaded sounds instead of loading multiple times
- Dispose sounds when no longer needed
- Use
LoadMode.disk
for large background music files - Use
LoadMode.memory
for sound effects needing quick access