Преглед изворни кода

修复切换列表背景音乐停播又重播的bug(去掉main.dart里的global key)

guoziyun пре 4 месеци
родитељ
комит
9e6c1a6b10

+ 1 - 0
CHANGELOG.md

@@ -15,4 +15,5 @@
 - 阶梯图片质量机制(三种阶梯宽度,分别为1200, 1800, 2400),低端手机采用1200,高端手机1800,平板2400, 避免直接使用原图,分辨率过大的原图进入渲染管道应是导致低端手机crash的主要原因
 - 优化 jc_audio_player, 引入线程池处理 SoundPool 播放,不堵塞UI主线程,解决发牌高频音效触发导致的ANR。
 - banner广告优化,Key 隔离,减少潜在风险
+- 修复切换到任务列表,背景音乐pause后又再次播放的bug, 提升了应用前后台切换性能(main.dart中使用了GlobalKey的原因)
 - 其他优化

+ 7 - 0
lib/audio/jc_audio_controller.dart

@@ -129,6 +129,13 @@ class JcAudioController {
 
   void startMusic() {
     _log.info('starting music');
+
+    // 如果当前生命周期不是 resumed,拒绝执行播放指令
+    if (_lifecycleNotifier?.value != AppLifecycleState.resumed) {
+      _log.warning('Attempted to start music while app is in background/inactive. Ignored.');
+      return;
+    }
+
     final musicOn = _settings?.music.value ?? false;
     if (!musicOn) {
       _log.info(() => 'Ignoring playing music because music are turned off.');

+ 6 - 1
lib/homepage/home_screen.dart

@@ -113,7 +113,12 @@ class _HomeScreen extends AdsState<HomeScreen> with TickerProviderStateMixin {
       TweenSequenceItem(tween: Tween<double>(begin: 1.4, end: 1.0).chain(CurveTween(curve: Curves.easeIn)), weight: 60.0),
     ]).animate(_collectionController);
 
-    audio.startMusic();
+    // 只有在应用是 resumed 状态且当前页面在前台时才自动播
+    WidgetsBinding.instance.addPostFrameCallback((_) {
+      if (WidgetsBinding.instance.lifecycleState == AppLifecycleState.resumed && mounted) {
+        audio.startMusic();
+      }
+    });
   }
 
   // 首页初始化之后的跳转,首次运行直接进入play页面,上次从play页面退出有缓存存在也跳转到play页面

+ 1 - 1
lib/main.dart

@@ -181,7 +181,7 @@ class MyApp extends StatelessWidget {
 
         child: Prepare(
           child: MaterialApp(
-            key: GlobalKey(),
+            // key: GlobalKey(),
             title: 'Jigsort Solitaire',
             // initialRoute: firstRun ? '/play' : '/', // 首次游戏直接进入游戏页面,而不是合集页
             initialRoute: '/', // 统一先到HomeScreen, 再根据情况跳转到相应页面

+ 3 - 2
lib/settings/settings_screen.dart

@@ -139,14 +139,15 @@ class _SettingScreenState extends State<SettingScreen> {
   Widget build(BuildContext context) {
     final audio = context.read<JcAudioController>();
     return Scaffold(
+      backgroundColor: SkinHelper.colorWhite,
       appBar: AppBar(
-        backgroundColor: Colors.white,
+        backgroundColor: SkinHelper.colorWhite,
         centerTitle: true,
         title: Text(
           AppLocalizations.of(context)!.setting,
           style: TextStyle(color: Colors.black87, fontWeight: FontWeight.bold, fontSize: 24),
         ),
-        elevation: 1,
+        // elevation: 1,
         leading: IconButton(
           icon: const Icon(Icons.arrow_back_outlined, color: Colors.black87),
           onPressed: () {