Blame view

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