Commit 2ee6209a6a06435a23e9651beab6c180127ebca7
1 parent
46c68b45
feat:绘本播放状态优化
Showing
1 changed file
with
33 additions
and
17 deletions
lib/pages/reading/bloc/reading_bloc.dart
| ... | ... | @@ -95,7 +95,7 @@ class ReadingPageBloc |
| 95 | 95 | //音频播放器 |
| 96 | 96 | audioPlayer = AudioPlayer(); |
| 97 | 97 | audioPlayer.onPlayerStateChanged.listen((event) async { |
| 98 | - debugPrint('播放状态变化'); | |
| 98 | + Log.d("播放状态变化 event=$event"); | |
| 99 | 99 | if (event == PlayerState.completed) { |
| 100 | 100 | debugPrint('播放完成'); |
| 101 | 101 | _voicePlayState = VoicePlayState.completed; |
| ... | ... | @@ -104,13 +104,18 @@ class ReadingPageBloc |
| 104 | 104 | if (event == PlayerState.stopped) { |
| 105 | 105 | debugPrint('播放结束'); |
| 106 | 106 | _voicePlayState = VoicePlayState.stop; |
| 107 | - _onAudioPlayComplete(); | |
| 108 | 107 | } |
| 109 | 108 | |
| 110 | 109 | if (event == PlayerState.playing) { |
| 111 | 110 | debugPrint('正在播放中'); |
| 112 | 111 | _voicePlayState = VoicePlayState.playing; |
| 113 | 112 | } |
| 113 | + | |
| 114 | + if (event == PlayerState.disposed) { | |
| 115 | + debugPrint('播放器释放'); | |
| 116 | + _voicePlayState = VoicePlayState.unKnow; | |
| 117 | + } | |
| 118 | + | |
| 114 | 119 | if (isClosed) { |
| 115 | 120 | return; |
| 116 | 121 | } |
| ... | ... | @@ -181,7 +186,7 @@ class ReadingPageBloc |
| 181 | 186 | Emitter<ReadingPageState> emitter) async { |
| 182 | 187 | _currentPage = event.pageIndex; |
| 183 | 188 | _voiceXsCancel(); |
| 184 | - _playOriginalAudioInner(null); | |
| 189 | + _playOriginalAudioInner(null, forcePlay: true); | |
| 185 | 190 | emitter(CurrentPageIndexState()); |
| 186 | 191 | } |
| 187 | 192 | |
| ... | ... | @@ -218,20 +223,27 @@ class ReadingPageBloc |
| 218 | 223 | } |
| 219 | 224 | |
| 220 | 225 | ///播放原音音频 |
| 221 | - void _playOriginalAudioInner(String? audioUrl) async { | |
| 226 | + /// - [force]: 是否强制播放(true:不管当前状态如何,都会播放目标原音音频,比如翻页场景) | |
| 227 | + /// (false:如果正在播放,暂停播放,比如点击播放按钮场景) | |
| 228 | + void _playOriginalAudioInner(String? audioUrl, {bool forcePlay = false}) async { | |
| 222 | 229 | if (_isRecordAudioPlaying) { |
| 230 | + await audioPlayer.stop(); | |
| 223 | 231 | _isRecordAudioPlaying = false; |
| 224 | 232 | } |
| 225 | 233 | Log.d( |
| 226 | 234 | "_playOriginalAudio _isRecordAudioPlaying=$_isRecordAudioPlaying _isOriginAudioPlaying=$_isOriginAudioPlaying url=$audioUrl"); |
| 227 | 235 | if (_isOriginAudioPlaying) { |
| 228 | - _isOriginAudioPlaying = false; | |
| 229 | 236 | await audioPlayer.stop(); |
| 237 | + _isOriginAudioPlaying = false; | |
| 238 | + if (forcePlay) { | |
| 239 | + audioUrl ??= currentPageData()?.audioUrl ?? ''; | |
| 240 | + await _playAudio(audioUrl); | |
| 241 | + _isOriginAudioPlaying = true; | |
| 242 | + } | |
| 230 | 243 | } else { |
| 231 | - _isOriginAudioPlaying = true; | |
| 232 | - Log.d("_playOriginalAudioInner _isOriginAudioPlaying: true"); | |
| 233 | 244 | audioUrl ??= currentPageData()?.audioUrl ?? ''; |
| 234 | - _playAudio(audioUrl); | |
| 245 | + await _playAudio(audioUrl); | |
| 246 | + _isOriginAudioPlaying = true; | |
| 235 | 247 | } |
| 236 | 248 | } |
| 237 | 249 | |
| ... | ... | @@ -242,23 +254,27 @@ class ReadingPageBloc |
| 242 | 254 | } |
| 243 | 255 | |
| 244 | 256 | Future<void> _playRecordAudioInner() async { |
| 257 | + Log.d( | |
| 258 | + "_playRecordAudioInner _isOriginAudioPlaying=$_isOriginAudioPlaying _isRecordAudioPlaying=$_isRecordAudioPlaying url=${currentPageData() | |
| 259 | + ?.recordUrl}"); | |
| 245 | 260 | if (_isOriginAudioPlaying) { |
| 261 | + ///如果正在播放原音,暂停 | |
| 262 | + await audioPlayer.stop(); | |
| 246 | 263 | _isOriginAudioPlaying = false; |
| 247 | 264 | } |
| 248 | - Log.d( | |
| 249 | - "_playRecordAudioInner _isRecordAudioPlaying=$_isRecordAudioPlaying url=${currentPageData()?.recordUrl}"); | |
| 265 | + | |
| 250 | 266 | if (_isRecordAudioPlaying) { |
| 251 | - _isRecordAudioPlaying = false; | |
| 252 | 267 | await audioPlayer.stop(); |
| 268 | + _isRecordAudioPlaying = false; | |
| 253 | 269 | } else { |
| 254 | - _isRecordAudioPlaying = true; | |
| 255 | 270 | final recordAudioUrl = currentPageData()?.recordUrl; |
| 256 | - _playAudio(recordAudioUrl); | |
| 271 | + await _playAudio(recordAudioUrl); | |
| 272 | + _isRecordAudioPlaying = true; | |
| 257 | 273 | } |
| 258 | 274 | // emitter(VoicePlayStateChange()); |
| 259 | 275 | } |
| 260 | 276 | |
| 261 | - void _playAudio(String? audioUrl) async { | |
| 277 | + Future<void> _playAudio(String? audioUrl) async { | |
| 262 | 278 | if (audioUrl!.isNotEmpty) { |
| 263 | 279 | await audioPlayer.play(UrlSource(audioUrl)); |
| 264 | 280 | } |
| ... | ... | @@ -302,10 +318,11 @@ class ReadingPageBloc |
| 302 | 318 | ///先声测试 |
| 303 | 319 | void _voiceXsStart( |
| 304 | 320 | XSVoiceStartEvent event, Emitter<ReadingPageState> emitter) async { |
| 305 | - _stopAudio(); | |
| 321 | + await _stopAudio(); | |
| 306 | 322 | startRecord(event.content); |
| 307 | 323 | } |
| 308 | 324 | |
| 325 | + ///开始录音 | |
| 309 | 326 | void startRecord(String content) async { |
| 310 | 327 | // 调用封装好的权限检查和请求方法 |
| 311 | 328 | bool result = |
| ... | ... | @@ -374,7 +391,6 @@ class ReadingPageBloc |
| 374 | 391 | Log.d( |
| 375 | 392 | "_onAudioPlayComplete _isOriginAudioPlaying=$_isOriginAudioPlaying _voicePlayState=$_voicePlayState recordUrl=${currentPageData()?.recordUrl?.isNotEmpty}"); |
| 376 | 393 | if (_isOriginAudioPlaying && |
| 377 | - _voicePlayState == VoicePlayState.completed && | |
| 378 | 394 | currentPageData()?.recordUrl?.isNotEmpty != true) { |
| 379 | 395 | ///如果刚刚完成原音播放&&录音为空,则开始录音 |
| 380 | 396 | startRecord(currentPageData()?.word ?? ''); |
| ... | ... | @@ -384,7 +400,7 @@ class ReadingPageBloc |
| 384 | 400 | _isRecordAudioPlaying = false; |
| 385 | 401 | } |
| 386 | 402 | |
| 387 | - void _stopAudio() async { | |
| 403 | + Future<void> _stopAudio() async { | |
| 388 | 404 | await audioPlayer.stop(); |
| 389 | 405 | _isOriginAudioPlaying = false; |
| 390 | 406 | _isRecordAudioPlaying = false; | ... | ... |