Blame view

lib/pages/reading/bloc/reading_bloc.dart 15.5 KB
a506beff   吴启风   feat:先声sdk方法找不到问题...
1
  import 'package:audioplayers/audioplayers.dart';
1dc46cf7   吴启风   feat:绘本ui
2
  import 'package:flutter/cupertino.dart';
a506beff   吴启风   feat:先声sdk方法找不到问题...
3
4
  import 'package:flutter/foundation.dart';
  import 'package:flutter/services.dart';
1dc46cf7   吴启风   feat:绘本ui
5
  import 'package:flutter_bloc/flutter_bloc.dart';
a506beff   吴启风   feat:先声sdk方法找不到问题...
6
  import 'package:flutter_easyloading/flutter_easyloading.dart';
fd737d6a   吴启风   feat:绘本作答结果以单词为单位...
7
  import 'package:flutter_screenutil/flutter_screenutil.dart';
6051f850   吴启风   feat:带隐私合规权限申请描述的...
8
  import 'package:permission_handler/permission_handler.dart';
2a427e12   吴启风   feat:绘本静态ui基本完成
9
  import 'package:wow_english/pages/reading/widgets/ReadingModeType.dart';
820997e6   吴启风   feat:过渡页-绘本环节
10
11
12
  import 'package:wow_english/pages/section/subsection/base_section/bloc.dart';
  import 'package:wow_english/pages/section/subsection/base_section/event.dart';
  import 'package:wow_english/pages/section/subsection/base_section/state.dart';
1dc46cf7   吴启风   feat:绘本ui
13
  
dae7f2fd   吴启风   feat:增加android端先声...
14
  import '../../../common/core/user_util.dart';
e811f164   吴启风   feat:权限申请页面增加隐私合规...
15
  import '../../../common/permission/permissionRequester.dart';
a506beff   吴启风   feat:先声sdk方法找不到问题...
16
17
  import '../../../common/request/dao/listen_dao.dart';
  import '../../../common/request/exception.dart';
d0623cfd   吴启风   feat:绘本作答结果动效
18
  import '../../../common/utils/show_star_reward_dialog.dart';
a506beff   吴启风   feat:先声sdk方法找不到问题...
19
  import '../../../models/course_process_entity.dart';
fd737d6a   吴启风   feat:绘本作答结果以单词为单位...
20
  import '../../../models/singsound_result_detail_entity.dart';
d0623cfd   吴启风   feat:绘本作答结果动效
21
  import '../../../models/voice_result_type.dart';
820997e6   吴启风   feat:过渡页-绘本环节
22
  import '../../../route/route.dart';
951eb853   吴启风   feat:代码优化-星星等动效对话...
23
  import '../../../utils/audio_player_util.dart';
a506beff   吴启风   feat:先声sdk方法找不到问题...
24
  import '../../../utils/loading.dart';
a506beff   吴启风   feat:先声sdk方法找不到问题...
25
  
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
26
  import '../../../utils/log_util.dart';
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
27
  
1dc46cf7   吴启风   feat:绘本ui
28
  part 'reading_event.dart';
d0623cfd   吴启风   feat:绘本作答结果动效
29
  
1dc46cf7   吴启风   feat:绘本ui
30
31
  part 'reading_state.dart';
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  enum VoicePlayState {
    ///未知
    unKnow,
  
    ///播放中
    playing,
  
    ///播放完成
    completed,
  
    ///播放终止
    stop
  }
  
