I'm trying to add background music to my app. if i start my app the music starts rightly, but if i press a button which should have no impact to the music, the music starts from new. i code in Flutter.Here is my code i cutted the unimportant things away.
import 'package:audioplayers/audio_cache.dart';
import '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,
),
),
...
...
...
raisedbutton(
....
)
You can copy paste run full code below
You can move
openingActions();frombuildtoinitStateAnd
rebuildwill not callopeningActions();againfull code