Commit 83628316 authored by Tt's avatar Tt

录音部分完成

parent 4bd56c06
This diff is collapsed.
...@@ -3,6 +3,8 @@ import { MyCocosSceneComponent } from "../script/MyCocosSceneComponent_dg18_tian ...@@ -3,6 +3,8 @@ import { MyCocosSceneComponent } from "../script/MyCocosSceneComponent_dg18_tian
import pg from "./pg_dg18_tianci"; import pg from "./pg_dg18_tianci";
import dg_xiaodi from "./dg18_tianci_xiaodi"; import dg_xiaodi from "./dg18_tianci_xiaodi";
let win: any = window;
let courseware = win.courseware;
const { ccclass, property } = cc._decorator; const { ccclass, property } = cc._decorator;
@ccclass @ccclass
...@@ -28,10 +30,10 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -28,10 +30,10 @@ export default class SceneComponent extends MyCocosSceneComponent {
this.initData(); this.initData();
this.initView(); this.initView();
this.initEvent(); this.initEvent();
pg.view.find(this, 'layout_xiaodi').getComponent(dg_xiaodi).playEnter(this.data.npcAudio).then(() => { // pg.view.find(this, 'layout_xiaodi').getComponent(dg_xiaodi).playEnter(this.data.npcAudio).then(() => {
this.initGame();
})
// this.initGame(); // this.initGame();
// })
this.initGame();
} }
_cantouch = null; _cantouch = null;
...@@ -56,13 +58,24 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -56,13 +58,24 @@ export default class SceneComponent extends MyCocosSceneComponent {
@property(cc.Node) question_img: cc.Node = null; @property(cc.Node) question_img: cc.Node = null;
@property(cc.Node) label_title: cc.Node = null; @property(cc.Node) label_title: cc.Node = null;
@property(cc.Node) btn_vioce: cc.Node = null;
@property(cc.Node) btn_restart: cc.Node = null;
@property(cc.Node) label_tip_wait: cc.Node = null;
@property(cc.Node) label_tip_ing: cc.Node = null;
@property(cc.Node) rich_content: cc.Node = null;
// 初始化页面 // 初始化页面
initView() { initView() {
this.btn_vioce.active = false;
this.btn_restart.active = false;
this.label_tip_wait.active = false;
this.label_tip_ing.active = false;
this.rich_content.active = false;
} }
// 事件监听 // 事件监听
initEvent() { initEvent() {
pg.view.touchOn(this.btn_vioce, this.onTouchRecord, this);
pg.view.touchOn(pg.view.find(this.btn_vioce,'img_ing'), this.onTouchRecordEnd, this);
} }
// 初始化游戏 // 初始化游戏
initGame() { initGame() {
...@@ -83,17 +96,137 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -83,17 +96,137 @@ export default class SceneComponent extends MyCocosSceneComponent {
} }
this.current++ this.current++
this.successArr = []; this.successArr = [];
this.targetTime = 10//this.currentQuestion.targetTime
this.updateQuestion(); this.updateQuestion();
} }
updateQuestion() { updateQuestion() {
// 图片部分 // 图片部分
pg.view.setNetImg(this.question_img, this.currentQuestion.img, { w: 1030, h: 720 }) pg.view.setNetImg(this.question_img, this.currentQuestion.img, { w: 1030, h: 720 })
pg.view.setString(this.label_title,this.currentQuestion.title); pg.view.setString(this.label_title, this.currentQuestion.title);
this.btn_vioce.active = true;
pg.view.visible(pg.view.find(this.btn_vioce, 'img_ing'), false)
this.label_tip_wait.active = true;
}
//按钮触发,使用的方法
private targetTime: number;
private recordAudio: string;
private startTime: number;
private recording: boolean;
private voiceTouchForbid: boolean;
private count: number;
showTimeStart() {
if (!this.targetTime) return;
let nowTime = new Date().getTime();
this.startTime = nowTime;
}
showTimeUpdate() {
if (!this.startTime) return;
let nowTime = new Date().getTime();
let subTime = this.targetTime - (nowTime - this.startTime) / 1000 + 1;
if (subTime < 0) subTime = 0;
console.log('subTime->' + subTime)
this.label_tip_wait.active = false
this.label_tip_ing.active = true;
pg.view.visible(pg.view.find(this.btn_vioce, 'img_ing'), true)
let img_ing = pg.view.find(this.btn_vioce, 'img_ing')
img_ing.getComponent(cc.Sprite).fillRange = subTime / this.targetTime;
if (subTime == 0) {
this.onTouchRecordEnd();
}
}
showTimeEnd() {
this.startTime = null;
this.label_tip_wait.active = false
this.label_tip_ing.active = false;
pg.view.visible(pg.view.find(this.btn_vioce, 'img_ing'), false)
}
async onTouchRecord() {
if (this.recording) return;
if (this.recordAudio) return;
this.recording = true;
this.recordAudio = '';
this.voiceTouchForbid = true;
this.showTimeStart();
this.log("-----------------------startTest")
cc.audioEngine.stopAllEffects();
courseware && courseware.startRecord(1); //开始录音
}
async onTouchRecordEnd() {
if (!this.recording) return;
this.showTimeEnd();
let testData = {
"text": "Hello every one my name is world",
"audio": "https://staging-teach.cdn.ireadabc.com/6ee3ccaa831a884f02c6a75c6f647cb5.wav"
}
if (!courseware) {
return this.recordEnd(testData)
}
this.log("---------------courseware.stopTest:")
courseware && courseware.stopRecord(0, (data) => {
this.log("---------------stopRecord:" + data)
// res = JSON.parse(res);
// res.exampleAudio = this.currentQuestion.startAudio ? this.currentQuestion.startAudio : "";
// res.exampleText = this.currentQuestion.keyword ? this.currentQuestion.keyword : "";
// this.recrodEnd(isClear, res);
data = JSON.parse(data);
console.log("data: " + JSON.stringify(data));
this.recordEnd(data);
});//结束录音
}
recordEnd(data) {
const keyTextArr = this.currentQuestion.keyword.split(",");
const dataTextArr = data.text.split(" ");
let colorNormal = '#313131';
let colorRight = '#1D8B38';
let str = '';
let isRight = false;
for (let i = 0; i < dataTextArr.length; i++) {
const word = dataTextArr[i];
let color = colorNormal;
if (keyTextArr.indexOf(word) != -1) {
color = colorRight;
isRight = true;
}
str += `<color=${color}>${word}</color> `
}
this.rich_content.active = true;
const richText = this.rich_content.getComponent(cc.RichText);
richText.string = str;
}
showData(data) {
let list = data.result.sentences[0].details;
// 列表处理
let str = "";
let keystr = "";
for (let i = 0; i < list.length; i++) {
let li = list[i];
let s = "";
if (li.overall > 40) {
s = `<color=#000000> ${li.word} </c>`
} else {
s = `<color=#FF0000> ${li.word} </c>`
}
keystr += li.word + 2
str += s;
}
// this.label_rich.getComponent(cc.RichText).fontSize = num;
// this.label_rich.getComponent(cc.RichText).lineHeight = num;
// this.label_rich.getComponent(cc.RichText).string = str;
} }
private waitTime: number; private waitTime: number;
update(dt) { update(dt) {
this.showTimeUpdate();
if (!this.waitTime) this.waitTime = 8; if (!this.waitTime) this.waitTime = 8;
this.waitTime -= dt; this.waitTime -= dt;
if (this.waitTime < 0) { if (this.waitTime < 0) {
......
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