Blame view

lib/pages/reading/bloc/reading_bloc.dart 10.3 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';
6051f850   吴启风   feat:带隐私合规权限申请描述的...
7
  import 'package:permission_handler/permission_handler.dart';
2a427e12   吴启风   feat:绘本静态ui基本完成
8
  import 'package:wow_english/pages/reading/widgets/ReadingModeType.dart';
1dc46cf7   吴启风   feat:绘本ui
9
  
dae7f2fd   吴启风   feat:增加android端先声...
10
  import '../../../common/core/user_util.dart';
a506beff   吴启风   feat:先声sdk方法找不到问题...
11
12
13
14
  import '../../../common/request/dao/listen_dao.dart';
  import '../../../common/request/exception.dart';
  import '../../../models/course_process_entity.dart';
  import '../../../utils/loading.dart';
a506beff   吴启风   feat:先声sdk方法找不到问题...
15
  
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
16
  import '../../../utils/log_util.dart';
20345cbf   吴启风   feat:修改头像权限androi...
17
  import '../../../common/permission/permissionRequestPage.dart';
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
18
  
1dc46cf7   吴启风   feat:绘本ui
19
20
21
  part 'reading_event.dart';
  part 'reading_state.dart';
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  enum VoicePlayState {
    ///未知
    unKnow,
  
    ///播放中
    playing,
  
    ///播放完成
    completed,
  
    ///播放终止
    stop
  }
  
