gpt4 book ai didi

flutter - flutter 使用音频播放器最小化时停止音乐

转载 作者:行者123 更新时间:2023-12-03 04:18:30 26 4
gpt4 key购买 nike

在我的应用中,我正在循环播放音乐(本地),除非用户停止播放,否则音乐会连续播放。我正在使用audioplayers包。

Future playLoop(String filePath) async {
player.stop();
player = await cache.loop(filePath);
}
当前,当应用程序最小化时,音乐不会停止。我要实现的功能是,当应用程序最小化时,它应停止在后台播放音乐。
提前致谢。

最佳答案

Solutions :

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.paused) {
//stop your audio player
}else{
print(state.toString());
}
}

@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}

There are mainly 4 states for it:

resumed: The application is visible and responding to user input.

inactive: The application is in an inactive state and is not receivinguser input.

paused: The application is not currently visible to the user, notresponding user input, and running in the background.

detached: The application is still hosted on a flutter engine but isdetached from any host views.

关于flutter - flutter 使用音频播放器最小化时停止音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63967309/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com