From 8fb5f8606a35fb4b298ad45468c9e3c205f52122 Mon Sep 17 00:00:00 2001 From: wuqifeng <540416539@qq.com> Date: Mon, 5 Aug 2024 00:42:49 +0800 Subject: [PATCH] feat:先声录音状态优化 --- lib/pages/practice/bloc/topic_picture_bloc.dart | 65 ++++++++++++++++++++++++----------------------------------------- lib/pages/practice/bloc/topic_picture_event.dart | 3 +++ lib/pages/practice/topic_picture_page.dart | 6 +++--- lib/pages/reading/bloc/reading_bloc.dart | 3 ++- 4 files changed, 32 insertions(+), 45 deletions(-) diff --git a/lib/pages/practice/bloc/topic_picture_bloc.dart b/lib/pages/practice/bloc/topic_picture_bloc.dart index 08455b5..bb07bbd 100644 --- a/lib/pages/practice/bloc/topic_picture_bloc.dart +++ b/lib/pages/practice/bloc/topic_picture_bloc.dart @@ -26,20 +26,6 @@ part 'topic_picture_event.dart'; part 'topic_picture_state.dart'; -enum VoicePlayState { - ///未知 - unKnow, - - ///播放中 - playing, - - ///播放完成 - completed, - - ///播放终止 - stop -} - class TopicPictureBloc extends BaseSectionBloc with WidgetsBindingObserver { final PageController pageController; @@ -57,9 +43,6 @@ class TopicPictureBloc ///正在评测 bool _isRecording = false; - ///正在播放音频 - VoicePlayState _voicePlayState = VoicePlayState.unKnow; - int get currentPage => _currentPage + 1; /// 选择题选中项 @@ -67,8 +50,6 @@ class TopicPictureBloc bool get isRecording => _isRecording; - VoicePlayState get voicePlayState => _voicePlayState; - late MethodChannel methodChannel; late AudioPlayer audioPlayer; @@ -77,6 +58,8 @@ class TopicPictureBloc final Color? moduleColor; + final String TAG = 'TopicPictureBloc'; + TopicPictureBloc( this.context, this.pageController, this.courseLessonId, this.moduleColor) : super(TopicPictureInitial()) { @@ -90,24 +73,12 @@ class TopicPictureBloc on(_voiceXsStart); on(_voiceXsStop); on(_questionVoicePlay); + on(_onVoiceXsStateChange); on((event, emit) { //音频播放器 audioPlayer = AudioPlayer(); audioPlayer.onPlayerStateChanged.listen((event) async { - debugPrint('播放状态变化 _voicePlayState=$_voicePlayState event=$event'); - if (event == PlayerState.completed) { - debugPrint('播放完成'); - _voicePlayState = VoicePlayState.completed; - } - if (event == PlayerState.stopped) { - debugPrint('播放结束'); - _voicePlayState = VoicePlayState.stop; - } - - if (event == PlayerState.playing) { - debugPrint('正在播放中'); - _voicePlayState = VoicePlayState.playing; - } + Log.d('$TAG onPlayerStateChanged event=$event'); if (isClosed) { return; } @@ -130,16 +101,20 @@ class TopicPictureBloc if (kDebugMode) { print('评测开始'); } + _isRecording = true; + add(OnXSVoiceStateChangeEvent()); return; } - if (call.method == 'voiceEnd') { + if (call.method == 'voiceEnd' || call.method == 'voiceCancel') { await audioPlayer.setAudioContext(AudioContext()); await audioPlayer.setBalance(0.0); //评测结束 if (kDebugMode) { - print('评测结束'); + print(call.method == 'voiceEnd' ? '评测结束' : '评测取消'); } + _isRecording = false; + add(OnXSVoiceStateChangeEvent()); return; } @@ -148,6 +123,8 @@ class TopicPictureBloc await audioPlayer.setAudioContext(AudioContext()); await audioPlayer.setBalance(0.0); + _isRecording = false; + add(OnXSVoiceStateChangeEvent()); EasyLoading.showToast('评测失败'); return; } @@ -157,10 +134,15 @@ class TopicPictureBloc }); } + void _onVoiceXsStateChange(OnXSVoiceStateChangeEvent event, + Emitter emitter) async { + emitter(XSVoiceTestState()); + } + @override Future didChangeAppLifecycleState(AppLifecycleState state) async { super.didChangeAppLifecycleState(state); - Log.d('TopicPictureBloc didChangeAppLifecycleState state=$state'); + Log.d('$TAG didChangeAppLifecycleState state=$state'); if (state == AppLifecycleState.paused) { ///切到后台暂停音频播放、录音等 if (audioPlayer.state == PlayerState.playing) { @@ -197,7 +179,7 @@ class TopicPictureBloc void _pageControllerChange(CurrentPageIndexChangeEvent event, Emitter emitter) async { await pageResetIfNeed(); - debugPrint('翻页 $_currentPage->${event.pageIndex}'); + Log.d('$TAG _pageControllerChange $_currentPage->${event.pageIndex}'); if (_currentPage == _entity?.topics?.length) { return; } @@ -276,8 +258,6 @@ class TopicPictureBloc 'type': event.type, 'userId': event.userId.toString() }); - _isRecording = true; - emitter(XSVoiceTestState()); } } @@ -321,6 +301,10 @@ class TopicPictureBloc emitter(VoicePlayStateChange()); } + bool isAudioPlaying() { + return audioPlayer.state == PlayerState.playing; + } + // 题目音频播放 void _questionVoicePlay( VoicePlayEvent event, Emitter emitter) async { @@ -335,7 +319,6 @@ class TopicPictureBloc Future pageResetIfNeed() async { _optionSelectItem = -1; _isRecording = false; - _voicePlayState = VoicePlayState.stop; await closePlayerResource(); await _voiceXsCancel(); @@ -347,7 +330,7 @@ class TopicPictureBloc } Future closePlayerResource() async { - if (voicePlayState == VoicePlayState.playing) { + if (isAudioPlaying()) { await audioPlayer.stop(); } } diff --git a/lib/pages/practice/bloc/topic_picture_event.dart b/lib/pages/practice/bloc/topic_picture_event.dart index 054051c..11a4d65 100644 --- a/lib/pages/practice/bloc/topic_picture_event.dart +++ b/lib/pages/practice/bloc/topic_picture_event.dart @@ -30,6 +30,9 @@ class XSVoiceResultEvent extends TopicPictureEvent { XSVoiceResultEvent(this.message); } +///先声评测状态 +class OnXSVoiceStateChangeEvent extends TopicPictureEvent {} + ///音频播放状态变化 class VoicePlayStateChangeEvent extends TopicPictureEvent {} diff --git a/lib/pages/practice/topic_picture_page.dart b/lib/pages/practice/topic_picture_page.dart index 7eaa996..fd896a6 100644 --- a/lib/pages/practice/topic_picture_page.dart +++ b/lib/pages/practice/topic_picture_page.dart @@ -311,7 +311,7 @@ class _TopicPicturePage extends StatelessWidget { children: [ SpeakerWidget( isPlaying: isCurrentPage && - bloc.voicePlayState == VoicePlayState.playing, + bloc.isAudioPlaying(), // 控制动画播放 width: 32.w, height: 32.w, @@ -392,7 +392,7 @@ class _TopicPicturePage extends StatelessWidget { children: [ SpeakerWidget( isPlaying: isCurrentPage && - bloc.voicePlayState == VoicePlayState.playing, + bloc.isAudioPlaying(), width: 32.w, height: 32.w, onTap: () { @@ -505,7 +505,7 @@ class _TopicPicturePage extends StatelessWidget { children: [ SpeakerWidget( isPlaying: isCurrentPage && - bloc.voicePlayState == VoicePlayState.playing, + bloc.isAudioPlaying(), // 控制动画播放 isClickable: !bloc.isRecording, // 控制是否可点击 diff --git a/lib/pages/reading/bloc/reading_bloc.dart b/lib/pages/reading/bloc/reading_bloc.dart index 6049b0d..dafe15f 100644 --- a/lib/pages/reading/bloc/reading_bloc.dart +++ b/lib/pages/reading/bloc/reading_bloc.dart @@ -170,10 +170,11 @@ class ReadingPageBloc if (call.method == 'voiceFail') { //评测失败 - _isRecording = false; await audioPlayer.setAudioContext(AudioContext()); await audioPlayer.setBalance(0.0); + _isRecording = false; + add(OnXSVoiceStateChangeEvent()); EasyLoading.showToast('评测失败'); return; } -- libgit2 0.22.2