Playback Controls
Learn how to control audio playback
Basic Playback
After loading a sound, you can play it using the play() method:
final sound = await soloud.loadAsset('assets/your-audio-file.mp3');
final handle = await SoLoud.instance.play(
sound,
volume: 1.0,
pan: 0.0,
paused: false,
looping: false,
);
[...]
/// Dispose the sound when it's no longer needed
await SoLoud.instance.disposeSource(sound);
Note:
- The returned
handleuniquely identifies this instance of the playing sound and it becomes invalid when the sound is stopped or ends. - The
soundremains valid in memory and can be played without lags untildisposeSource()is called.
The load* methods accept the autoDispose parameter, which will automatically dispose the sound when all its playing handles are finished.
To make thisgs simplier and faster in some circumstances, you can use the playSource method to play audio from the given source and forget about the sound handle and its lifecycle. It will automatically dispose the source when it is finished playing:
SoLoud.instance.playSource(assets: 'assets/your-audio-file.mp3'); // accepts files and urls also
Pausing and Resuming
// Toggle pause state
SoLoud.instance.pauseSwitch(handle);
// Set specific pause state
SoLoud.instance.setPause(handle, true); // pause
SoLoud.instance.setPause(handle, false); // resume
// Check pause state
final isPaused = SoLoud.instance.getPause(handle);
Seeking
// Seek to specific position
SoLoud.instance.seek(handle, Duration(seconds: 5));
// Get current position
final position = SoLoud.instance.getPosition(handle);
Looping
Enable looping during playback:
final handle = await SoLoud.instance.play(
sound,
looping: true,
loopingStartAt: Duration(seconds: 1),
);
Control looping for an already playing sound:
// Enable/disable looping
SoLoud.instance.setLooping(handle, true);
// Set loop point
SoLoud.instance.setLoopPoint(handle, Duration(seconds: 1));
// Check if looping
final isLooping = SoLoud.instance.getLooping(handle);
Playback Speed
Adjust the playback speed of a sound:
// Set relative play speed (1.0 is normal speed)
SoLoud.instance.setRelativePlaySpeed(handle, 2.0); // Play twice as fast
// Get current play speed
final speed = SoLoud.instance.getRelativePlaySpeed(handle);
Voice Protection
Protect important sounds from being stopped when voice limit is reached:
// Protect background music from being stopped
SoLoud.instance.setProtectVoice(musicHandle, true);
Voice Management
// Set maximum number of concurrent sounds (default is 16)
SoLoud.instance.setMaxActiveVoiceCount(32);
// Get current number of playing sounds
final activeVoices = SoLoud.instance.getActiveVoiceCount();
// Check if a handle is still valid
final isValid = SoLoud.instance.getIsValidVoiceHandle(handle);