diff --git a/lib/pages/practice/bloc/topic_picture_bloc.dart b/lib/pages/practice/bloc/topic_picture_bloc.dart index e9fbda3..906941b 100644 --- a/lib/pages/practice/bloc/topic_picture_bloc.dart +++ b/lib/pages/practice/bloc/topic_picture_bloc.dart @@ -24,16 +24,19 @@ part 'topic_picture_state.dart'; enum VoicePlayState { ///未知 unKnow, + ///播放中 playing, + ///播放完成 completed, + ///播放终止 stop } -class TopicPictureBloc extends BaseSectionBloc { - +class TopicPictureBloc + extends BaseSectionBloc { final PageController pageController; final String courseLessonId; @@ -68,7 +71,7 @@ class TopicPictureBloc extends BaseSectionBloc _isVoicing; - VoicePlayState get voicePlayState => _voicePlayState; + VoicePlayState get voicePlayState => _voicePlayState; late MethodChannel methodChannel; @@ -76,7 +79,8 @@ class TopicPictureBloc extends BaseSectionBloc(_pageControllerChange); on(_voicePlayStateChange); on(_voiceXsResult); @@ -90,7 +94,8 @@ class TopicPictureBloc extends BaseSectionBloc close(){ + Future close() { pageController.dispose(); audioPlayer.release(); audioPlayer.dispose(); @@ -172,7 +189,8 @@ class TopicPictureBloc extends BaseSectionBloc emitter) async { + void _requestData( + RequestDataEvent event, Emitter emitter) async { try { await loading(() async { _entity = await ListenDao.process(courseLessonId); @@ -180,13 +198,14 @@ class TopicPictureBloc extends BaseSectionBloc emitter) async { + void _pageControllerChange(CurrentPageIndexChangeEvent event, + Emitter emitter) async { await closePlayerResource(); debugPrint('翻页 $_currentPage->${event.pageIndex}'); if (_currentPage == _entity?.topics?.length) { @@ -196,7 +215,7 @@ class TopicPictureBloc extends BaseSectionBloc emitter) async { + void _selectItemLoad( + SelectItemEvent event, Emitter emitter) async { if (_forbiddenWhenCorrect) { return; } _selectItem = event.selectIndex; CourseProcessTopics? topics = _entity?.topics?[_currentPage]; - CourseProcessTopicsTopicAnswerList? answerList = topics?.topicAnswerList?[_selectItem]; + CourseProcessTopicsTopicAnswerList? answerList = + topics?.topicAnswerList?[_selectItem]; if (answerList?.correct == 0) { _playResultSound(false); // showToast('继续加油哦',duration: const Duration(seconds: 2)); @@ -226,35 +247,32 @@ class TopicPictureBloc extends BaseSectionBloc emitter) async { - methodChannel.invokeMethod('initVoiceSdk',event.data); + _initVoiceSdk( + XSVoiceInitEvent event, Emitter emitter) async { + methodChannel.invokeMethod('initVoiceSdk', event.data); } ///先声测试 - void _voiceXsStart(XSVoiceStartEvent event,Emitter emitter) async { + void _voiceXsStart( + XSVoiceStartEvent event, Emitter emitter) async { await audioPlayer.stop(); // 调用封装好的权限检查和请求方法 - bool result = await permissionCheckAndRequest( - context, - Permission.microphone, - "录音" - ); + bool result = + await permissionCheckAndRequest(context, Permission.microphone, "录音"); if (result) { - methodChannel.invokeMethod( - 'startVoice', - { - 'word': event.testWord, - 'type': event.type, - 'userId': event.userId.toString() - } - ); + methodChannel.invokeMethod('startVoice', { + 'word': event.testWord, + 'type': event.type, + 'userId': event.userId.toString() + }); _isVoicing = true; emitter(XSVoiceTestState()); } } ///终止评测 - void _voiceXsStop(XSVoiceStopEvent event,Emitter emitter) async { + void _voiceXsStop( + XSVoiceStopEvent event, Emitter emitter) async { methodChannel.invokeMethod('stopVoice'); } @@ -264,11 +282,12 @@ class TopicPictureBloc extends BaseSectionBloc emitter) async { + void _voiceXsResult( + XSVoiceResultEvent event, Emitter emitter) async { final Map args = event.message as Map; final result = args['result'] as Map; final overall = result['overall'].toString(); - showToast('测评成功,分数是$overall',duration: const Duration(seconds: 5)); + showToast('测评成功,分数是$overall', duration: const Duration(seconds: 5)); _isVoicing = false; emitter(XSVoiceTestState()); if (isLastPage()) { @@ -277,20 +296,23 @@ class TopicPictureBloc extends BaseSectionBloc emitter) async { + void _voicePlayStateChange(VoicePlayStateChangeEvent event, + Emitter emitter) async { emitter(VoicePlayStateChange()); } // 题目音频播放 - void _questionVoicePlay(VoicePlayEvent event,Emitter emitter) async { + void _questionVoicePlay( + VoicePlayEvent event, Emitter emitter) async { if (_forbiddenWhenCorrect) { return; } _forbiddenWhenCorrect = false; await closePlayerResource(); final topics = _entity?.topics?[_currentPage]; - final urlStr = topics?.audioUrl??''; - await audioPlayer.play(UrlSource(urlStr)); + final urlStr = topics?.audioUrl ?? ''; + await audioPlayer.play(UrlSource(urlStr), + balance: 0.0, ctx: AudioContext()); } Future closePlayerResource() async { @@ -302,7 +324,8 @@ class TopicPictureBloc extends BaseSectionBloc _playAudio(String? audioUrl) async { if (audioUrl!.isNotEmpty) { - await audioPlayer.play(UrlSource(audioUrl)); + await audioPlayer.play(UrlSource(audioUrl), + balance: 0.0, ctx: AudioContext()); } }