soundpool 1.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 96

soundpool #

A Flutter Sound Pool for playing short media files.

Sound Pool caches audio tracks in memory. This can be useful in following scenarios:

  • lower latency between play signal and actual playing of the sound (audio does not need to be read from disc/web),
  • the same sound may be used multiple times.

Inspired by Android SoundPool API. This also means that multiple optional features are supported on Android only.

Example:

    import 'package:soundpool/soundpool.dart';

    Soundpool pool = Soundpool(streamType: StreamType.notification);

    int soundId = await rootBundle.load("sounds/dices.m4a").then((ByteData soundData) {
                  return pool.load(soundData);
                });
    int streamId = await pool.play(soundId);

DOs and DON'Ts #

  • DO NOT create the Soundpool instance multiple times; DO create a Soundpool for logically connected sounds (ex. sounds for level, sounds with the same streamType),
  • DO NOT load the same file multiple times; DO load it once (ideally while the application is starting), store sound id and use play(soundId),
  • DO NOT leave sounds loaded in memory when no longer used; DO call release() or dispose() when sounds are no longer useful,
  • DO NOT play music files; DO use plugin that is intended for that.

1.1.1 #

  • Bugfix (iOS): stop() not working correctly sometimes

1.1.0 #

  • MacOS support
  • Bugfix (iOS): sound not played anymore after calling stop()
  • iOS: Loading sound file from URL is done in .uitility queue instead of main
  • Multiple assertions added to help debugging app

1.0.1 #

  • Homepage url fix

1.0.0 (21-02-2020) #

  • Migrated to use platform interface

0.6.1 (22-01-2020) #

  • Playback rate control ** set in advance with play() ** update while playing with setRate()

0.6.0 (04-01-2020) #

  • BREAKING CHANGE: Web support (limited)
    • Unavailable:
      • pause()/resume() functions crashes
      • streamType parameter has no effect
      • maxStreams parameter has no effect - there is no limit on simultanously played sounds

0.5.0 (20-03-2019) #

  • maxStreams parameter added to the constructor (Android only feature) (thanks to niusounds)

0.5.1

  • Gradle plugin upgraded
  • Kotlin upgraded
  • Dependency to Android Support library was removed
  • Swift version upgraded

0.5.2 (28-05-2019)

  • Handle content:// schema on Android
  • Run callbacks on UI thread

0.5.3 (03-06-2019)

  • Set Swift version in plugin's podspec

0.5.4 (04-01-2020)

  • Clear temporary files from previous session (Android)
  • Emulate maxStreams parameter on iOS

0.4.0 (04-03-2019) #

  • Methods for immediate playback after loading
  • Documentation update

0.4.1 (06-03-2019)

  • Android bugs fixed

0.3.0 (11-12-2018) #

  • Upgrade Android Gradle plugin

0.2.0 (05-11-2018) #

  • New methods for controlling audio stream:
    • stop
    • pause
    • resume
  • Fixed iOS problem with repeat parameter not working on the first play() call
  • Stream handling wrapper API

0.1.1 (16-09-2018) #

0.1.0 (10-09-2018) #

  • Breaking change
    • Multiple soundpools support
    • Audio stream type may be defined for Soundpool to use (Android only)
      • Every sound loaded for the Soundpool is played on the audio stream Soundpool instace was created with
  • Fix Android app crashing when URI is not reachable

0.0.3 (19-06-2018) #

  • Load sound files from URI
  • Fix setVolume() and release() never finishing

0.0.1 (10-06-2018) #

  • Initial release.
    • Loading sound files from assets.
    • Playing, stopping and pausing streams.
    • Releasing resources.
  • Works on Android and iOS.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:soundpool/soundpool.dart';
import 'package:flutter/services.dart';
import 'package:flutter/foundation.dart';

Soundpool _soundpool;

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  _soundpool = Soundpool();
  runApp(SimpleApp());
}

class SimpleApp extends StatefulWidget {
  SimpleApp({Key key}) : super(key: key);

  @override
  _SimpleAppState createState() => _SimpleAppState();
}

class _SimpleAppState extends State<SimpleApp> {

  int _alarmSoundStreamId;
  int _cheeringStreamId = -1;

