Play or stream sound and music with flutter_soloud
Learn how to play or stream audio in your Flutter app using the flutter_soloud package.
This recipe demonstrates how to integrate the flutter_soloud package
into your Flutter application to play sound effects and
background music with low latency.
To get started using the package, follow these steps:
- Add the package dependency
- Initialize
SoLoud - Load audio
- Play audio
- Control playback
- Dispose of sound sources
- [Optional] Stream audio
1. Add the package dependency
#To add package:flutter_soloud as a dependency, use flutter pub add:
flutter pub add flutter_soloud
2. Initialize SoLoud
#
Before playing any audio,
you need to initialize the SoLoud instance.
You can also configure playback settings like sample rate,
buffer size, and the number of channels.
import 'package:flutter_soloud/flutter_soloud.dart';
// Initialize with default settings
await SoLoud.instance.init();
// Or configure with custom settings
await SoLoud.instance.init(
sampleRate: 44100,
bufferSize: 2048,
channels: Channels.stereo,
);
3. Load audio
#
The flutter_soloud package supports various audio formats
including MP3, WAV, OGG, and FLAC.
You can load audio from different sources.
From a local file:
SoundProps sound = await SoLoud.instance.loadFile('path/to/sound.mp3');
From an app asset:
SoundProps sound = await SoLoud.instance.loadAsset('assets/sound.mp3');
From a network URL:
SoundProps sound = await SoLoud.instance.loadUrl(
'https://example.com/sound.mp3',
mode: LoadMode.memory,
);
From bytes in memory:
// Uint8List bytes = ...; // Your audio data
SoundProps sound = await SoLoud.instance.loadMem(
'reference_name.mp3',
bytes,
mode: LoadMode.memory,
);
4. Play audio
#
Once the audio is loaded,
you can play it using SoLoud.instance.play.
You can also configure gapless looping.
// Play the sound
var handle = await SoLoud.instance.play(sound);
// Play with gapless looping
handle = await SoLoud.instance.play(
sound,
looping: true,
loopingStartAt: Duration(seconds: 1),
);
The play method returns a handle that references
that particular instance of the sound being played.
You can use this handle to control the playback.
5. Control playback
#
With the handle returned from play,
you can perform various actions such as pausing or resuming playback:
SoLoud.instance.pauseSwitch(handle); // Toggles pause state
Seek to a specific timestamp:
SoLoud.instance.seek(handle, Duration(seconds: 5));
Set playback speed:
SoLoud.instance.setRelativePlaySpeed(handle, 2.0); // Play twice as fast
Adjust playback volume:
SoLoud.instance.setVolume(handle, 0.5); // 50% volume
Fade the audio volume:
SoLoud.instance.fadeVolume(
handle,
0.0, // target volume
Duration(seconds: 2), // fade duration
);
Stop playback:
await SoLoud.instance.stop(handle);
6. Dispose of sound sources
#When you are finished with a sound source, remember to dispose it to free up resources.
await SoLoud.instance.disposeSource(sound);
7. [Optional] Stream audio
#
The flutter_soloud package also supports streaming
audio data in real-time.
Initialize and configure a buffer stream:
var stream = SoLoud.instance.setBufferStream(
bufferingType: BufferingType.released,
sampleRate: 24000,
channels: Channels.mono,
format: BufferType.s16le, // pcm16bits
);
Play the stream:
final handle = await SoLoud.instance.play(stream);
As your app receives audio data, add it to the stream:
// Uint8List audioChunk = ...; // Your audio data chunk
SoLoud.instance.addAudioDataStream(
stream,
audioChunk,
);
When you're done with the audio stream, mark it as complete:
SoLoud.instance.setDataIsEnded(stream);
More information
#
For more detailed information and examples,
visit the flutter_soloud package
on pub.dev or
check out the flutter_soloud Package of the Week video.
Unless stated otherwise, the documentation on this site reflects Flutter 3.41.2. Page last updated on 2026-02-20. View source or report an issue.