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
93
        audioPlayer.onPlayerStateChanged.listen((event) async {
          debugPrint('播放状态变化');
a506beff   吴启风   feat:先声sdk方法找不到问题...
94
          if (event == PlayerState.completed) {
53e9e6db   吴启风   feat:绘本语音评测逻辑
95
96
            debugPrint('播放完成');
            _voicePlayState = VoicePlayState.completed;
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
97
            _onAudioPlayComplete();
53e9e6db   吴启风   feat:绘本语音评测逻辑
98
99
100
101
          }
          if (event == PlayerState.stopped) {
            debugPrint('播放结束');
            _voicePlayState = VoicePlayState.stop;
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
102
            _onAudioPlayComplete();
53e9e6db   吴启风   feat:绘本语音评测逻辑
103
          }
a506beff   吴启风   feat:先声sdk方法找不到问题...
104
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
105
106
107
108
109
110
          if (event == PlayerState.playing) {
            debugPrint('正在播放中');
            _voicePlayState = VoicePlayState.playing;
          }
          if (isClosed) {
            return;
a506beff   吴启风   feat:先声sdk方法找不到问题...
111
          }
53e9e6db   吴启风   feat:绘本语音评测逻辑
112
          add(VoicePlayStateChangeEvent());
a506beff   吴启风   feat:先声sdk方法找不到问题...
113
114
        });
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
115
116
117
        methodChannel =
            const MethodChannel('wow_english/sing_sound_method_channel');
        methodChannel.invokeMethod('initVoiceSdk', {}); //初始化评测
a506beff   吴启风   feat:先声sdk方法找不到问题...
118
        methodChannel.setMethodCallHandler((call) async {
6051f850   吴启风   feat:带隐私合规权限申请描述的...
119
120
          Log.d(
              "setMethodCallHandler method=${call.method} arguments=${call.arguments}");
53e9e6db   吴启风   feat:绘本语音评测逻辑
121
122
123
124
125
126
127
128
129
130
131
          if (call.method == 'voiceResult') {
            //评测结果
            add(XSVoiceResultEvent(call.arguments));
            return;
          }
  
          if (call.method == 'voiceStart') {
            //评测开始
            if (kDebugMode) {
              print('评测开始');
            }
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
132
133
            _isRecording = true;
            add(OnXSVoiceStateChangeEvent());
53e9e6db   吴启风   feat:绘本语音评测逻辑
134
135
136
137
138
139
140
141
            return;
          }
  
          if (call.method == 'voiceEnd') {
            //评测结束
            if (kDebugMode) {
              print('评测结束');
            }
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
142
143
            _isRecording = false;
            add(OnXSVoiceStateChangeEvent());
53e9e6db   吴启风   feat:绘本语音评测逻辑
144
145
146
147
148
149
150
            return;
          }
  
          if (call.method == 'voiceFail') {
            //评测失败
            EasyLoading.showToast('评测失败');
            return;
a506beff   吴启风   feat:先声sdk方法找不到问题...
151
152
153
          }
        });
      });
53e9e6db   吴启风   feat:绘本语音评测逻辑
154
      on<VoicePlayStateChangeEvent>(_voicePlayStateChange);
a506beff   吴启风   feat:先声sdk方法找不到问题...
155
      on<PlayOriginalAudioEvent>(_playOriginalAudio);
53e9e6db   吴启风   feat:绘本语音评测逻辑
156
157
158
      on<XSVoiceInitEvent>(_initVoiceSdk);
      on<XSVoiceStartEvent>(_voiceXsStart);
      on<XSVoiceStopEvent>(_voiceXsStop);
a506beff   吴启风   feat:先声sdk方法找不到问题...
159
      on<XSVoiceResultEvent>(_voiceXsResult);
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
160
      on<OnXSVoiceStateChangeEvent>(_onVoiceXsStateChange);
53e9e6db   吴启风   feat:绘本语音评测逻辑
161
162
  
      on<PlayRecordAudioEvent>(_playRecordAudio);
2a427e12   吴启风   feat:绘本静态ui基本完成
163
164
165
166
167
    }
  
    @override
    Future<void> close() {
      pageController.dispose();
a506beff   吴启风   feat:先声sdk方法找不到问题...
168
169
      audioPlayer.release();
      audioPlayer.dispose();
6b0947ca   吴启风   feat:绘本增加initVoic...
170
      _voiceXsCancel();
2a427e12   吴启风   feat:绘本静态ui基本完成
171
172
173
      return super.close();
    }
  
