sound.dart 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import 'dart:async';
  2. //import 'package:flutter/services.dart';
  3. import 'package:audioplayers/audioplayers.dart';
  4. //import 'package:soundpool/soundpool.dart';
  5. import 'game_setting.dart';
  6. class Sound {
  7. static const move = 'move2.wav';
  8. static const capture = 'capture2.wav';
  9. static const check = 'check2.wav';
  10. static const click = 'click.wav';
  11. static const newGame = 'newgame.wav';
  12. static const loose = 'loss.wav';
  13. static const win = 'win.wav';
  14. static const draw = 'draw.wav';
  15. static const illegal = 'illegal.wav';
  16. static AudioPlayer audioPlayer = AudioPlayer()
  17. ..audioCache = AudioCache(prefix: 'assets/sounds/');
  18. // static final Soundpool pool = Soundpool.fromOptions(
  19. // options: const SoundpoolOptions(streamType: StreamType.notification));
  20. static GameSetting? setting;
  21. static Future<bool> play(String id) async {
  22. setting ??= await GameSetting.getInstance();
  23. if (!setting!.sound) return false;
  24. await audioPlayer.play(AssetSource(id), volume: setting!.soundVolume);
  25. // final soundId = await loadAsset(id);
  26. // await pool.play(soundId);
  27. return true;
  28. }
  29. // static final Map<String, Completer<int>> _loaders = {};
  30. // static Future<int> loadAsset(String id) async {
  31. // if (_loaders.containsKey(id)) {
  32. // return _loaders[id]!.future;
  33. // }
  34. // _loaders[id] = Completer<int>();
  35. // rootBundle.load("assets/sounds/$id").then((value) {
  36. // _loaders[id]!.complete(pool.load(value));
  37. // });
  38. // return _loaders[id]!.future;
  39. // }
  40. }