2eb67dd4
liangchengyou
feat:调整代码
|
1
|
import 'package:audioplayers/audioplayers.dart';
|
688b7b5c
liangchengyou
feat:更新文件结构
|
2
|
import 'package:flutter/cupertino.dart';
|
2eb67dd4
liangchengyou
feat:调整代码
|
3
4
|
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
|
688b7b5c
liangchengyou
feat:更新文件结构
|
5
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2eb67dd4
liangchengyou
feat:调整代码
|
6
|
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
354ac7e6
吴启风
feat:隐藏视频跟读入口、底部草...
|
7
|
import 'package:permission_handler/permission_handler.dart';
|
aeafd474
吴启风
feat:选择题作答后播放音效&答...
|
8
|
import 'package:wow_english/common/extension/string_extension.dart';
|
2eb67dd4
liangchengyou
feat:调整代码
|
9
10
11
|
import 'package:wow_english/common/request/dao/listen_dao.dart';
import 'package:wow_english/common/request/exception.dart';
import 'package:wow_english/models/course_process_entity.dart';
|
22f36232
吴启风
feat:过渡页-练习环节
|
12
13
14
|
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';
|
2eb67dd4
liangchengyou
feat:调整代码
|
15
|
import 'package:wow_english/utils/loading.dart';
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
16
|
import 'package:wow_english/utils/toast_util.dart';
|
7652f701
liangchengyou
feat:课程购买UI逻辑
|
17
|
|
354ac7e6
吴启风
feat:隐藏视频跟读入口、底部草...
|
18
|
import '../../../common/permission/permissionRequestPage.dart';
|
22f36232
吴启风
feat:过渡页-练习环节
|
19
|
import '../../../route/route.dart';
|
354ac7e6
吴启风
feat:隐藏视频跟读入口、底部草...
|
20
|
|
7652f701
liangchengyou
feat:课程购买UI逻辑
|
21
|
part 'topic_picture_event.dart';
|
688b7b5c
liangchengyou
feat:更新文件结构
|
22
|
part 'topic_picture_state.dart';
|
7652f701
liangchengyou
feat:课程购买UI逻辑
|
23
|
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
24
25
26
27
28
29
30
31
32
33
34
|
enum VoicePlayState {
///未知
unKnow,
///播放中
playing,
///播放完成
completed,
///播放终止
stop
}
|
22f36232
吴启风
feat:过渡页-练习环节
|
35
|
class TopicPictureBloc extends BaseSectionBloc<TopicPictureEvent, TopicPictureState> {
|
624214d0
liangchengyou
feat:看题选字/选图UI和部分逻辑
|
36
37
38
|
final PageController pageController;
|
608c05b4
liangchengyou
feat:兑换课程
|
39
|
final String courseLessonId;
|
624214d0
liangchengyou
feat:看题选字/选图UI和部分逻辑
|
40
41
42
|
int _currentPage = 0;
|
2eb67dd4
liangchengyou
feat:调整代码
|
43
44
45
46
47
48
49
|
int _selectItem = -1;
CourseProcessEntity? _entity;
///正在评测
bool _isVoicing = false;
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
50
51
52
|
///正在播放音频
VoicePlayState _voicePlayState = VoicePlayState.unKnow;
|
aeafd474
吴启风
feat:选择题作答后播放音效&答...
|
53
54
55
56
57
58
59
60
61
62
|
// 是否是回答(选择)结果音效
bool _isResultSoundPlaying = false;
bool get isResultSoundPlaying => _isResultSoundPlaying;
// 答对播放音效时禁止任何点击(选择)操作
bool _forbiddenWhenCorrect = false;
bool get forbiddenWhenCorrect => _forbiddenWhenCorrect;
|
2eb67dd4
liangchengyou
feat:调整代码
|
63
|
CourseProcessEntity? get entity => _entity;
|
624214d0
liangchengyou
feat:看题选字/选图UI和部分逻辑
|
64
65
66
67
68
|
int get currentPage => _currentPage + 1;
int get selectItem => _selectItem;
|
2eb67dd4
liangchengyou
feat:调整代码
|
69
70
|
bool get isVoicing => _isVoicing;
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
71
72
|
VoicePlayState get voicePlayState => _voicePlayState;
|
e3c2820c
liangchengyou
feat:先声SDK逻辑调整
|
73
|
late MethodChannel methodChannel;
|
2eb67dd4
liangchengyou
feat:调整代码
|
74
|
|
e3c2820c
liangchengyou
feat:先声SDK逻辑调整
|
75
|
late AudioPlayer audioPlayer;
|
2eb67dd4
liangchengyou
feat:调整代码
|
76
|
|
354ac7e6
吴启风
feat:隐藏视频跟读入口、底部草...
|
77
78
79
|
final BuildContext context;
TopicPictureBloc(this.context, this.pageController, this.courseLessonId) : super(TopicPictureInitial()) {
|
624214d0
liangchengyou
feat:看题选字/选图UI和部分逻辑
|
80
|
on<CurrentPageIndexChangeEvent>(_pageControllerChange);
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
81
82
83
|
on<VoicePlayStateChangeEvent>(_voicePlayStateChange);
on<XSVoiceResultEvent>(_voiceXsResult);
on<XSVoiceInitEvent>(_initVoiceSdk);
|
624214d0
liangchengyou
feat:看题选字/选图UI和部分逻辑
|
84
|
on<SelectItemEvent>(_selectItemLoad);
|
e3c2820c
liangchengyou
feat:先声SDK逻辑调整
|
85
|
on<RequestDataEvent>(_requestData);
|
ae77d87f
吴启风
feat:fix语音题无法手动停止...
|
86
|
on<XSVoiceStartEvent>(_voiceXsStart);
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
87
|
on<XSVoiceStopEvent>(_voiceXsStop);
|
aeafd474
吴启风
feat:选择题作答后播放音效&答...
|
88
|
on<VoicePlayEvent>(_questionVoicePlay);
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
89
|
on<InitBlocEvent>((event, emit) {
|
e3c2820c
liangchengyou
feat:先声SDK逻辑调整
|
90
91
|
//音频播放器
audioPlayer = AudioPlayer();
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
92
|
audioPlayer.onPlayerStateChanged.listen((event) async {
|
aeafd474
吴启风
feat:选择题作答后播放音效&答...
|
93
94
95
96
97
98
|
debugPrint('播放状态变化 _voicePlayState=$_voicePlayState event=$event _isResultSoundPlaying=$_isResultSoundPlaying _forbiddenWhenCorrect=$_forbiddenWhenCorrect');
if (_isResultSoundPlaying) {
if (event != PlayerState.playing) {
_isResultSoundPlaying = false;
if (_forbiddenWhenCorrect) {
_forbiddenWhenCorrect = false;
|
77d53ec4
吴启风
feat:练习音效控制优化
|
99
100
|
debugPrint('播放完成后解除禁止');
if (event == PlayerState.completed) {
|
22f36232
吴启风
feat:过渡页-练习环节
|
101
102
103
104
105
|
if (isLastPage()) {
showStepPage();
} else {
// 答对后且播放完自动翻页
pageController.nextPage(
|
8df5fbf9
吴启风
feat:单元列表页由于ios嵌套...
|
106
|
duration: const Duration(milliseconds: 250),
|
22f36232
吴启风
feat:过渡页-练习环节
|
107
108
109
|
curve: Curves.ease,
);
}
|
77d53ec4
吴启风
feat:练习音效控制优化
|
110
|
}
|
aeafd474
吴启风
feat:选择题作答后播放音效&答...
|
111
112
113
114
115
116
117
118
119
120
121
|
}
}
} else {
if (event == PlayerState.completed) {
debugPrint('播放完成');
_voicePlayState = VoicePlayState.completed;
}
if (event == PlayerState.stopped) {
debugPrint('播放结束');
_voicePlayState = VoicePlayState.stop;
}
|
e3c2820c
liangchengyou
feat:先声SDK逻辑调整
|
122
|
|
aeafd474
吴启风
feat:选择题作答后播放音效&答...
|
123
124
125
126
127
128
129
130
|
if (event == PlayerState.playing) {
debugPrint('正在播放中');
_voicePlayState = VoicePlayState.playing;
}
if (isClosed) {
return;
}
add(VoicePlayStateChangeEvent());
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
131
|
}
|
e3c2820c
liangchengyou
feat:先声SDK逻辑调整
|
132
133
|
});
|
53e9e6db
吴启风
feat:绘本语音评测逻辑
|
134
|
methodChannel = const MethodChannel('wow_english/sing_sound_method_channel');
|
e3c2820c
liangchengyou
feat:先声SDK逻辑调整
|
135
|
methodChannel.setMethodCallHandler((call) async {
|
b90a1518
liangchengyou
feat:练习接口逻辑完成
|
136
|
if (call.method == 'voiceResult') {//评测结果
|
e3c2820c
liangchengyou
feat:先声SDK逻辑调整
|
137
|
add(XSVoiceResultEvent(call.arguments));
|
b90a1518
liangchengyou
feat:练习接口逻辑完成
|
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
return;
}
if (call.method == 'voiceStart') {//评测开始
if (kDebugMode) {
print('评测开始');
}
return;
}
if (call.method == 'voiceEnd') {//评测结束
if (kDebugMode) {
print('评测结束');
}
return;
}
if (call.method == 'voiceFail') {//评测失败
EasyLoading.showToast('评测失败');
return;
|
e3c2820c
liangchengyou
feat:先声SDK逻辑调整
|
158
159
|
}
});
|
2eb67dd4
liangchengyou
feat:调整代码
|
160
|
});
|
624214d0
liangchengyou
feat:看题选字/选图UI和部分逻辑
|
161
162
163
|
}
@override
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
164
|
Future<void> close(){
|
624214d0
liangchengyou
feat:看题选字/选图UI和部分逻辑
|
165
|
pageController.dispose();
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
166
|
audioPlayer.release();
|
2eb67dd4
liangchengyou
feat:调整代码
|
167
|
audioPlayer.dispose();
|
aeafd474
吴启风
feat:选择题作答后播放音效&答...
|
168
169
|
_isResultSoundPlaying = false;
_forbiddenWhenCorrect = false;
|
6b0947ca
吴启风
feat:绘本增加initVoic...
|
170
|
_voiceXsCancel();
|
624214d0
liangchengyou
feat:看题选字/选图UI和部分逻辑
|
171
172
173
|
return super.close();
}
|
e3c2820c
liangchengyou
feat:先声SDK逻辑调整
|
174
|
///请求数据
|
2eb67dd4
liangchengyou
feat:调整代码
|
175
176
177
|
void _requestData(RequestDataEvent event,Emitter<TopicPictureState> emitter) async {
try {
await loading(() async {
|
608c05b4
liangchengyou
feat:兑换课程
|
178
|
_entity = await ListenDao.process(courseLessonId);
|
2eb67dd4
liangchengyou
feat:调整代码
|
179
180
181
182
|
emitter(RequestDataState());
});
} catch (e) {
if (e is ApiException) {
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
183
|
showToast(e.message??'请求失败,请检查网络连接');
|
2eb67dd4
liangchengyou
feat:调整代码
|
184
185
186
187
|
}
}
}
|
e3c2820c
liangchengyou
feat:先声SDK逻辑调整
|
188
|
///页面切换
|
624214d0
liangchengyou
feat:看题选字/选图UI和部分逻辑
|
189
|
void _pageControllerChange(CurrentPageIndexChangeEvent event,Emitter<TopicPictureState> emitter) async {
|
aeafd474
吴启风
feat:选择题作答后播放音效&答...
|
190
191
192
193
|
await closePlayerResource();
debugPrint('翻页 $_currentPage->${event.pageIndex}');
if (_currentPage == _entity?.topics?.length) {
return;
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
194
|
}
|
aeafd474
吴启风
feat:选择题作答后播放音效&答...
|
195
|
_currentPage = event.pageIndex;
|
2eb67dd4
liangchengyou
feat:调整代码
|
196
|
final topics = _entity?.topics?[_currentPage];
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
197
|
if (topics?.type != 3 && topics?.type != 4) {
|
2eb67dd4
liangchengyou
feat:调整代码
|
198
199
200
|
if (topics?.audioUrl != null) {
final urlStr = topics?.audioUrl??'';
if (urlStr.isNotEmpty) {
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
201
202
|
debugPrint(urlStr);
await audioPlayer.play(UrlSource(urlStr));
|
2eb67dd4
liangchengyou
feat:调整代码
|
203
204
|
}
}
|
2eb67dd4
liangchengyou
feat:调整代码
|
205
|
}
|
b90a1518
liangchengyou
feat:练习接口逻辑完成
|
206
|
_selectItem = -1;
|
624214d0
liangchengyou
feat:看题选字/选图UI和部分逻辑
|
207
208
209
|
emitter(CurrentPageIndexState());
}
|
e3c2820c
liangchengyou
feat:先声SDK逻辑调整
|
210
|
///选择
|
624214d0
liangchengyou
feat:看题选字/选图UI和部分逻辑
|
211
|
void _selectItemLoad(SelectItemEvent event,Emitter<TopicPictureState> emitter) async {
|
aeafd474
吴启风
feat:选择题作答后播放音效&答...
|
212
213
214
|
if (_forbiddenWhenCorrect) {
return;
}
|
624214d0
liangchengyou
feat:看题选字/选图UI和部分逻辑
|
215
|
_selectItem = event.selectIndex;
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
216
217
218
|
CourseProcessTopics? topics = _entity?.topics?[_currentPage];
CourseProcessTopicsTopicAnswerList? answerList = topics?.topicAnswerList?[_selectItem];
if (answerList?.correct == 0) {
|
aeafd474
吴启风
feat:选择题作答后播放音效&答...
|
219
220
|
_playResultSound(false);
// showToast('继续加油哦',duration: const Duration(seconds: 2));
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
221
|
} else {
|
aeafd474
吴启风
feat:选择题作答后播放音效&答...
|
222
223
|
_playResultSound(true);
// showToast('恭喜你,答对啦!',duration: const Duration(seconds: 2));
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
224
|
}
|
624214d0
liangchengyou
feat:看题选字/选图UI和部分逻辑
|
225
|
emitter(SelectItemChangeState());
|
7652f701
liangchengyou
feat:课程购买UI逻辑
|
226
|
}
|
2eb67dd4
liangchengyou
feat:调整代码
|
227
|
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
228
229
230
231
232
|
///初始化SDK
_initVoiceSdk(XSVoiceInitEvent event,Emitter<TopicPictureState> emitter) async {
methodChannel.invokeMethod('initVoiceSdk',event.data);
}
|
e3c2820c
liangchengyou
feat:先声SDK逻辑调整
|
233
|
///先声测试
|
ae77d87f
吴启风
feat:fix语音题无法手动停止...
|
234
|
void _voiceXsStart(XSVoiceStartEvent event,Emitter<TopicPictureState> emitter) async {
|
53e9e6db
吴启风
feat:绘本语音评测逻辑
|
235
|
await audioPlayer.stop();
|
354ac7e6
吴启风
feat:隐藏视频跟读入口、底部草...
|
236
237
238
239
240
|
// 调用封装好的权限检查和请求方法
bool result = await permissionCheckAndRequest(
context,
Permission.microphone,
"录音"
|
e3c2820c
liangchengyou
feat:先声SDK逻辑调整
|
241
|
);
|
354ac7e6
吴启风
feat:隐藏视频跟读入口、底部草...
|
242
243
244
245
246
247
248
249
250
251
252
253
|
if (result) {
methodChannel.invokeMethod(
'startVoice',
{
'word': event.testWord,
'type': event.type,
'userId': event.userId.toString()
}
);
_isVoicing = true;
emitter(XSVoiceTestState());
}
|
e3c2820c
liangchengyou
feat:先声SDK逻辑调整
|
254
255
|
}
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
256
257
258
259
260
|
///终止评测
void _voiceXsStop(XSVoiceStopEvent event,Emitter<TopicPictureState> emitter) async {
methodChannel.invokeMethod('stopVoice');
}
|
6b0947ca
吴启风
feat:绘本增加initVoic...
|
261
262
263
264
265
|
///取消评测(用于处理退出页面后录音未停止等异常情况的保护操作)
void _voiceXsCancel() {
methodChannel.invokeMethod('cancelVoice');
}
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
266
|
///先声评测结果
|
e3c2820c
liangchengyou
feat:先声SDK逻辑调整
|
267
268
|
void _voiceXsResult(XSVoiceResultEvent event,Emitter<TopicPictureState> emitter) async {
final Map args = event.message as Map;
|
cb38bc90
liangchengyou
feat:视频跟读逻辑处理
|
269
270
271
|
final result = args['result'] as Map;
final overall = result['overall'].toString();
showToast('测评成功,分数是$overall',duration: const Duration(seconds: 5));
|
2eb67dd4
liangchengyou
feat:调整代码
|
272
|
_isVoicing = false;
|
e3c2820c
liangchengyou
feat:先声SDK逻辑调整
|
273
|
emitter(XSVoiceTestState());
|
22f36232
吴启风
feat:过渡页-练习环节
|
274
275
276
|
if (isLastPage()) {
showStepPage();
}
|
2eb67dd4
liangchengyou
feat:调整代码
|
277
|
}
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
278
|
|
aeafd474
吴启风
feat:选择题作答后播放音效&答...
|
279
|
// 暂时没用上
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
280
281
282
283
|
void _voicePlayStateChange(VoicePlayStateChangeEvent event,Emitter<TopicPictureState> emitter) async {
emitter(VoicePlayStateChange());
}
|
aeafd474
吴启风
feat:选择题作答后播放音效&答...
|
284
285
286
|
// 题目音频播放
void _questionVoicePlay(VoicePlayEvent event,Emitter<TopicPictureState> emitter) async {
if (_forbiddenWhenCorrect) {
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
287
288
|
return;
}
|
aeafd474
吴启风
feat:选择题作答后播放音效&答...
|
289
290
|
_forbiddenWhenCorrect = false;
await closePlayerResource();
|
3c1d5c64
liangchengyou
feat:练习功能完成
|
291
292
293
294
|
final topics = _entity?.topics?[_currentPage];
final urlStr = topics?.audioUrl??'';
await audioPlayer.play(UrlSource(urlStr));
}
|
aeafd474
吴启风
feat:选择题作答后播放音效&答...
|
295
296
297
298
299
300
301
|
Future<void> closePlayerResource() async {
if (voicePlayState == VoicePlayState.playing || _isResultSoundPlaying) {
await audioPlayer.stop();
}
}
|
22f36232
吴启风
feat:过渡页-练习环节
|
302
|
///播放选择结果音效
|
aeafd474
吴启风
feat:选择题作答后播放音效&答...
|
303
|
void _playResultSound(bool isCorrect) async {
|
77d53ec4
吴启风
feat:练习音效控制优化
|
304
305
306
307
308
|
// await audioPlayer.stop();
if (audioPlayer.state == PlayerState.playing && _isResultSoundPlaying == false) {
_voicePlayState = VoicePlayState.stop;
}
debugPrint("_playResultSound isCorrect=$isCorrect");
|
aeafd474
吴启风
feat:选择题作答后播放音效&答...
|
309
310
311
312
313
314
315
316
|
_isResultSoundPlaying = true;
_forbiddenWhenCorrect = isCorrect;
if (isCorrect) {
await audioPlayer.play(AssetSource('correct_voice'.assetMp3));
} else {
await audioPlayer.play(AssetSource('incorrect_voice'.assetMp3));
}
}
|
22f36232
吴启风
feat:过渡页-练习环节
|
317
318
319
320
321
322
323
324
325
326
327
328
|
///是否是最后一页
bool isLastPage() {
return currentPage == _entity?.topics?.length;
}
///展示过渡页
void showStepPage() {
///如果最后一页是语音问答题,评测完后自动翻页
sectionComplete(() {
popPage(
data:{
|
66a7e3e7
吴启风
feat:退出课堂和结束课堂优化
|
329
|
'currentStep':currentPage,
|
22f36232
吴启风
feat:过渡页-练习环节
|
330
|
'courseLessonId':courseLessonId,
|
66a7e3e7
吴启风
feat:退出课堂和结束课堂优化
|
331
|
'isCompleted': true,
|
22f36232
吴启风
feat:过渡页-练习环节
|
332
333
334
|
'nextSection': true
});
}, againSectionTap: () {
|
22f36232
吴启风
feat:过渡页-练习环节
|
335
336
337
|
pageController.jumpToPage(0);
});
}
|
7652f701
liangchengyou
feat:课程购买UI逻辑
|
338
|
}
|