1dc46cf7   吴启风   feat:绘本ui
36
  class ReadingPageBloc extends Bloc<ReadingPageEvent, ReadingPageState> {
2a427e12   吴启风   feat:绘本静态ui基本完成
37
38
    final PageController pageController;
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
39
40
    final String courseLessonId;
  
2a427e12   吴启风   feat:绘本静态ui基本完成
41
42
43
44
    ///当前页索引
    int _currentPage = 0;
  
    ///当前播放模式
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
45
    ReadingModeType _currentMode = ReadingModeType.manual;
2a427e12   吴启风   feat:绘本静态ui基本完成
46
47
48
49
50
  
    int get currentPage => _currentPage + 1;
  
    ReadingModeType get currentMode => _currentMode;
  
a506beff   吴启风   feat:先声sdk方法找不到问题...
51
52
53
54
55
56
57
58
59
    CourseProcessEntity? _entity;
  
    CourseProcessEntity? get entity => _entity;
  
    ///正在评测
    bool _isRecording = false;
  
    bool get isRecording => _isRecording;
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    ///原始音频是否正在播放
    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方法找不到问题...
75
76
77
78
    late MethodChannel methodChannel;
  
    late AudioPlayer audioPlayer;
  
6051f850   吴启风   feat:带隐私合规权限申请描述的...
79
80
81
    final BuildContext context;
  
    ReadingPageBloc(this.context, this.pageController, this.courseLessonId)
53e9e6db   吴启风   feat:绘本语音评测逻辑
82
        : super(ReadingPageInitial()) {
2a427e12   吴启风   feat:绘本静态ui基本完成
83
      on<CurrentPageIndexChangeEvent>(_pageControllerChange);
53e9e6db   吴启风   feat:绘本语音评测逻辑
84
      on<CurrentModeChangeEvent>(_playModeChange);
2a427e12   吴启风   feat:绘本静态ui基本完成
85
86
87
      // pageController.addListener(() {
      //   _currentPage = pageController.page!.round();
      // });
a506beff   吴启风   feat:先声sdk方法找不到问题...
88
      on<RequestDataEvent>(_requestData);
53e9e6db   吴启风   feat:绘本语音评测逻辑
89
      on<InitBlocEvent>((event, emit) {
a506beff   吴启风   feat:先声sdk方法找不到问题...
90
91
        //音频播放器
        audioPlayer = AudioPlayer();
53e9e6db   吴启风   feat:绘本语音评测逻辑
92
        audioPlayer.onPlayerStateChanged.listen((event) async {
a506beff   吴启风   feat:先声sdk方法找不到问题...
93
          if (event == PlayerState.completed) {
53e9e6db   吴启风   feat:绘本语音评测逻辑
94
            _voicePlayState = VoicePlayState.completed;
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
95
            _onAudioPlayComplete();
53e9e6db   吴启风   feat:绘本语音评测逻辑
96
97
          }
          if (event == PlayerState.stopped) {
53e9e6db   吴启风   feat:绘本语音评测逻辑
98
            _voicePlayState = VoicePlayState.stop;
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
99
            _onAudioPlayComplete();
53e9e6db   吴启风   feat:绘本语音评测逻辑
100
          }
a506beff   吴启风   feat:先声sdk方法找不到问题...
101
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
102
          if (event == PlayerState.playing) {
53e9e6db   吴启风   feat:绘本语音评测逻辑
103
104
105
106
            _voicePlayState = VoicePlayState.playing;
          }
          if (isClosed) {
            return;
a506beff   吴启风   feat:先声sdk方法找不到问题...
107
          }
53e9e6db   吴启风   feat:绘本语音评测逻辑
108
          add(VoicePlayStateChangeEvent());
a506beff   吴启风   feat:先声sdk方法找不到问题...
109
110
        });
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
111
112
113
        methodChannel =
            const MethodChannel('wow_english/sing_sound_method_channel');
        methodChannel.invokeMethod('initVoiceSdk', {}); //初始化评测
a506beff   吴启风   feat:先声sdk方法找不到问题...
114
        methodChannel.setMethodCallHandler((call) async {
6051f850   吴启风   feat:带隐私合规权限申请描述的...
115
116
          Log.d(
              "setMethodCallHandler method=${call.method} arguments=${call.arguments}");
53e9e6db   吴启风   feat:绘本语音评测逻辑
117
118
119
120
121
122
123
124
125
126
127
          if (call.method == 'voiceResult') {
            //评测结果
            add(XSVoiceResultEvent(call.arguments));
            return;
          }
  
          if (call.method == 'voiceStart') {
            //评测开始
            if (kDebugMode) {
              print('评测开始');
            }
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
128
129
            _isRecording = true;
            add(OnXSVoiceStateChangeEvent());
53e9e6db   吴启风   feat:绘本语音评测逻辑
130
131
132
133
134
135
136
137
            return;
          }
  
          if (call.method == 'voiceEnd') {
            //评测结束
            if (kDebugMode) {
              print('评测结束');
            }
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
138
139
            _isRecording = false;
            add(OnXSVoiceStateChangeEvent());
53e9e6db   吴启风   feat:绘本语音评测逻辑
140
141
142
143
144
145
146
            return;
          }
  
          if (call.method == 'voiceFail') {
            //评测失败
            EasyLoading.showToast('评测失败');
            return;
a506beff   吴启风   feat:先声sdk方法找不到问题...
147
148
149
          }
        });
      });
53e9e6db   吴启风   feat:绘本语音评测逻辑
150
      on<VoicePlayStateChangeEvent>(_voicePlayStateChange);
a506beff   吴启风   feat:先声sdk方法找不到问题...
151
      on<PlayOriginalAudioEvent>(_playOriginalAudio);
53e9e6db   吴启风   feat:绘本语音评测逻辑
152
153
154
      on<XSVoiceInitEvent>(_initVoiceSdk);
      on<XSVoiceStartEvent>(_voiceXsStart);
      on<XSVoiceStopEvent>(_voiceXsStop);
a506beff   吴启风   feat:先声sdk方法找不到问题...
155
      on<XSVoiceResultEvent>(_voiceXsResult);
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
156
      on<OnXSVoiceStateChangeEvent>(_onVoiceXsStateChange);
53e9e6db   吴启风   feat:绘本语音评测逻辑
157
158
  
      on<PlayRecordAudioEvent>(_playRecordAudio);
2a427e12   吴启风   feat:绘本静态ui基本完成
159
160
161
162
163
    }
  
    @override
    Future<void> close() {
      pageController.dispose();
a506beff   吴启风   feat:先声sdk方法找不到问题...
164
165
      audioPlayer.release();
      audioPlayer.dispose();
6b0947ca   吴启风   feat:绘本增加initVoic...
166
      _voiceXsCancel();
2a427e12   吴启风   feat:绘本静态ui基本完成
167
168
169
      return super.close();
    }
  
a506beff   吴启风   feat:先声sdk方法找不到问题...
170
171
    void _pageControllerChange(CurrentPageIndexChangeEvent event,
        Emitter<ReadingPageState> emitter) async {
2a427e12   吴启风   feat:绘本静态ui基本完成
172
      _currentPage = event.pageIndex;
53e9e6db   吴启风   feat:绘本语音评测逻辑
173
      _playOriginalAudioInner(null);
2a427e12   吴启风   feat:绘本静态ui基本完成
174
175
176
      emitter(CurrentPageIndexState());
    }
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
177
    void _playModeChange(
a506beff   吴启风   feat:先声sdk方法找不到问题...
178
        CurrentModeChangeEvent event, Emitter<ReadingPageState> emitter) async {
2a427e12   吴启风   feat:绘本静态ui基本完成
179
180
181
182
183
184
      if (_currentMode == ReadingModeType.auto) {
        _currentMode = ReadingModeType.manual;
      } else {
        _currentMode = ReadingModeType.auto;
      }
      emitter(CurrentModeState());
1dc46cf7   吴启风   feat:绘本ui
185
    }
a506beff   吴启风   feat:先声sdk方法找不到问题...
186
187
188
189
190
191
  
    ///请求数据
    void _requestData(
        RequestDataEvent event, Emitter<ReadingPageState> emitter) async {
      try {
        await loading(() async {
53e9e6db   吴启风   feat:绘本语音评测逻辑
192
          _entity = await ListenDao.process(courseLessonId);
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
193
          Log.d("reading page entity: ${_entity!.toJson()}");
a506beff   吴启风   feat:先声sdk方法找不到问题...
194
195
196
197
198
199
200
201
202
          emitter(RequestDataState());
        });
      } catch (e) {
        if (e is ApiException) {
          EasyLoading.showToast(e.message ?? '请求失败,请检查网络连接');
        }
      }
    }
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
203
204
205
206
207
208
    /// 播放绘本原音
    void _playOriginalAudio(
        PlayOriginalAudioEvent event, Emitter<ReadingPageState> emitter) async {
      _playOriginalAudioInner(event.url);
    }
  
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
209
210
211
212
213
    ///播放原音音频
    Future<void> _playOriginalAudioInner(String? audioUrl) async {
      if (_isRecordAudioPlaying) {
        _isRecordAudioPlaying = false;
      }
6051f850   吴启风   feat:带隐私合规权限申请描述的...
214
215
      Log.d(
          "_playOriginalAudio _isRecordAudioPlaying=$_isRecordAudioPlaying _isOriginAudioPlaying=$_isOriginAudioPlaying url=$audioUrl");
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
216
217
218
219
220
221
222
223
      if (_isOriginAudioPlaying) {
        _isOriginAudioPlaying = false;
        await audioPlayer.stop();
      } else {
        _isOriginAudioPlaying = true;
        audioUrl ??= currentPageData()?.audioUrl ?? '';
        _playAudio(audioUrl);
      }
a506beff   吴启风   feat:先声sdk方法找不到问题...
224
225
    }
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
226
227
228
229
230
231
    /// 播放录音
    void _playRecordAudio(
        PlayRecordAudioEvent event, Emitter<ReadingPageState> emitter) async {
      _playRecordAudioInner();
    }
  
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
232
233
234
235
    Future<void> _playRecordAudioInner() async {
      if (_isOriginAudioPlaying) {
        _isOriginAudioPlaying = false;
      }
6051f850   吴启风   feat:带隐私合规权限申请描述的...
236
237
      Log.d(
          "_playRecordAudioInner _isRecordAudioPlaying=$_isRecordAudioPlaying url=${currentPageData()?.recordUrl}");
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
238
239
240
241
242
243
244
245
246
      if (_isRecordAudioPlaying) {
        _isRecordAudioPlaying = false;
        await audioPlayer.stop();
      } else {
        _isRecordAudioPlaying = true;
        final recordAudioUrl = currentPageData()?.recordUrl;
        _playAudio(recordAudioUrl);
      }
      // emit(VoicePlayStateChange());
53e9e6db   吴启风   feat:绘本语音评测逻辑
247
248
249
    }
  
    void _playAudio(String? audioUrl) async {
53e9e6db   吴启风   feat:绘本语音评测逻辑
250
251
      if (audioUrl!.isNotEmpty) {
        await audioPlayer.play(UrlSource(audioUrl));
a506beff   吴启风   feat:先声sdk方法找不到问题...
252
253
254
255
      }
    }
  
    int dataCount() {
a506beff   吴启风   feat:先声sdk方法找不到问题...
256
257
258
259
      return _entity?.readings?.length ?? 0;
    }
  
    CourseProcessReadings? currentPageData() {
a434ef7b   liangchengyou   feat:修复绘本页闪退问题
260
261
262
      if ((_entity?.readings?.length??0) - 1 < _currentPage) {
        return null;
      }
a506beff   吴启风   feat:先声sdk方法找不到问题...
263
264
265
      return _entity?.readings?[_currentPage];
    }
  
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
266
267
268
269
270
271
272
273
274
275
276
277
    void nextPage() {
      if (_currentPage >= dataCount() - 1) {
        ///todo 最后一页了
      } else {
        _currentPage += 1;
        pageController.nextPage(
          duration: const Duration(milliseconds: 500),
          curve: Curves.ease,
        );
      }
    }
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
278
279
280
281
282
283
    ///初始化SDK
    _initVoiceSdk(
        XSVoiceInitEvent event, Emitter<ReadingPageState> emitter) async {
      methodChannel.invokeMethod('initVoiceSdk', event.data);
    }
  
a506beff   吴启风   feat:先声sdk方法找不到问题...
284
    ///先声测试
53e9e6db   吴启风   feat:绘本语音评测逻辑
285
286
287
    void _voiceXsStart(
        XSVoiceStartEvent event, Emitter<ReadingPageState> emitter) async {
      _stopAudio();
a506beff   吴启风   feat:先声sdk方法找不到问题...
288
      startRecord(event.content);
a506beff   吴启风   feat:先声sdk方法找不到问题...
289
290
291
    }
  
    void startRecord(String content) async {
6051f850   吴启风   feat:带隐私合规权限申请描述的...
292
293
294
295
296
297
298
299
      // 调用封装好的权限检查和请求方法
      bool result = await permissionCheckAndRequest(
          context,
          Permission.microphone,
          "录音"
      );
      if (result) {
        methodChannel.invokeMethod(
dae7f2fd   吴启风   feat:增加android端先声...
300
            'startVoice', {'word': content, 'type': '0', 'userId': UserUtil.getUser()?.id.toString()});
a506beff   吴启风   feat:先声sdk方法找不到问题...
301
      }
a506beff   吴启风   feat:先声sdk方法找不到问题...
302
303
    }
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
304
305
    void _voiceXsResult(
        XSVoiceResultEvent event, Emitter<ReadingPageState> emitter) async {
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
306
307
308
309
310
311
312
313
314
315
316
      final Map args = event.message as Map;
      final result = args['result'] as Map;
      Log.d("_voiceXsResult result=$result");
      final overall = result['overall'].toString();
      EasyLoading.showToast('测评成功,分数是$overall',
          duration: const Duration(seconds: 10));
      currentPageData()?.recordScore = overall;
      currentPageData()?.recordUrl = args['audioUrl'] + '.mp3';
      ///完成录音后紧接着播放录音
      _playRecordAudioInner();
      emitter(FeedbackState());
a506beff   吴启风   feat:先声sdk方法找不到问题...
317
    }
53e9e6db   吴启风   feat:绘本语音评测逻辑
318
319
320
321
322
323
324
  
    ///终止评测
    void _voiceXsStop(
        XSVoiceStopEvent event, Emitter<ReadingPageState> emitter) async {
      methodChannel.invokeMethod('stopVoice');
    }
  
6b0947ca   吴启风   feat:绘本增加initVoic...
325
326
327
328
329
    ///取消评测(用于处理退出页面后录音未停止等异常情况的保护操作)
    void _voiceXsCancel() {
      methodChannel.invokeMethod('cancelVoice');
    }
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
330
331
332
333
334
    void _voicePlayStateChange(VoicePlayStateChangeEvent event,
        Emitter<ReadingPageState> emitter) async {
      emitter(VoicePlayStateChange());
    }
  
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
335
336
337
338
339
    void _onAudioPlayComplete() {
      if (_isRecordAudioPlaying && _currentMode == ReadingModeType.auto) {
        nextPage();
      }
  
a434ef7b   liangchengyou   feat:修复绘本页闪退问题
340
      Log.d("_onAudioPlayComplete _isOriginAudioPlaying=$_isOriginAudioPlaying _voicePlayState=$_voicePlayState recordUrl=${currentPageData()?.recordUrl?.isNotEmpty}");
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
341
342
343
344
345
346
347
348
349
      if (_isOriginAudioPlaying && _voicePlayState == VoicePlayState.completed && currentPageData()?.recordUrl?.isNotEmpty != true) {
        ///如果刚刚完成原音播放&&录音为空,则开始录音
        startRecord(currentPageData()?.word ?? '');
      }
  
      _isOriginAudioPlaying = false;
      _isRecordAudioPlaying = false;
    }
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
350
351
352
353
354
    void _stopAudio() async {
      await audioPlayer.stop();
      _isOriginAudioPlaying = false;
      _isRecordAudioPlaying = false;
    }
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
355
356
357
358
359
  
    void _onVoiceXsStateChange(
      OnXSVoiceStateChangeEvent event,
        Emitter<ReadingPageState> emitter
    ) async {
a434ef7b   liangchengyou   feat:修复绘本页闪退问题
360
        emitter(XSVoiceTestState());
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
361
    }
1dc46cf7   吴启风   feat:绘本ui
362
  }
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...