  String get _cheeringUrl => kIsWeb ? '/c-c-1.mp3' : 'https://raw.githubusercontent.com/ukasz123/soundpool/feature/web_support/example/web/c-c-1.mp3';

  void initState(){
    _soundId = _loadSound();
    _cheeringId = _loadCheering();
  }
  double _volume = 1.0;
  double _rate = 1.0;
  Future<int> _soundId;
  Future<int> _cheeringId;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Material(
        child: Center(
          child: SizedBox(
            width: kIsWeb ? 450: double.infinity,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text('Rolling dices'),
               Row(
                 mainAxisAlignment: MainAxisAlignment.center,
                children:[
                  RaisedButton(
                    onPressed: _playSound,
                    child: Text("Play"),
                    ),
                    SizedBox(width:8),
                  RaisedButton(
                    onPressed: _pauseSound,
                    child: Text("Pause"),
                    ),
                    SizedBox(width:8),
                  RaisedButton(
                    onPressed: _stopSound,
                    child: Text("Stop"),
                    ),
                ],
              ),
                    SizedBox(height:8),
              RaisedButton(
            onPressed: _playCheering,
            child: Text("Play cheering"),
          ),
            SizedBox(height: 4),
              Text('Set rate '),
              Row(children:[Expanded(child:Slider.adaptive(
                min: 0.5, max: 2.0,
                value: _rate,
                onChanged: (newRate){
                  setState((){_rate = newRate;});
                  _updateCheeringRate();
                },
              ),), Text('${_rate.toStringAsFixed(3)}'),]),
              SizedBox(height: 8.0),
              Text('Volume'),
            Slider.adaptive(
              value: _volume,
              onChanged:(newVolume) {
                setState((){_volume = newVolume;});
                _updateVolume(newVolume);
              }
            ),
          ],
          ),
          ),
        ),
      ),
    );
  }

  Future<int> _loadSound() async {
    var asset = await rootBundle.load("sounds/do-you-like-it.wav");
      return await _soundpool.load(asset);
  }

  Future<int> _loadCheering() async {
    return await _soundpool.loadUri(_cheeringUrl);
  }

  Future<void> _playSound() async {
    var _alarmSound = await _soundId;
    _alarmSoundStreamId = await _soundpool.play(_alarmSound);
  }

  Future<void> _pauseSound() async {
    if (_alarmSoundStreamId != null){
      await _soundpool.pause(_alarmSoundStreamId);
    }
  }

  Future<void> _stopSound() async {
    if (_cheeringStreamId != null){
      await _soundpool.stop(_cheeringStreamId);
    }
  }

  Future<void> _playCheering() async {
    
    var _sound = await _cheeringId;
    _cheeringStreamId = await _soundpool.play(_sound, rate: _rate,);
  }

  Future<void> _updateCheeringRate() async {
    if (_cheeringStreamId > 0){
      await _soundpool.setRate(streamId: _cheeringStreamId, playbackRate: _rate);
    }
  }

  Future<void> _updateVolume(newVolume) async{
    // if (_alarmSound >= 0){
      var _cheeringSound = await _cheeringId;
      _soundpool.setVolume(soundId: _cheeringSound, volume: newVolume);
    // }
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  soundpool: ^1.1.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:soundpool/soundpool.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
93
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
96
Learn more about scoring.

We analyzed this package on Jul 2, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform linux

Because of import path [package:soundpool/soundpool.dart] that declares support for platforms: android, ios, macos, web

Package does not support Flutter platform windows

Because of import path [package:soundpool/soundpool.dart] that declares support for platforms: android, ios, macos, web

Package not compatible with SDK dart

because of import path [soundpool] that is in a package requiring null.

Health suggestions

Format lib/soundpool.dart.

Run flutter format to format lib/soundpool.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
soundpool_macos ^0.2.3 0.2.3
soundpool_platform_interface ^1.0.1 1.0.1
soundpool_web ^1.0.1 1.0.3
Transitive dependencies
charcode 1.1.3
collection 1.14.12 1.14.13
flutter_web_plugins 0.0.0
http 0.12.1
http_parser 3.1.4
meta 1.1.8
path 1.7.0
pedantic 1.9.0 1.9.1
plugin_platform_interface 1.0.2
sky_engine 0.0.99
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6 1.2.0
vector_math 2.0.8