嘿,我想要我的Flutter应用程序背景音乐,在那里我导入了这个包
-导入'package:audioplayers / audio_cache.dart';
-导入'package:audioplayers / audioplayers.dart';
我能够播放音乐,但是总是可以,如果我再次按下凸起的按钮,则音乐会重新开始播放,因此它可以多次播放,有人知道我可以解决这个问题,这是我代码中的重要部分。
class _MyHomepageState extends State<MyHomepage> {
AudioPlayer player = AudioPlayer();
AudioCache cache = new AudioCache();
bool isPlaying = false;
Future<bool> _willPopCallback() async {
if (isPlaying == false) {
setState(() {
isPlaying = true;
});
player.stop();
}
return true;
}
openingActions() async {
player = await cache.loop('audio/test.mp3');
}
@override
Widget build(BuildContext context) {
openingActions();
return WillPopScope(
onWillPop: () => _willPopCallback(),
child: Scaffold(
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/background.jpg'),
fit: BoxFit.cover,
),
),