soundpool 0.6.0 copy "soundpool: ^0.6.0" to clipboard
soundpool: ^0.6.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';

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;

  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;
  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"),
          ),
            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;
    _soundpool.play(_sound);
  }

  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.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter, flutter_web_plugins, http

More

Packages that depend on soundpool