soundpool 0.1.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 84

soundpool

A Flutter Sound Pool for playing short media files.

Inspired by Android SoundPool API.

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);

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.

0.0.3 (19-06-2018)

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

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.1.1 (16-09-2018)

  • Fix type cast error in play() (thanks to SpencerCornish)

example/lib/main.dart

import 'package:flutter/material.dart';

import 'dart:async';
import 'package:flutter/services.dart';
import 'package:soundpool/soundpool.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List<Soundpool> soundpools = [];
  Map<Soundpool, SoundsMap> soundsMap;
  Soundpool _selectedPool;
  int _selectedIndex = 0;

  @override
  initState() {
    super.initState();
    initSoundPools();
  }

  @override
  Widget build(BuildContext context) {
    final ready = (soundpools.length > 0) && _selectedPool != null;
    final Widget body =
        ready ? _buildReadyWidget(context) : _buildWaitingWidget(context);
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text('Soundpool plugin example app'),
          actions: <Widget>[
            new IconButton(
              icon: Icon(Icons.clear_all),
              tooltip: "Releases all sounds",
              onPressed: () {
                resetSoundpool();
              },
            )
          ],
        ),
        bottomNavigationBar: _buildBottomNavigationBar(context),
        body: body,
        floatingActionButton: ready
            ? new FloatingActionButton(
                onPressed: soundsMap[_selectedPool].dicesSoundId != null &&
                        soundsMap[_selectedPool].dicesSoundId >= 0
                    ? playSound
                    : null,
                child: new Icon(Icons.play_circle_filled),
              )
            : null,
      ),
    );
  }

  Stack _buildReadyWidget(BuildContext context) {
    return new Stack(
      children: <Widget>[
        Positioned(
          top: 8.0,
          child: Text(_selectedPool.streamType.toString()),
        ),
        Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            new Padding(
              padding: const EdgeInsets.symmetric(horizontal: 16.0),
              child: new Text('Volume control'),
            ),
            new Slider(
                value: soundsMap[_selectedPool].volume,
                onChanged: (newValue) {
                  setState(() {
                    soundsMap[_selectedPool].volume = newValue;
                    updateVolume();
                  });
                }),
          ],
        ),
        new Positioned(
            bottom: 16.0,
            left: 16.0,
            child: new FloatingActionButton(
              onPressed: soundsMap[_selectedPool].dicesSoundFromUriId == null ||
                      soundsMap[_selectedPool].dicesSoundFromUriId < 0
                  ? null
                  : () => playSoundFromUri(),
              child: new Icon(Icons.play_arrow),
            ))
      ],
    );
  }

  void initSoundPools() {
    soundpools =
        StreamType.values.map((type) => Soundpool(streamType: type)).toList();

    soundsMap = Map.fromEntries(
        soundpools.map((soundpool) => MapEntry(soundpool, SoundsMap())));
    print("Waiting for all pools to initialize themself");
    Future.wait(
            soundpools.map((pool) => initSoundsForPool(pool, soundsMap[pool])))
        .then((_) {
      setState(() {
        _selectedPool = soundpools[_selectedIndex];
      });
    });
  }

  void playSound() async {
    if (soundsMap[_selectedPool].dicesSoundId > -1) {
      int streamId = soundsMap[_selectedPool].dicesStreamId = await _selectedPool
          .play(soundsMap[_selectedPool].dicesSoundId, repeat: 4);
      print("Playing sound with stream id: $streamId");
    }
  }

  void updateVolume() {
    if (soundsMap[_selectedPool].dicesStreamId != null) {
      _selectedPool.setVolume(
          streamId: soundsMap[_selectedPool].dicesStreamId,
          soundId: soundsMap[_selectedPool].dicesSoundId,
          volume: soundsMap[_selectedPool].volume);
    } else {
      _selectedPool.setVolume(
          soundId: soundsMap[_selectedPool].dicesSoundId,
          volume: soundsMap[_selectedPool].volume);
    }
  }

  void playSoundFromUri() {
    _selectedPool.play(soundsMap[_selectedPool].dicesSoundFromUriId);
  }

  void resetSoundpool() async {
    setState(() {
      soundsMap[_selectedPool].dicesSoundId = -1;
      soundsMap[_selectedPool].dicesSoundFromUriId = -1;
    });
    _selectedPool.dispose();
    initSoundsForPool(_selectedPool, soundsMap[_selectedPool]).then((_) {
      setState(() {});
    });
  }

  _buildBottomNavigationBar(BuildContext context) {
    if (soundpools.length > 1) {
      return BottomNavigationBar(
        items: soundpools
            .map((s) => s.streamType)
            .map((streamType) => BottomNavigationBarItem(
                backgroundColor: Colors.lightBlueAccent,
                icon: Icon(Icons.pages),
                title: Text(streamType.toString())))
            .toList(),
        currentIndex: _selectedIndex,
        onTap: (index) {
          setState(() {
            _selectedIndex = index;
            _selectedPool = soundpools[_selectedIndex];
          });
        },
      );
    } else {
      return new Container(
        height: 40.0,
        child: Center(
          child: Text("Loading soundpools"),
        ),
      );
    }
  }

  Widget _buildWaitingWidget(BuildContext context) {
    return Center(child: CircularProgressIndicator());
  }

  Future<Null> initSoundsForPool(Soundpool pool, SoundsMap sounds) async {
    print("Loading sounds for pool ${pool.streamType}...");
    sounds.dicesSoundId =
        await rootBundle.load("sounds/dices.m4a").then((ByteData soundData) {
      return pool.load(soundData);
    });
    await pool.setVolume(soundId: sounds.dicesSoundId, volume: sounds.volume);
    sounds.dicesSoundFromUriId = await pool.loadUri(
        "https://github.com/ukasz123/soundpool/raw/master/example/sounds/dices.m4a");
    print(
        "stream = ${pool.streamType}: dicesSoundId = ${sounds.dicesSoundId}, dicesSoundFromUri = ${sounds.dicesSoundFromUriId}");
    return;
  }
}

class SoundsMap {
  int dicesSoundId;

  int dicesStreamId;

  int dicesSoundFromUriId;

  double volume = 1.0;
}

Use this package as a library

1. Depend on it

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


dependencies:
  soundpool: ^0.1.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages 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';
  
Version Uploaded Documentation Archive
0.1.1 Sep 16, 2018 Go to the documentation of soundpool 0.1.1 Download soundpool 0.1.1 archive
0.1.0 Sep 10, 2018 Go to the documentation of soundpool 0.1.0 Download soundpool 0.1.0 archive
0.0.3+4 Aug 27, 2018 Go to the documentation of soundpool 0.0.3+4 Download soundpool 0.0.3+4 archive
0.0.3+3 Jun 19, 2018 Go to the documentation of soundpool 0.0.3+3 Download soundpool 0.0.3+3 archive
0.0.1+2 Jun 11, 2018 Go to the documentation of soundpool 0.0.1+2 Download soundpool 0.0.1+2 archive
0.0.1+1 Jun 10, 2018 Go to the documentation of soundpool 0.0.1+1 Download soundpool 0.0.1+1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
77
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
84
Learn more about scoring.

We analyzed this package on Oct 10, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.4
  • Flutter: 0.9.5

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Maintenance suggestions

The description is too short. (-20 points)

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.48.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8