Commit d100908ac69cec7836e8e5ed486e36bc33716e4d
1 parent
9e14f47a
feat:解决练习切后台后音频未暂停问题
Showing
1 changed file
with
18 additions
and
1 deletions
lib/pages/practice/bloc/topic_picture_bloc.dart
| ... | ... | @@ -20,6 +20,7 @@ import '../../../common/permission/permissionRequester.dart'; | 
| 20 | 20 | import '../../../common/utils/show_star_reward_dialog.dart'; | 
| 21 | 21 | import '../../../models/voice_result_type.dart'; | 
| 22 | 22 | import '../../../route/route.dart'; | 
| 23 | +import '../../../utils/log_util.dart'; | |
| 23 | 24 | |
| 24 | 25 | part 'topic_picture_event.dart'; | 
| 25 | 26 | |
| ... | ... | @@ -40,7 +41,7 @@ enum VoicePlayState { | 
| 40 | 41 | } | 
| 41 | 42 | |
| 42 | 43 | class TopicPictureBloc | 
| 43 | - extends BaseSectionBloc<TopicPictureEvent, TopicPictureState> { | |
| 44 | + extends BaseSectionBloc<TopicPictureEvent, TopicPictureState> with WidgetsBindingObserver { | |
| 44 | 45 | final PageController pageController; | 
| 45 | 46 | |
| 46 | 47 | final String courseLessonId; | 
| ... | ... | @@ -151,15 +152,31 @@ class TopicPictureBloc | 
| 151 | 152 | return; | 
| 152 | 153 | } | 
| 153 | 154 | }); | 
| 155 | + | |
| 156 | + WidgetsBinding.instance.addObserver(this); | |
| 154 | 157 | }); | 
| 155 | 158 | } | 
| 156 | 159 | |
| 157 | 160 | @override | 
| 161 | + Future<void> didChangeAppLifecycleState(AppLifecycleState state) async { | |
| 162 | + super.didChangeAppLifecycleState(state); | |
| 163 | + Log.d('TopicPictureBloc didChangeAppLifecycleState state=$state'); | |
| 164 | + if (state == AppLifecycleState.paused) { | |
| 165 | + ///切到后台暂停音频播放、录音等 | |
| 166 | + if (audioPlayer.state == PlayerState.playing) { | |
| 167 | + await audioPlayer.pause(); | |
| 168 | + } | |
| 169 | + _voiceXsCancel(); | |
| 170 | + } | |
| 171 | + } | |
| 172 | + | |
| 173 | + @override | |
| 158 | 174 | Future<void> close() { | 
| 159 | 175 | pageController.dispose(); | 
| 160 | 176 | audioPlayer.release(); | 
| 161 | 177 | audioPlayer.dispose(); | 
| 162 | 178 | _voiceXsCancel(); | 
| 179 | + WidgetsBinding.instance.removeObserver(this); | |
| 163 | 180 | return super.close(); | 
| 164 | 181 | } | 
| 165 | 182 | ... | ... | 
