soundpool 2.0.0-nullsafety.0 copy "soundpool: ^2.0.0-nullsafety.0" to clipboard
soundpool: ^2.0.0-nullsafety.0 copied to clipboard

outdated

A Flutter Sound Pool for playing short audio files. Sound tracks are cached in memory and played directly from cache.

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

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(
    SoundpoolInitializer()
    );
}

class SoundpoolInitializer extends StatefulWidget {
  @override
  _SoundpoolInitializerState createState() => _SoundpoolInitializerState();

}

class _SoundpoolInitializerState extends State<SoundpoolInitializer> {
  Soundpool? _pool;

  @override
  void initState(){
    super.initState();
    if (!kIsWeb) {
      _pool = Soundpool();
    } 
  }

  @override
  Widget build(BuildContext context) {
    if (_pool == null){
      return MaterialApp(
      home: Material(
        child: Center(
          child: RaisedButton(
                    onPressed: () => setState(()=> _pool = Soundpool()),
                    child: Text("Init Soundpool"),
                    ),
        ),
      ),
      );
    } else {
      return SimpleApp(pool: _pool!);
    }
  }
}

class SimpleApp extends StatefulWidget {
  final Soundpool pool;
  SimpleApp({Key? key, required this.pool}) : 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';

  Soundpool get _soundpool => widget.pool;

  void initState(){
    _soundId = _loadSound();
    _cheeringId = _loadCheering();
    super.initState();
  }
  double _volume = 1.0;
  double _rate = 1.0;
  late Future<int> _soundId;
  late 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 (_alarmSoundStreamId != null){
      await _soundpool.stop(_alarmSoundStreamId!);
    }
  }

  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);
    // }
  }
}
227
likes
0
pub points
96%
popularity

Publisher

verified publisherukaszapps.pl

A Flutter Sound Pool for playing short audio files. Sound tracks are cached in memory and played directly from cache.

Homepage
Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter, soundpool_macos, soundpool_platform_interface, soundpool_web

More

Packages that depend on soundpool