Skip to main content

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:

  1. Add the package dependency
  2. Initialize SoLoud
  3. Load audio
  4. Play audio
  5. Control playback
  6. Dispose of sound sources
  7. [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.

dart
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:

dart
SoundProps sound = await SoLoud.instance.loadFile('path/to/sound.mp3');

From an app asset:

dart
SoundProps sound = await SoLoud.instance.loadAsset('assets/sound.mp3');

From a network URL:

dart
SoundProps sound = await SoLoud.instance.loadUrl(
  'https://example.com/sound.mp3',
  mode: LoadMode.memory,
);

From bytes in memory:

dart
// 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.

dart
// 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:

dart
SoLoud.instance.pauseSwitch(handle); // Toggles pause state

Seek to a specific timestamp:

dart
SoLoud.instance.seek(handle, Duration(seconds: 5));

Set playback speed:

dart
SoLoud.instance.setRelativePlaySpeed(handle, 2.0); // Play twice as fast

Adjust playback volume:

dart
SoLoud.instance.setVolume(handle, 0.5); // 50% volume

Fade the audio volume:

dart
SoLoud.instance.fadeVolume(
  handle,
  0.0, // target volume
  Duration(seconds: 2), // fade duration
);

Stop playback:

dart
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.

dart
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:

dart
var stream = SoLoud.instance.setBufferStream(
  bufferingType: BufferingType.released,
  sampleRate: 24000,
  channels: Channels.mono,
  format: BufferType.s16le, // pcm16bits
);

Play the stream:

dart
final handle = await SoLoud.instance.play(stream);

As your app receives audio data, add it to the stream:

dart
// Uint8List audioChunk = ...; // Your audio data chunk
SoLoud.instance.addAudioDataStream(
  stream,
  audioChunk,
);

When you're done with the audio stream, mark it as complete:

dart
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.