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 .
import 'dart:async';import 'package:flutter/material.dart';import 'package:flutter/services.dart';import 'package:soundpool/soundpool.dart';class Sound extends StatefulWidget { final Widget child; const Sound({Key key, this.child}) : super(key: key); @override SoundState createState() => SoundState(); static SoundState of(BuildContext context) { final state = context.ancestorStateOfType(const TypeMatcher()); assert(state != null, 'can not find Sound widget'); return state; }}const _SOUNDS = [ 'clean.mp3', 'drop.mp3', 'explosion.mp3', 'move.mp3', 'rotate.mp3', 'start.mp3'];class SoundState extends State { Soundpool _pool; Map _soundIds; bool mute = false; void _play(String name) { final soundId = _soundIds[name]; if (soundId != null && !mute) { _pool.play(soundId); } } @override void initState() { super.initState(); _pool = Soundpool(streamType: StreamType.music, maxStreams: 4); _soundIds = Map(); for (var value in _SOUNDS) { scheduleMicrotask(() async { final data = await rootBundle.load('assets/audios/$value'); _soundIds[value] = await _pool.load(data); }); } } @override void dispose() { super.dispose(); _pool.dispose(); } @override Widget build(BuildContext context) { return widget.child; } void start() { _play('start.mp3'); } void clear() { _play('clean.mp3'); } void fall() { _play('drop.mp3'); } void rotate() { _play('rotate.mp3'); } void move() { _play('move.mp3'); }}
class PlaySound { final _SOUNDS = [ 'clean.mp3', 'drop.mp3', 'explosion.mp3', 'move.mp3', 'rotate.mp3', 'start.mp3' ]; Map soundIds = Map(); play(String name){ soundpool.play(soundIds[name]); } Soundpool soundpool = Soundpool(streamType: StreamType.music, maxStreams: 4); PlaySound(){ scheduleMicrotask(()async{ for(var value in _SOUNDS){ var data = await rootBundle.load('assets/audios/$value'); soundIds[value] = await soundpool.load(data); } }); } }