我遇到了同样的问题,所以这适用于我的游戏代码,我希望这对你有用,首先,创建一个这样的控制器类:
import 'package:audioplayers/audio_cache.dart';
import 'package:audioplayers/audioplayers.dart';
void audioPlayerHandler(AudioPlayerState value) => print('state => $value');
class GameController {
static AudioPlayer audioPlayer = AudioPlayer();
static AudioCache audioCache = AudioCache();
static void play(String sound) {
if (!kIsWeb && Platform.isIOS) {
audioPlayer.monitorNotificationStateChanges(audioPlayerHandler);
}
audioCache.play(sound);
}
}
然后通过这种方式使用您的代码:
FlatButton(
onPressed: () => {
GameController.play('note1.wav');
}
child: Text('Click to ding!'),
),