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