820997e6   吴启风   feat:过渡页-绘本环节
46
47
  class ReadingPageBloc
      extends BaseSectionBloc<ReadingPageEvent, ReadingPageState> {
2a427e12   吴启风   feat:绘本静态ui基本完成
48
49
    final PageController pageController;
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
50
51
    final String courseLessonId;
  
2a427e12   吴启风   feat:绘本静态ui基本完成
52
53
54
55
    ///当前页索引
    int _currentPage = 0;
  
    ///当前播放模式
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
56
    ReadingModeType _currentMode = ReadingModeType.manual;
2a427e12   吴启风   feat:绘本静态ui基本完成
57
58
59
60
61
  
    int get currentPage => _currentPage + 1;
  
    ReadingModeType get currentMode => _currentMode;
  
a506beff   吴启风   feat:先声sdk方法找不到问题...
62
63
64
65
66
67
68
69
70
    CourseProcessEntity? _entity;
  
    CourseProcessEntity? get entity => _entity;
  
    ///正在评测
    bool _isRecording = false;
  
    bool get isRecording => _isRecording;
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    ///原始音频是否正在播放
    bool _isOriginAudioPlaying = false;
  
    bool get isOriginAudioPlaying => _isOriginAudioPlaying;
  
    ///录音音频是否正在播放
    bool _isRecordAudioPlaying = false;
  
    bool get isRecordAudioPlaying => _isRecordAudioPlaying;
  
    ///正在播放音频状态
    VoicePlayState _voicePlayState = VoicePlayState.unKnow;
  
    VoicePlayState get voicePlayState => _voicePlayState;
  
a506beff   吴启风   feat:先声sdk方法找不到问题...
86
87
88
89
    late MethodChannel methodChannel;
  
    late AudioPlayer audioPlayer;
  
6051f850   吴启风   feat:带隐私合规权限申请描述的...
90
91
92
    final BuildContext context;
  
    ReadingPageBloc(this.context, this.pageController, this.courseLessonId)
53e9e6db   吴启风   feat:绘本语音评测逻辑
93
        : super(ReadingPageInitial()) {
2a427e12   吴启风   feat:绘本静态ui基本完成
94
      on<CurrentPageIndexChangeEvent>(_pageControllerChange);
53e9e6db   吴启风   feat:绘本语音评测逻辑
95
      on<CurrentModeChangeEvent>(_playModeChange);
2a427e12   吴启风   feat:绘本静态ui基本完成
96
97
98
      // pageController.addListener(() {
      //   _currentPage = pageController.page!.round();
      // });
a506beff   吴启风   feat:先声sdk方法找不到问题...
99
      on<RequestDataEvent>(_requestData);
53e9e6db   吴启风   feat:绘本语音评测逻辑
100
      on<InitBlocEvent>((event, emit) {
a506beff   吴启风   feat:先声sdk方法找不到问题...
101
102
        //音频播放器
        audioPlayer = AudioPlayer();
53e9e6db   吴启风   feat:绘本语音评测逻辑
103
        audioPlayer.onPlayerStateChanged.listen((event) async {
2ee6209a   吴启风   feat:绘本播放状态优化
104
          Log.d("播放状态变化 event=$event");
a506beff   吴启风   feat:先声sdk方法找不到问题...
105
          if (event == PlayerState.completed) {
53e9e6db   吴启风   feat:绘本语音评测逻辑
106
107
            debugPrint('播放完成');
            _voicePlayState = VoicePlayState.completed;
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
108
            _onAudioPlayComplete();
53e9e6db   吴启风   feat:绘本语音评测逻辑
109
110
111
112
          }
          if (event == PlayerState.stopped) {
            debugPrint('播放结束');
            _voicePlayState = VoicePlayState.stop;
53e9e6db   吴启风   feat:绘本语音评测逻辑
113
          }
a506beff   吴启风   feat:先声sdk方法找不到问题...
114
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
115
116
117
118
          if (event == PlayerState.playing) {
            debugPrint('正在播放中');
            _voicePlayState = VoicePlayState.playing;
          }
2ee6209a   吴启风   feat:绘本播放状态优化
119
120
121
122
123
124
  
          if (event == PlayerState.disposed) {
            debugPrint('播放器释放');
            _voicePlayState = VoicePlayState.unKnow;
          }
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
125
126
          if (isClosed) {
            return;
a506beff   吴启风   feat:先声sdk方法找不到问题...
127
          }
53e9e6db   吴启风   feat:绘本语音评测逻辑
128
          add(VoicePlayStateChangeEvent());
a506beff   吴启风   feat:先声sdk方法找不到问题...
129
130
        });
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
131
132
133
        methodChannel =
            const MethodChannel('wow_english/sing_sound_method_channel');
        methodChannel.invokeMethod('initVoiceSdk', {}); //初始化评测
a506beff   吴启风   feat:先声sdk方法找不到问题...
134
        methodChannel.setMethodCallHandler((call) async {
6051f850   吴启风   feat:带隐私合规权限申请描述的...
135
136
          Log.d(
              "setMethodCallHandler method=${call.method} arguments=${call.arguments}");
53e9e6db   吴启风   feat:绘本语音评测逻辑
137
138
          if (call.method == 'voiceResult') {
            //评测结果
1f5969b8   biao   修复 练习和绘本播放音频问题
139
140
141
            await audioPlayer.setAudioContext(AudioContext());
            await audioPlayer.setBalance(0.0);
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
142
143
144
145
146
147
148
149
150
            add(XSVoiceResultEvent(call.arguments));
            return;
          }
  
          if (call.method == 'voiceStart') {
            //评测开始
            if (kDebugMode) {
              print('评测开始');
            }
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
151
152
            _isRecording = true;
            add(OnXSVoiceStateChangeEvent());
53e9e6db   吴启风   feat:绘本语音评测逻辑
153
154
155
            return;
          }
  
46c68b45   吴启风   feat:翻页取消先声评测&增加先...
156
157
          if (call.method == 'voiceEnd' || call.method == 'voiceCancel') {
            //评测结束or评测取消
53e9e6db   吴启风   feat:绘本语音评测逻辑
158
            if (kDebugMode) {
46c68b45   吴启风   feat:翻页取消先声评测&增加先...
159
              print(call.method == 'voiceEnd' ? '评测结束' : '评测取消');
53e9e6db   吴启风   feat:绘本语音评测逻辑
160
            }
1f5969b8   biao   修复 练习和绘本播放音频问题
161
162
163
            await audioPlayer.setAudioContext(AudioContext());
            await audioPlayer.setBalance(0.0);
  
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
164
            _isRecording = false;
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
165
            add(OnXSVoiceStateChangeEvent());
53e9e6db   吴启风   feat:绘本语音评测逻辑
166
167
168
169
170
            return;
          }
  
          if (call.method == 'voiceFail') {
            //评测失败
46c68b45   吴启风   feat:翻页取消先声评测&增加先...
171
            _isRecording = false;
1f5969b8   biao   修复 练习和绘本播放音频问题
172
173
174
            await audioPlayer.setAudioContext(AudioContext());
            await audioPlayer.setBalance(0.0);
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
175
176
            EasyLoading.showToast('评测失败');
            return;
a506beff   吴启风   feat:先声sdk方法找不到问题...
177
178
179
          }
        });
      });
53e9e6db   吴启风   feat:绘本语音评测逻辑
180
      on<VoicePlayStateChangeEvent>(_voicePlayStateChange);
a506beff   吴启风   feat:先声sdk方法找不到问题...
181
      on<PlayOriginalAudioEvent>(_playOriginalAudio);
53e9e6db   吴启风   feat:绘本语音评测逻辑
182
183
184
      on<XSVoiceInitEvent>(_initVoiceSdk);
      on<XSVoiceStartEvent>(_voiceXsStart);
      on<XSVoiceStopEvent>(_voiceXsStop);
a506beff   吴启风   feat:先声sdk方法找不到问题...
185
      on<XSVoiceResultEvent>(_voiceXsResult);
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
186
      on<OnXSVoiceStateChangeEvent>(_onVoiceXsStateChange);
53e9e6db   吴启风   feat:绘本语音评测逻辑
187
188
  
      on<PlayRecordAudioEvent>(_playRecordAudio);
2a427e12   吴启风   feat:绘本静态ui基本完成
189
190
191
192
193
    }
  
    @override
    Future<void> close() {
      pageController.dispose();
a506beff   吴启风   feat:先声sdk方法找不到问题...
194
195
      audioPlayer.release();
      audioPlayer.dispose();
081fbff7   吴启风   feat:性能优化-录音内存泄漏
196
      _voiceXsCancel(force: true);
2a427e12   吴启风   feat:绘本静态ui基本完成
197
198
199
      return super.close();
    }
  
a506beff   吴启风   feat:先声sdk方法找不到问题...
200
201
    void _pageControllerChange(CurrentPageIndexChangeEvent event,
        Emitter<ReadingPageState> emitter) async {
2a427e12   吴启风   feat:绘本静态ui基本完成
202
      _currentPage = event.pageIndex;
46c68b45   吴启风   feat:翻页取消先声评测&增加先...
203
      _voiceXsCancel();
2ee6209a   吴启风   feat:绘本播放状态优化
204
      _playOriginalAudioInner(null, forcePlay: true);
2a427e12   吴启风   feat:绘本静态ui基本完成
205
206
207
      emitter(CurrentPageIndexState());
    }
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
208
    void _playModeChange(
a506beff   吴启风   feat:先声sdk方法找不到问题...
209
        CurrentModeChangeEvent event, Emitter<ReadingPageState> emitter) async {
2a427e12   吴启风   feat:绘本静态ui基本完成
210
211
212
213
214
215
      if (_currentMode == ReadingModeType.auto) {
        _currentMode = ReadingModeType.manual;
      } else {
        _currentMode = ReadingModeType.auto;
      }
      emitter(CurrentModeState());
1dc46cf7   吴启风   feat:绘本ui
216
    }
a506beff   吴启风   feat:先声sdk方法找不到问题...
217
218
219
220
221
222
  
    ///请求数据
    void _requestData(
        RequestDataEvent event, Emitter<ReadingPageState> emitter) async {
      try {
        await loading(() async {
53e9e6db   吴启风   feat:绘本语音评测逻辑
223
          _entity = await ListenDao.process(courseLessonId);
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
224
          Log.d("reading page entity: ${_entity!.toJson()}");
a506beff   吴启风   feat:先声sdk方法找不到问题...
225
226
227
228
229
230
231
232
233
          emitter(RequestDataState());
        });
      } catch (e) {
        if (e is ApiException) {
          EasyLoading.showToast(e.message ?? '请求失败,请检查网络连接');
        }
      }
    }
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
234
235
236
237
238
239
    /// 播放绘本原音
    void _playOriginalAudio(
        PlayOriginalAudioEvent event, Emitter<ReadingPageState> emitter) async {
      _playOriginalAudioInner(event.url);
    }
  
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
240
    ///播放原音音频
2ee6209a   吴启风   feat:绘本播放状态优化
241
242
    /// - [force]: 是否强制播放(true:不管当前状态如何,都会播放目标原音音频,比如翻页场景)
    /// (false:如果正在播放,暂停播放,比如点击播放按钮场景)
93efa69f   biao   删除评测代码
243
244
    void _playOriginalAudioInner(String? audioUrl,
        {bool forcePlay = false}) async {
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
245
      if (_isRecordAudioPlaying) {
2ee6209a   吴启风   feat:绘本播放状态优化
246
        await audioPlayer.stop();
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
247
248
        _isRecordAudioPlaying = false;
      }
6051f850   吴启风   feat:带隐私合规权限申请描述的...
249
250
      Log.d(
          "_playOriginalAudio _isRecordAudioPlaying=$_isRecordAudioPlaying _isOriginAudioPlaying=$_isOriginAudioPlaying url=$audioUrl");
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
251
      if (_isOriginAudioPlaying) {
75e0dcd2   吴启风   feat:修复绘本音频ios听筒出声
252
        await audioPlayer.stop();
2ee6209a   吴启风   feat:绘本播放状态优化
253
254
255
256
257
258
        _isOriginAudioPlaying = false;
        if (forcePlay) {
          audioUrl ??= currentPageData()?.audioUrl ?? '';
          await _playAudio(audioUrl);
          _isOriginAudioPlaying = true;
        }
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
259
      } else {
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
260
        audioUrl ??= currentPageData()?.audioUrl ?? '';
2ee6209a   吴启风   feat:绘本播放状态优化
261
262
        await _playAudio(audioUrl);
        _isOriginAudioPlaying = true;
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
263
      }
a506beff   吴启风   feat:先声sdk方法找不到问题...
264
265
    }
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
266
267
268
269
270
271
    /// 播放录音
    void _playRecordAudio(
        PlayRecordAudioEvent event, Emitter<ReadingPageState> emitter) async {
      _playRecordAudioInner();
    }
  
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
272
    Future<void> _playRecordAudioInner() async {
2ee6209a   吴启风   feat:绘本播放状态优化
273
      Log.d(
93efa69f   biao   删除评测代码
274
          "_playRecordAudioInner _isOriginAudioPlaying=$_isOriginAudioPlaying _isRecordAudioPlaying=$_isRecordAudioPlaying url=${currentPageData()?.recordUrl}");
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
275
      if (_isOriginAudioPlaying) {
2ee6209a   吴启风   feat:绘本播放状态优化
276
277
        ///如果正在播放原音,暂停
        await audioPlayer.stop();
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
278
279
        _isOriginAudioPlaying = false;
      }
2ee6209a   吴启风   feat:绘本播放状态优化
280
  
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
281
      if (_isRecordAudioPlaying) {
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
282
        await audioPlayer.stop();
2ee6209a   吴启风   feat:绘本播放状态优化
283
        _isRecordAudioPlaying = false;
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
284
      } else {
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
285
        final recordAudioUrl = currentPageData()?.recordUrl;
2ee6209a   吴启风   feat:绘本播放状态优化
286
287
        await _playAudio(recordAudioUrl);
        _isRecordAudioPlaying = true;
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
288
      }
ad37b653   吴启风   feat:1、完成微信支付;2、商...
289
      // emitter(VoicePlayStateChange());
53e9e6db   吴启风   feat:绘本语音评测逻辑
290
291
    }
  
2ee6209a   吴启风   feat:绘本播放状态优化
292
    Future<void> _playAudio(String? audioUrl) async {
53e9e6db   吴启风   feat:绘本语音评测逻辑
293
      if (audioUrl!.isNotEmpty) {
1f5969b8   biao   修复 练习和绘本播放音频问题
294
295
        await audioPlayer.play(UrlSource(audioUrl),
            balance: 0.0, ctx: AudioContext());
a506beff   吴启风   feat:先声sdk方法找不到问题...
296
297
298
299
      }
    }
  
    int dataCount() {
a506beff   吴启风   feat:先声sdk方法找不到问题...
300
301
302
303
304
305
306
      return _entity?.readings?.length ?? 0;
    }
  
    CourseProcessReadings? currentPageData() {
      return _entity?.readings?[_currentPage];
    }
  
4d8b0da4   吴启风   feat:绘本跟读底部ui优化
307
308
309
310
311
    ///当前页绘本跟读内容
    String readingContent() {
      return currentPageData()?.word?.trim() ?? '';
    }
  
fd737d6a   吴启风   feat:绘本作答结果以单词为单位...
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
    /// 结合单词分数返回带颜色的TextSpan数组
    List<TextSpan>? displayContent() {
      // return _textController.text.isNotEmpty ? _textController.text : readingContent();
      // 创建用于展示每个单词的 TextSpan 列表
      if (currentPageData() == null) {
        return null;
      }
      List<SingsoundResultDetailEntity> resultDetails;
      if (currentPageData()?.resultDetails != null) {
        resultDetails = currentPageData()!.resultDetails!;
      } else {
        List<String>? wordList = currentPageData()?.word?.split(RegExp(r'\s+'));
        resultDetails = wordList!
            .map(
                (word) => SingsoundResultDetailEntity.withCharAndScore(word, 0))
            .toList();
      }
      List<TextSpan> textSpans = resultDetails.asMap().entries.map((entry) {
        int index = entry.key;
        SingsoundResultDetailEntity detail = entry.value;
        // Check if this is the last word in the list
        bool isLastWord = index == resultDetails.length - 1;
        return TextSpan(
          text: '${detail.char}${isLastWord ? '' : ' '}',
          style: TextStyle(
            color: detail.score > 80
                ? const Color(0XFF35C137)
                : const Color(0xFF333333),
457ac447   吴启风   feat:代码优化-空条件调用
340
            fontSize: 18.sp,
fd737d6a   吴启风   feat:绘本作答结果以单词为单位...
341
342
343
344
345
346
          ),
        );
      }).toList();
      return textSpans;
    }
  
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
347
    void nextPage() {
820997e6   吴启风   feat:过渡页-绘本环节
348
349
350
351
352
353
354
355
356
357
358
      if (currentPage >= dataCount()) {
        sectionComplete(() {
          popPage(data: {
            'currentStep': currentPage,
            'courseLessonId': courseLessonId,
            'isCompleted': true,
            'nextSection': true
          });
        }, againSectionTap: () {
          pageController.jumpToPage(0);
        });
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
359
360
361
      } else {
        _currentPage += 1;
        pageController.nextPage(
8df5fbf9   吴启风   feat:单元列表页由于ios嵌套...
362
          duration: const Duration(milliseconds: 250),
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
363
364
365
366
367
          curve: Curves.ease,
        );
      }
    }
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
368
369
370
371
372
373
    ///初始化SDK
    _initVoiceSdk(
        XSVoiceInitEvent event, Emitter<ReadingPageState> emitter) async {
      methodChannel.invokeMethod('initVoiceSdk', event.data);
    }
  
a506beff   吴启风   feat:先声sdk方法找不到问题...
374
    ///先声测试
53e9e6db   吴启风   feat:绘本语音评测逻辑
375
376
    void _voiceXsStart(
        XSVoiceStartEvent event, Emitter<ReadingPageState> emitter) async {
2ee6209a   吴启风   feat:绘本播放状态优化
377
      await _stopAudio();
a506beff   吴启风   feat:先声sdk方法找不到问题...
378
      startRecord(event.content);
a506beff   吴启风   feat:先声sdk方法找不到问题...
379
380
    }
  
2ee6209a   吴启风   feat:绘本播放状态优化
381
    ///开始录音
a506beff   吴启风   feat:先声sdk方法找不到问题...
382
    void startRecord(String content) async {
6051f850   吴启风   feat:带隐私合规权限申请描述的...
383
      // 调用封装好的权限检查和请求方法
d0623cfd   吴启风   feat:绘本作答结果动效
384
385
      bool result = await requestPermission(
          context, Permission.microphone, "录音", "用于开启录音,识别您的开口作答并给出反馈");
6051f850   吴启风   feat:带隐私合规权限申请描述的...
386
      if (result) {
820997e6   吴启风   feat:过渡页-绘本环节
387
388
389
390
391
        methodChannel.invokeMethod('startVoice', {
          'word': content,
          'type': '0',
          'userId': UserUtil.getUser()?.id.toString()
        });
a506beff   吴启风   feat:先声sdk方法找不到问题...
392
      }
a506beff   吴启风   feat:先声sdk方法找不到问题...
393
394
    }
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
395
396
    void _voiceXsResult(
        XSVoiceResultEvent event, Emitter<ReadingPageState> emitter) async {
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
397
398
399
400
      final Map args = event.message as Map;
      final result = args['result'] as Map;
      Log.d("_voiceXsResult result=$result");
      final overall = result['overall'].toString();
fd737d6a   吴启风   feat:绘本作答结果以单词为单位...
401
402
403
404
405
406
407
408
409
      List<dynamic> resultDetailsJsons = result['details'];
      // 提取 score 和 char 字段
      List<SingsoundResultDetailEntity> detailEntities = [];
      for (var detail in resultDetailsJsons) {
        int score = detail['score'] as int;
        String char = detail['char'] as String;
        detailEntities
            .add(SingsoundResultDetailEntity.withCharAndScore(char, score));
      }
d0623cfd   吴启风   feat:绘本作答结果动效
410
411
412
413
  
      ///todo 后面可以考虑要不要传自己的服务器
      final recordFileUrl = args['audioUrl'].toString();
      int score = int.parse(overall);
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
414
      currentPageData()?.recordScore = overall;
fc048c78   吴启风   feat:绘本跟读录音文件播放失败...
415
      currentPageData()?.recordUrl = args['audioUrl'] + '.mp3';
fd737d6a   吴启风   feat:绘本作答结果以单词为单位...
416
417
      currentPageData()?.resultDetails = detailEntities;
      add(OnXSVoiceStateChangeEvent());
93efa69f   biao   删除评测代码
418
  
d0623cfd   吴启风   feat:绘本作答结果动效
419
      final voiceResult = VoiceResultType.fromScore(score);
951eb853   吴启风   feat:代码优化-星星等动效对话...
420
  
d0623cfd   吴启风   feat:绘本作答结果动效
421
      if (voiceResult.lottieFilePath != null) {
951eb853   吴启风   feat:代码优化-星星等动效对话...
422
423
424
425
426
427
428
        AudioPlayerUtil.getInstance().playAudio(voiceResult.audioType);
        await showCheerRewardDialog(context, lottieFile: voiceResult.lottieFilePath!, onDismiss: () async {
          await actionAfterRecord();
        });
      } else {
        await AudioPlayerUtil.getInstance().playAudio(voiceResult.audioType);
        await actionAfterRecord();
820997e6   吴启风   feat:过渡页-绘本环节
429
      }
951eb853   吴启风   feat:代码优化-星星等动效对话...
430
431
432
433
434
435
436
437
438
439
440
441
442
    }
  
    /// 作答音效&动效播放结束后
    Future<void> actionAfterRecord() async {
      ///完成录音后紧接着播放录音
      await _playRecordAudioInner();
      if (isLastPage()) {
        sectionComplete(() {
          popPage(data: {
            'currentStep': currentPage,
            'courseLessonId': courseLessonId,
            'isCompleted': true,
            'nextSection': true
d0623cfd   吴启风   feat:绘本作答结果动效
443
          });
951eb853   吴启风   feat:代码优化-星星等动效对话...
444
445
446
447
448
        }, againSectionTap: () {
          _resetLocalResult();
          pageController.jumpToPage(0);
        });
      }
a506beff   吴启风   feat:先声sdk方法找不到问题...
449
    }
53e9e6db   吴启风   feat:绘本语音评测逻辑
450
451
452
453
454
455
456
  
    ///终止评测
    void _voiceXsStop(
        XSVoiceStopEvent event, Emitter<ReadingPageState> emitter) async {
      methodChannel.invokeMethod('stopVoice');
    }
  
6b0947ca   吴启风   feat:绘本增加initVoic...
457
    ///取消评测(用于处理退出页面后录音未停止等异常情况的保护操作)
081fbff7   吴启风   feat:性能优化-录音内存泄漏
458
    void _voiceXsCancel({bool force = false}) {
46c68b45   吴启风   feat:翻页取消先声评测&增加先...
459
      Log.d("取消评测 _voiceXsCancel _isRecording=$_isRecording");
081fbff7   吴启风   feat:性能优化-录音内存泄漏
460
      if (_isRecording || force) {
46c68b45   吴启风   feat:翻页取消先声评测&增加先...
461
462
        methodChannel.invokeMethod('cancelVoice');
      }
6b0947ca   吴启风   feat:绘本增加initVoic...
463
464
    }
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
465
466
467
468
469
    void _voicePlayStateChange(VoicePlayStateChangeEvent event,
        Emitter<ReadingPageState> emitter) async {
      emitter(VoicePlayStateChange());
    }
  
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
470
471
472
473
474
    void _onAudioPlayComplete() {
      if (_isRecordAudioPlaying && _currentMode == ReadingModeType.auto) {
        nextPage();
      }
  
820997e6   吴启风   feat:过渡页-绘本环节
475
476
477
      Log.d(
          "_onAudioPlayComplete _isOriginAudioPlaying=$_isOriginAudioPlaying _voicePlayState=$_voicePlayState recordUrl=${currentPageData()?.recordUrl?.isNotEmpty}");
      if (_isOriginAudioPlaying &&
820997e6   吴启风   feat:过渡页-绘本环节
478
          currentPageData()?.recordUrl?.isNotEmpty != true) {
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
479
480
481
482
483
484
485
486
        ///如果刚刚完成原音播放&&录音为空,则开始录音
        startRecord(currentPageData()?.word ?? '');
      }
  
      _isOriginAudioPlaying = false;
      _isRecordAudioPlaying = false;
    }
  
2ee6209a   吴启风   feat:绘本播放状态优化
487
    Future<void> _stopAudio() async {
53e9e6db   吴启风   feat:绘本语音评测逻辑
488
489
490
491
      await audioPlayer.stop();
      _isOriginAudioPlaying = false;
      _isRecordAudioPlaying = false;
    }
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
492
  
820997e6   吴启风   feat:过渡页-绘本环节
493
494
    void _onVoiceXsStateChange(OnXSVoiceStateChangeEvent event,
        Emitter<ReadingPageState> emitter) async {
ad37b653   吴启风   feat:1、完成微信支付;2、商...
495
      emitter(XSVoiceTestState());
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
496
    }
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
497
  
820997e6   吴启风   feat:过渡页-绘本环节
498
499
500
501
502
503
504
505
506
507
508
509
510
    ///是否是最后一页
    bool isLastPage() {
      return currentPage == dataCount();
    }
  
    ///重置数据
    void _resetLocalResult() {
      _entity?.readings?.forEach((element) {
        element.recordScore = null;
        element.recordUrl = null;
      });
    }
  }