a506beff   吴启风   feat:先声sdk方法找不到问题...
174
175
    void _pageControllerChange(CurrentPageIndexChangeEvent event,
        Emitter<ReadingPageState> emitter) async {
2a427e12   吴启风   feat:绘本静态ui基本完成
176
      _currentPage = event.pageIndex;
53e9e6db   吴启风   feat:绘本语音评测逻辑
177
      _playOriginalAudioInner(null);
2a427e12   吴启风   feat:绘本静态ui基本完成
178
179
180
      emitter(CurrentPageIndexState());
    }
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
181
    void _playModeChange(
a506beff   吴启风   feat:先声sdk方法找不到问题...
182
        CurrentModeChangeEvent event, Emitter<ReadingPageState> emitter) async {
2a427e12   吴启风   feat:绘本静态ui基本完成
183
184
185
186
187
188
      if (_currentMode == ReadingModeType.auto) {
        _currentMode = ReadingModeType.manual;
      } else {
        _currentMode = ReadingModeType.auto;
      }
      emitter(CurrentModeState());
1dc46cf7   吴启风   feat:绘本ui
189
    }
a506beff   吴启风   feat:先声sdk方法找不到问题...
190
191
192
193
194
195
  
    ///请求数据
    void _requestData(
        RequestDataEvent event, Emitter<ReadingPageState> emitter) async {
      try {
        await loading(() async {
53e9e6db   吴启风   feat:绘本语音评测逻辑
196
          _entity = await ListenDao.process(courseLessonId);
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
197
          Log.d("reading page entity: ${_entity!.toJson()}");
a506beff   吴启风   feat:先声sdk方法找不到问题...
198
199
200
201
202
203
204
205
206
          emitter(RequestDataState());
        });
      } catch (e) {
        if (e is ApiException) {
          EasyLoading.showToast(e.message ?? '请求失败,请检查网络连接');
        }
      }
    }
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
207
208
209
210
211
212
    /// 播放绘本原音
    void _playOriginalAudio(
        PlayOriginalAudioEvent event, Emitter<ReadingPageState> emitter) async {
      _playOriginalAudioInner(event.url);
    }
  
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
213
214
215
216
217
    ///播放原音音频
    Future<void> _playOriginalAudioInner(String? audioUrl) async {
      if (_isRecordAudioPlaying) {
        _isRecordAudioPlaying = false;
      }
6051f850   吴启风   feat:带隐私合规权限申请描述的...
218
219
      Log.d(
          "_playOriginalAudio _isRecordAudioPlaying=$_isRecordAudioPlaying _isOriginAudioPlaying=$_isOriginAudioPlaying url=$audioUrl");
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
220
221
222
223
224
225
226
227
      if (_isOriginAudioPlaying) {
        _isOriginAudioPlaying = false;
        await audioPlayer.stop();
      } else {
        _isOriginAudioPlaying = true;
        audioUrl ??= currentPageData()?.audioUrl ?? '';
        _playAudio(audioUrl);
      }
a506beff   吴启风   feat:先声sdk方法找不到问题...
228
229
    }
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
230
231
232
233
234
235
    /// 播放录音
    void _playRecordAudio(
        PlayRecordAudioEvent event, Emitter<ReadingPageState> emitter) async {
      _playRecordAudioInner();
    }
  
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
236
237
238
239
    Future<void> _playRecordAudioInner() async {
      if (_isOriginAudioPlaying) {
        _isOriginAudioPlaying = false;
      }
6051f850   吴启风   feat:带隐私合规权限申请描述的...
240
241
      Log.d(
          "_playRecordAudioInner _isRecordAudioPlaying=$_isRecordAudioPlaying url=${currentPageData()?.recordUrl}");
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
242
243
244
245
246
247
248
249
      if (_isRecordAudioPlaying) {
        _isRecordAudioPlaying = false;
        await audioPlayer.stop();
      } else {
        _isRecordAudioPlaying = true;
        final recordAudioUrl = currentPageData()?.recordUrl;
        _playAudio(recordAudioUrl);
      }
ad37b653   吴启风   feat:1、完成微信支付;2、商...
250
      // emitter(VoicePlayStateChange());
53e9e6db   吴启风   feat:绘本语音评测逻辑
251
252
253
    }
  
    void _playAudio(String? audioUrl) async {
53e9e6db   吴启风   feat:绘本语音评测逻辑
254
255
      if (audioUrl!.isNotEmpty) {
        await audioPlayer.play(UrlSource(audioUrl));
a506beff   吴启风   feat:先声sdk方法找不到问题...
256
257
258
259
      }
    }
  
    int dataCount() {
a506beff   吴启风   feat:先声sdk方法找不到问题...
260
261
262
263
264
265
266
      return _entity?.readings?.length ?? 0;
    }
  
    CourseProcessReadings? currentPageData() {
      return _entity?.readings?[_currentPage];
    }
  
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
267
268
269
270
271
272
273
274
275
276
277
278
    void nextPage() {
      if (_currentPage >= dataCount() - 1) {
        ///todo 最后一页了
      } else {
        _currentPage += 1;
        pageController.nextPage(
          duration: const Duration(milliseconds: 500),
          curve: Curves.ease,
        );
      }
    }
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
279
280
281
282
283
284
    ///初始化SDK
    _initVoiceSdk(
        XSVoiceInitEvent event, Emitter<ReadingPageState> emitter) async {
      methodChannel.invokeMethod('initVoiceSdk', event.data);
    }
  
a506beff   吴启风   feat:先声sdk方法找不到问题...
285
    ///先声测试
53e9e6db   吴启风   feat:绘本语音评测逻辑
286
287
288
    void _voiceXsStart(
        XSVoiceStartEvent event, Emitter<ReadingPageState> emitter) async {
      _stopAudio();
a506beff   吴启风   feat:先声sdk方法找不到问题...
289
      startRecord(event.content);
a506beff   吴启风   feat:先声sdk方法找不到问题...
290
291
292
    }
  
    void startRecord(String content) async {
6051f850   吴启风   feat:带隐私合规权限申请描述的...
293
294
295
296
297
298
299
300
      // 调用封装好的权限检查和请求方法
      bool result = await permissionCheckAndRequest(
          context,
          Permission.microphone,
          "录音"
      );
      if (result) {
        methodChannel.invokeMethod(
dae7f2fd   吴启风   feat:增加android端先声...
301
            'startVoice', {'word': content, 'type': '0', 'userId': UserUtil.getUser()?.id.toString()});
a506beff   吴启风   feat:先声sdk方法找不到问题...
302
      }
a506beff   吴启风   feat:先声sdk方法找不到问题...
303
304
    }
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
305
306
    void _voiceXsResult(
        XSVoiceResultEvent event, Emitter<ReadingPageState> emitter) async {
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
307
308
309
310
311
      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',
a6ad33b3   吴启风   feat:绘本评测结果toast时长修改
312
          duration: const Duration(seconds: 2));
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
313
314
315
316
      currentPageData()?.recordScore = overall;
      currentPageData()?.recordUrl = args['audioUrl'] + '.mp3';
      ///完成录音后紧接着播放录音
      _playRecordAudioInner();
a6ad33b3   吴启风   feat:绘本评测结果toast时长修改
317
      // emitter(FeedbackState());
a506beff   吴启风   feat:先声sdk方法找不到问题...
318
    }
53e9e6db   吴启风   feat:绘本语音评测逻辑
319
320
321
322
323
324
325
  
    ///终止评测
    void _voiceXsStop(
        XSVoiceStopEvent event, Emitter<ReadingPageState> emitter) async {
      methodChannel.invokeMethod('stopVoice');
    }
  
6b0947ca   吴启风   feat:绘本增加initVoic...
326
327
328
329
330
    ///取消评测(用于处理退出页面后录音未停止等异常情况的保护操作)
    void _voiceXsCancel() {
      methodChannel.invokeMethod('cancelVoice');
    }
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
331
332
333
334
335
    void _voicePlayStateChange(VoicePlayStateChangeEvent event,
        Emitter<ReadingPageState> emitter) async {
      emitter(VoicePlayStateChange());
    }
  
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
336
337
338
339
340
    void _onAudioPlayComplete() {
      if (_isRecordAudioPlaying && _currentMode == ReadingModeType.auto) {
        nextPage();
      }
  
354ac7e6   吴启风   feat:隐藏视频跟读入口、底部草...
341
      Log.d("_onAudioPlayComplete _isOriginAudioPlaying=$_isOriginAudioPlaying _voicePlayState=$_voicePlayState recordUrl=${currentPageData()?.recordUrl?.isNotEmpty}");
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
342
343
344
345
346
347
348
349
350
      if (_isOriginAudioPlaying && _voicePlayState == VoicePlayState.completed && currentPageData()?.recordUrl?.isNotEmpty != true) {
        ///如果刚刚完成原音播放&&录音为空,则开始录音
        startRecord(currentPageData()?.word ?? '');
      }
  
      _isOriginAudioPlaying = false;
      _isRecordAudioPlaying = false;
    }
  
53e9e6db   吴启风   feat:绘本语音评测逻辑
351
352
353
354
355
    void _stopAudio() async {
      await audioPlayer.stop();
      _isOriginAudioPlaying = false;
      _isRecordAudioPlaying = false;
    }
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
356
357
358
359
360
  
    void _onVoiceXsStateChange(
      OnXSVoiceStateChangeEvent event,
        Emitter<ReadingPageState> emitter
    ) async {
ad37b653   吴启风   feat:1、完成微信支付;2、商...
361
      emitter(XSVoiceTestState());
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...
362
    }
1dc46cf7   吴启风   feat:绘本ui
363
  }
065022b7   吴启风   feat:绘本评测反馈弹窗+原音播...