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