Commit 94759aba authored by Tt's avatar Tt

fix(语音功能): 修复语音按钮点击和录音关闭逻辑问题

parent d6b97261
...@@ -1353,9 +1353,7 @@ ...@@ -1353,9 +1353,7 @@
"__id__": 25 "__id__": 25
}, },
"_enabled": true, "_enabled": true,
"_clip": { "_clip": null,
"__uuid__": "8bbf063f-fd82-4e2c-b54d-0997b6004b2c"
},
"_volume": 1, "_volume": 1,
"_mute": false, "_mute": false,
"_loop": false, "_loop": false,
...@@ -1434,9 +1432,7 @@ ...@@ -1434,9 +1432,7 @@
"__id__": 27 "__id__": 27
}, },
"_enabled": true, "_enabled": true,
"_clip": { "_clip": null,
"__uuid__": "36ab29df-0eff-40ad-bd25-daa3d15f55cd"
},
"_volume": 1, "_volume": 1,
"_mute": false, "_mute": false,
"_loop": false, "_loop": false,
...@@ -3758,7 +3754,7 @@ ...@@ -3758,7 +3754,7 @@
"__id__": 111 "__id__": 111
} }
], ],
"_active": false, "_active": true,
"_components": [ "_components": [
{ {
"__id__": 113 "__id__": 113
...@@ -4485,8 +4481,10 @@ ...@@ -4485,8 +4481,10 @@
"_fontSize": 130, "_fontSize": 130,
"_lineHeight": 130, "_lineHeight": 130,
"_enableWrapText": false, "_enableWrapText": false,
"_N$file": null, "_N$file": {
"_isSystemFontUsed": true, "__uuid__": "23f9661e-5c67-41a5-abdf-65a4b99ef31b"
},
"_isSystemFontUsed": false,
"_spacingX": 0, "_spacingX": 0,
"_batchAsBitmap": false, "_batchAsBitmap": false,
"_styleFlags": 0, "_styleFlags": 0,
......
...@@ -692,6 +692,11 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -692,6 +692,11 @@ export default class SceneComponent extends MyCocosSceneComponent {
this.renderProgressLabel(label_progress); this.renderProgressLabel(label_progress);
} }
onTouchVoice() {
// 使用pg.ts的API播放音频
const currentData = this.currentTouchNode.data;
pg.audio.playAudioByUrl(currentData.audioUrl);
}
/** /**
* 处理语音按钮的显示和事件绑定 * 处理语音按钮的显示和事件绑定
* @param btn_voice 语音按钮节点 * @param btn_voice 语音按钮节点
...@@ -702,10 +707,7 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -702,10 +707,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
// 如果数据中有audio,显示语音按钮 // 如果数据中有audio,显示语音按钮
pg.view.visible(btn_voice, true); pg.view.visible(btn_voice, true);
// 绑定点击事件播放音频 // 绑定点击事件播放音频
pg.view.touchOn(btn_voice, () => { pg.view.touchOn(btn_voice, this.onTouchVoice, this);
// 使用pg.ts的API播放音频
pg.audio.playAudioByUrl(currentData.audioUrl);
}, this);
} else { } else {
// 没有音频则隐藏按钮 // 没有音频则隐藏按钮
pg.view.visible(btn_voice, false); pg.view.visible(btn_voice, false);
...@@ -809,11 +811,26 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -809,11 +811,26 @@ export default class SceneComponent extends MyCocosSceneComponent {
this.stopVoiceAni(); this.stopVoiceAni();
} }
} }
onTouchSpeakClose() {
private onTouchSpeakCloseClosing: boolean;
async onTouchSpeakClose() {
if (this.onTouchSpeakCloseClosing) return;
this.onTouchSpeakCloseClosing = true;
// 检查是否在录音状态
const btn_stop = pg.view.find(this.layout_speak, 'btn_stop');
if (btn_stop && btn_stop.active) {
// 如果在录音状态,先执行停止录音操作
await this.onTouchRecordEnd()
}
this.onTouchSpeakCloseClosing = false;
this.showCard(false); this.showCard(false);
// 获取最后一次录音数据 // 获取最后一次录音数据
const lastVoice = Game.getIns().player.getLastVoice(); const lastVoice = Game.getIns().player.getLastVoice();
if (lastVoice.questionId != this.touchData.id) {
this.onRecordFail();
return;
}
// 如果有录音数据,则使用其分数 // 如果有录音数据,则使用其分数
if (lastVoice && lastVoice.result && lastVoice.questionId == this.touchData.id) { if (lastVoice && lastVoice.result && lastVoice.questionId == this.touchData.id) {
const score = lastVoice.result.overall; const score = lastVoice.result.overall;
...@@ -935,90 +952,97 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -935,90 +952,97 @@ export default class SceneComponent extends MyCocosSceneComponent {
* 停止倒计时,显示播放和重新录制按钮,结束录音 * 停止倒计时,显示播放和重新录制按钮,结束录音
*/ */
private onTouchRecordEnd() { private onTouchRecordEnd() {
// 使用更新后的showCard方法,传入'recorded'状态 return new Promise(async (resolve, reject) => {
this.showCard(true, 'recorded'); // 使用更新后的showCard方法,传入'recorded'状态
this.showCard(true, 'recorded');
// 隐藏倒计时并设置一个很大的值防止倒计时继续
this.countDown = 999999; // 隐藏倒计时并设置一个很大的值防止倒计时继续
this.countDown = 999999;
// 重置录音开始时间
this.recordStartTime = 0; // 重置录音开始时间
this.recordStartTime = 0;
let testData = {
"audioUrl": "https://staging-teach.cdn.ireadabc.com/a8ea7bb153a46941e6f28b7d0dda49f4.mp3", let testData = {
"eof": 1, "audioUrl": "https://staging-teach.cdn.ireadabc.com/a8ea7bb153a46941e6f28b7d0dda49f4.mp3",
"file": "/storage/emulated/0/Android/data/com.example.oxford/cache/shengtong/gradeVoice.mp3", "eof": 1,
"result": { "file": "/storage/emulated/0/Android/data/com.example.oxford/cache/shengtong/gradeVoice.mp3",
"duration": "2.630", "result": {
"fluency": 84, "duration": "2.630",
"integrity": 100, "fluency": 84,
"kernel_version": "3.7.1", "integrity": 100,
"overall": 68, "kernel_version": "3.7.1",
"pronunciation": 88, "overall": 68,
"resource_version": "2.3.11", "pronunciation": 88,
"rhythm": 46, "resource_version": "2.3.11",
"sentences": [ "rhythm": 46,
{ "sentences": [
"beginIndex": 0, {
"details": [ "beginIndex": 0,
{ "details": [
"charType": 0, {
"end": 0, "charType": 0,
"overall": 69, "end": 0,
"prominence": 0, "overall": 69,
"start": 0, "prominence": 0,
"word": "day" "start": 0,
}, "word": "day"
{ },
"charType": 0, {
"end": 0, "charType": 0,
"overall": 100, "end": 0,
"prominence": 0, "overall": 100,
"start": 0, "prominence": 0,
"word": "day" "start": 0,
}, "word": "day"
{ },
"charType": 0, {
"end": 0, "charType": 0,
"overall": 100, "end": 0,
"prominence": 0, "overall": 100,
"start": 0, "prominence": 0,
"word": "up" "start": 0,
}, "word": "up"
{ },
"charType": 0, {
"end": 0, "charType": 0,
"overall": 100, "end": 0,
"prominence": 0, "overall": 100,
"start": 0, "prominence": 0,
"word": "day" "start": 0,
}, "word": "day"
{ },
"charType": 0, {
"end": 0, "charType": 0,
"overall": 100, "end": 0,
"prominence": 0, "overall": 100,
"start": 0, "prominence": 0,
"word": "up" "start": 0,
} "word": "up"
], }
"end": 0, ],
"endIndex": 0, "end": 0,
"overall": 88, "endIndex": 0,
"sentence": "day day up day up", "overall": 88,
"start": 0 "sentence": "day day up day up",
} "start": 0
], }
"speed": 170 ],
}, "speed": 170
"tokenId": "632be7a73327935c2b000005" },
} "tokenId": "632be7a73327935c2b000005"
if (!courseware) return this.recrodEnd(testData) }
// await pg.time.delay(5); if (!courseware) {
courseware && courseware.stopTest((data) => { await this.recrodEnd(testData)
data = JSON.parse(data); resolve(testData);
this.recrodEnd(data); return;
}); //结束录音 }
// await pg.time.delay(5);
courseware && courseware.stopTest(async (data) => {
data = JSON.parse(data);
await this.recrodEnd(data);
resolve(data);
}); //结束录音
})
} }
onTouchPlayRecord() { onTouchPlayRecord() {
const voice = Game.getIns().player.getLastVoice(); const voice = Game.getIns().player.getLastVoice();
...@@ -1092,38 +1116,16 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -1092,38 +1116,16 @@ export default class SceneComponent extends MyCocosSceneComponent {
this.updateTips(null); this.updateTips(null);
return; return;
} else { } else {
// 单词不正确 // 首次尝试失败,显示再试一次提示
if (this.tryData.some(td => td.id == this.touchData.id)) { this.updateTips(TIPS_STATE.TRYAGAIN);
// 已经尝试过的题目,显示加油提示 this.tryData.push(this.touchData);
this.updateTips(TIPS_STATE.COMEON); this.tryAgain = true;
if (this.tryAgain) { // 将火苗节点重新添加到活跃列表中,以便再次尝试
// 第二次尝试后,无论结果如何都进入下一题 if (this.touchData && this.touchData.parent && !this.activeNodes.includes(this.touchData.parent)) {
this.tryAgain = false; this.activeNodes.push(this.touchData.parent);
// 如果有父节点(火苗节点),隐藏它
if (this.touchData && this.touchData.parent) {
this.touchData.parent.active = false;
}
await pg.time.delay(3);
this.showCard(false);
this.updateTips(null);
this.recordTouching = false;
this.onRecordFail();
return;
}
} else {
// 首次尝试失败,显示再试一次提示
this.updateTips(TIPS_STATE.TRYAGAIN);
this.tryData.push(this.touchData);
this.tryAgain = true;
// 将火苗节点重新添加到活跃列表中,以便再次尝试
if (this.touchData && this.touchData.parent && !this.activeNodes.includes(this.touchData.parent)) {
this.activeNodes.push(this.touchData.parent);
}
} }
Game.getIns().player.addVoice(data);
} }
// 延迟后隐藏提示 // 延迟后隐藏提示
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment