用audioplayers插件播放本地音乐总是出错,查到可以使用AudioCache播放,但是又没有了停止和暂停功能,尝试了一下,用以下办法实现
AudioPlayer _audioPlayer;
AudioCache _audioCache;
mode = PlayerMode.MEDIA_PLAYER;
_audioPlayer = AudioPlayer(mode: mode);
_audioCache = AudioCache(prefix: ‘audio/’);
//关键的一句在这里
_audioCache.fixedPlayer = _audioPlayer;
//播放的时候用_audioCache.play.
void _cacheplay() async {
final result = await _audioCache.play(‘pfzl.mp3’, isNotification: true);
if (result == 1) {
print(‘succes’);
}
}
//暂停和停止是用_audioPlayer来实现
//暂停
void _pause() async {
final result = await _audioPlayer.pause();
if (result == 1) {
print(‘succes’);
}
}
//停止播放
_stop() async {
final result = await _audioPlayer.stop();
if (result == 1) {
setState(() {
_position = Duration();
});
}
}