import httpRequestApi from '../../utils/APIClient'; const aiengine = require('../../utils/ChivoxAiEngine') const sha1 = require('../../utils/sha1'); import { formatDate } from '../../utils/util'; Page({ data: { title: '', id: '', img: '', fullScreenBtn: false, playBtn: true, gesture: true, muted: false, gesture: false, centerBtn: true, recordFlag: 0, recordSource: '', videoCtr: 'recordingVideoEnd', btnFlag: false, btnImgFlag: false, microphonePng: '../../static/index/microphone.png', recordingGif: '../../static/index/readingNow.gif', videoUrl: '', readingText: '', videoList: [], pageNo: 1, totalSize: 0, nextMargin: getApp().globalData.nextMargin, lowerThresHold: 100, isVideoListShow: true, overall: '', // 评测总分 integrity: '', //完成度 tone: '', //语调声调 fluency: '', //流利度 accuracy: '', // 正确分,发音分 star: [0, 0, 0, 0, 0], ifTextShow: true, ifScoreDialogShow: false, ifScoreShow: false, statusbarobj: { isshowbtn: false, //是否显示按钮 title: "小学语文朗读配音", //标题 }, }, onLoad: function (option) { console.log(option); this.videoCtx = null; const uid = wx.getStorageSync('uid') this.getClassInfo(option.id); // this.getMicAuth() }, getClassInfo: function (id) { httpRequestApi.getClassDetail(id).success(res => { console.log('课程信息', res) let reg = /\\n/g this.setData({ title: res.data.data.userRead.title, videoUrl: res.data.data.userRead.videoPath, originVideo: res.data.data.userRead.originVideo, img: res.data.data.userRead.iconImg, id: res.data.data.userRead.id, readingText: res.data.data.userRead.lessonText, grade: res.data.data.userRead.grade, exampleId: res.data.data.userRead.exampleId, summary: res.data.data.userRead.summary, coverImg: res.data.data.userRead.coverImg, }) this.getReadInfo(id) httpRequestApi.userIntoPage('pages/reading/reading', '朗读页面').success((res) => { }) }) }, onHide: function () { console.log('onhide') if (this.data.audioPlaying) { console.log('返回时状态', this.data.audioPlaying) // this.ss.stopPlay(); } if (this.data.btnImgFlag) { // this.ss.stopRecord(); } // this.ss.destroyEngine(); this.setData({ recordFlag: 0 }) }, onUnload: function () { console.log('onUnload') if (this.data.audioPlaying) { console.log('销毁时状态', this.data.audioPlaying) // this.ss.stopPlay(); } if (this.data.btnImgFlag) { // this.ss.stopRecord(); } // this.ss.destroyEngine(); }, onShow: function () { this.videoCtx = wx.createVideoContext('myVideo', this); let data = requirePlugin("myPlugin"); const obj = { appid: 'a415', userid: wx.getStorageSync('uid'), getEvalMessage: (res) => { this.getRecordScore(res) // console.log('评测结果',res) }, recorderCallback: (type, data) => { // 录音测评监控回调 this.ssRecorderCallback(type, data) } } // this.ss = new data.ssEngine(obj); this.recorderManager = wx.getRecorderManager(); this.recorderManager.onStop((res) => { // wx.showToast({ // title: 'onStop', // icon: 'fail', // duration: 500 // }) this.videoCtx.stop(); console.log('recorder stop', res) const recordFile = res.tempFilePath; this.setData({ recordFlag: 0, recordSource: recordFile, btnFlag: true, btnImgFlag: false }) this.wsEngine.stop({ success: () => { console.log('====== wsEngine stop success ======'); // TODO: add your code here }, fail: (res) => { console.log("====== wsEngine stop fail ======"); console.log(JSON.stringify(res)); //请关注res.errId, res.error // TODO: add your code here }, complete: () => { // TODO: add your code here // clearInterval(recordTimeInterval); console.log("====== wsEngine stop complete ======"); } }); }) this.recorderManager.onStart(() => { // this.saveVideo(); this.setData({ btnImgFlag: true, btnFlag: false }) console.log('recorder start') }); //监听已录制完指定帧大小的文件事件。如果设置了 frameSize,则会回调此事件。 this.recorderManager.onFrameRecorded((res) => { const { frameBuffer } = res console.log('frameBuffer.byteLength', frameBuffer.byteLength) //TODO 调用feed接口传递音频片给驰声评测引擎 this.wsEngine.feed({ data: frameBuffer, // frameBuffer为微信录音机回调的音频数据 success: () => { // feed 成功 console.log('feed success.') }, fail: (res) => { // feed 失败, 请关注res.errId, res.error console.log('feed fail:', JSON.stringify(res)) }, complete: () => { // feed 完成 } }); }); this.wsEngine = aiengine.createWsEngine({}); this.wsEngine.onResult(res => { console.log('驰声评测结果', res); let overall = res.result.overall; let accuracy = res.result.accuracy; wx.showModal({ title: `总分${overall}`, content: `accuracy${accuracy}`, confirmText: '我知道了', showCancel: false, success(res) { console.log('用户点击确定') } }) }) this.wsEngine.onErrorResult(res => { console.log('驰声createWsEngine失败', res); }) }, ssRecorderCallback: function (type, data) { console.log('录音测评监控回调', type) console.log('录音测评监控回调', data) /* 录音开始 */ if (type === 'onStart') { this.setData({ btnImgFlag: true, btnFlag: false }) console.log('recorder start') } if (type === 'onStop') { this.videoCtx.stop(); this.setData({ recordFlag: 0, recordSource: data.tempFilePath, btnFlag: true, btnImgFlag: false }) console.log('recorder start') } }, // 录音中视频播放结束 (控制录音同时结束) recordingVideoEnd: function () { console.log(this.data.videoCtr) console.log('recordingVideoEnd'); // if (this.data.recordFlag === 0) { // this.recordStop(); this.playingVideoEnd(); return; } // 录音结束 if (this.data.recordFlag === 1) { this.recordStop(); } }, // 播放中视频播放结束 (控制录音同时结束) playingVideoEnd: function () { console.log('playingVideoEnd') // this.ss.stopPlay(); }, /*** * recordFlag: * 0 初始状态 * 1 录音中 * 2 录音结束 ***/ audioRecord: function () { console.log('111111111111111recordFlag', this.data.recordFlag) if (this.data.recordFlag === 0) { // this.recordStart(); // this.saveVideo(); console.log('2222222222是否有视频', this.videoCtx) if (this.videoCtx) { console.log('3333333333333停止视频停止视频停止视频') this.videoCtx.stop(); // this.videoCtx.seek(0); } if (this.data.audioPlaying) { // this.ss.stopPlay(); } // if (this.recorderManager) { // this.recorderManager.stop(); // } // if (this.innerAudioContext) { // this.innerAudioContext.stop(); // } this.getMicAuth() // this.videoComplete(); return; } // 录音结束后 if (this.data.recordFlag === 1) { wx.showLoading({ title: '作品转码中', mask: true }) this.recordStop(); } }, // 录音开始 /** * duration: 时长 最长10分钟 sampleRate: 44100, 采样率 numberOfChannels: 1, 录音通道 encodeBitRate: 192000, 码率 format: 'mp3', 格式 frameSize: 50 制定帧大小 */ recordStart: function () { console.log('录音开始'); const options = { duration: 600000, sampleRate: 44100, numberOfChannels: 1, encodeBitRate: 192000, format: 'mp3', frameSize: 50 } this.recorderManager.start(options); // this.ss.startRecord({ // coreType: 'cn.sent.score', //测评题型 // evalTime: 300000, //测评的录音时间,时间到自动停止测评 // refText: this.data.readingText, //测评文本 // warrantId: 'c11163aa6c834a028da4a4b30955be96', //鉴权id // }) // 驰声测评 let timeStamp = new Date().getTime() let sig = sha1(`16075689600000da${timeStamp}caa8e60da6042731c230fe431ac9c7fd`) console.log('加密串', sig) // return let app = { applicationId: '16075689600000da', sig, //签名字符串 通过签名算法alg(appkey + timestamp + secretKey)生成 alg: 'sha1', timestamp: timeStamp + '', userId: wx.getStorageSync('uid') } console.log('app数据', app) let lessonText = this.data.readingText; console.log('课文课文课文',lessonText) this.wsEngine.start({ app, request: { coreType: "cn.pred.raw", refText: lessonText, rank: 100, attachAudioUrl: 1 }, audio: { audioType: "mp3", channel: 1, sampleBytes: 2, sampleRate: 16000 }, success: (res) => { // 调用成功 console.log('驰声start成功', res) }, fail: (res) => { // start失败,请关注res.errId, res.error console.log('驰声start失败', res) } }) }, getMicAuth() { let _this = this; wx.getSetting({ success(res) { if (res.authSetting['scope.record']) { // that.videoComplete(); _this.videoComplete(); } else { _this.getMicSetAuth(); return; } } }) }, getMicSetAuth() { let _this = this; wx.authorize({ scope: 'scope.record', success() { // 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问 _this.audioRecord(); }, fail() { wx.showModal({ title: '录音前请打开麦克风权限', content: '', confirmText: '我知道了', showCancel: false, success(res) { // console.log('用户点击确定') wx.openSetting({ success(res) { console.log('跳转到设置页', res.authSetting) // res.authSetting = { // "scope.userInfo": true, // "scope.userLocation": true // } } }) } }) } }) }, // 录音结束 recordStop: function () { this.setData({ muted: false }) // this.ss.stopRecord(); console.log('录音结束') this.recorderManager.stop(); wx.hideLoading() }, // 获取测评结果 getRecordScore(res) { console.log('测评结果', res) const result = res.result; const overall = result.overall; // 总分 const integrity = result.integrity; //完成度 const tone = result.tone; // 语调声调 const accuracy = result.accuracy; // 发音分 const fluency = result.fluency.overall; //流利度 let starArray = []; let score = overall / 20; if (score <= 0) { starArray = [0, 0, 0, 0, 0] } else { for (let i = 1; i < 5; i += 0.9) { if (i < score) { starArray.push(1); } else { starArray.push(0) } } } this.setData({ overall, integrity, tone, accuracy, fluency, ifScoreDialogShow: true, star: starArray }) }, closeScoreDialog() { this.setData({ ifScoreDialogShow: false, ifScoreShow: true, ifTextShow: false }) }, // 播放录音 audioPlay: function () { /* 用了先声智能以后录音播放由先声只能接管 */ this.setData({ videoUrl: this.data.videoUrl }) if (this.data.audioPath) { if (this.data.audioPlaying) { // this.ss.stopPlay(); } // this.ss.startPlay(this.data.audioPath); this.videoCtx.stop(); this.videoCtx.play(); } else { const recordSource = this.data.recordSource wx.uploadFile({ url: 'https://reader-api.ai160.com/file/upload', filePath: recordSource, name: '朗读录音', header: { uid: wx.getStorageSync('uid') }, success: (res) => { const formateRes = JSON.parse(res.data); let audioPath = formateRes.data; this.setData({ audioPath, audioPlaying: true }) // this.ss.startPlay(audioPath); // this.videoCtx.seek(0); this.videoCtx.stop(); console.log('播放器归0') this.videoCtx.play(); console.log('播放器播放') } }) } }, videoComplete: function () { // let videoUrl = 'http://efunimgs.ai160.com/ott/test/002tPr2Xlx07oP7B4ro40104120022hP0k010.mp4'; this.setData({ recordFlag: 1, videoUrl: this.data.originVideo, centerBtn: false, playBtn: false, isVideoListShow: false, muted: true, ifTextShow: true, ifScoreShow: false }, () => { this.videoCtx.play(); this.recordStart(); }) }, // 上传 upload: function () { if (this.videoCtx) { this.videoCtx.stop(); } if (this.data.audioPlaying) { // this.ss.stopPlay(); this.setData({ audioPlaying: false }) } wx.showLoading({ title: '作品上传中', mask: true }) if (this.data.audioPath) { this.shareWorks(this.data.audioPath) } else { const recordSource = this.data.recordSource; wx.uploadFile({ url: 'https://reader-api.ai160.com/file/upload', filePath: recordSource, name: '朗读录音', header: { uid: wx.getStorageSync('uid') }, success: (res) => { const formateRes = JSON.parse(res.data); let audioPath = formateRes.data; this.shareWorks(audioPath); } }) } }, shareWorks: function (audio) { const data = { "lessonId": this.data.id, "originVideo": this.data.videoUrl, "audioPath": audio, "title": this.data.title, "iconImg": this.data.img, "summary": this.data.summary, "productId": this.data.productId, "grade": this.data.grade, "exampleId": this.data.exampleId, "coverImg": this.data.coverImg }; httpRequestApi.postWork(data).success(res => { wx.hideLoading({ success: () => { wx.showToast({ title: '作品已上传正在审核中', icon: 'none', duration: 1000, success: () => { console.log('上传成功', res); const _data = this.data; const scoreData = { "userReadId": res.data.data.id, "complete": _data.integrity, "accuracy": _data.accuracy, "speed": _data.fluency, "intonation": _data.tone, "score": _data.overall } // 上传评分 httpRequestApi.postWorksScore(scoreData).success(res => { console.log(res) }); const pages = getCurrentPages(); const prevPage = pages[pages.length - 2]; prevPage.setData({ // workId: res.data.data.id, // 有id就塞到第一位 fromReading: true }, () => { wx.navigateBack({ delta: 1 }) }) } }) } }); }).fail(res => { wx.hideLoading({ success: () => { wx.showToast({ title: '上传超时', icon: 'fail', duration: 1000 }) } }); }) }, // 获取本课朗读内容 getReadInfo: function (id, pageNo, pageSize) { // const uid = wx.getStorageSync('uid'); const data = { exampleId: this.data.id || id, pageNo: this.data.pageNo, pageSize: 3, type: 'READ' }; httpRequestApi.getClassRead(data).success(res => { const readInfo = res.data.data.list; console.log(res) readInfo.forEach(item => { const temp = {}; temp.title = item.userRead ? item.userRead.title : ''; temp.img = item.userRead.iconImg; temp.plays = item.userRead.playAmount ? item.userRead.playAmount : 0; temp.likes = item.userRead.likeAmount ? item.userRead.likeAmount : 0; temp.classId = item.userRead.exampleId; temp.time = formatDate(item.userRead.gmtCreated, 3); temp.avatar = item.user ? item.user.avatar : ''; temp.uid = item.user ? item.user.uid : ''; temp.url = item.userRead.videoPath; // temp.avatar = item.user.avatar; temp.nickName = item.user ? item.user.wechatName : ''; temp.id = item.userRead.id; temp.noReading = true; temp.isFans = item.isFans ? true : item.user.uid === this.uid ? true : false; temp.coverImg = item.userRead.coverImg; temp.videoShow = false; temp.grade = item.userRead.grade; temp.type = item.userRead.type; // recommendWorks.push(temp); // that.data.hotData.hotWorks.push(temp); this.data.videoList.push(temp); }); this.setData({ videoList: this.data.videoList, totalSize: res.data.data.totalSize }) }); }, // 评论区点击 commentTap: function (e) { console.log('点击评论区', e) if (e.target.dataset.type === 'blank') { this.setData({ commentShow: false }) } }, scrollToLower: function (e) { console.log('滑动到底部', e) this.setData({ pageNo: this.data.pageNo + 1 }, () => { this.getReadInfo() }) }, scrollToUpper: function (e) { console.log('滑动到顶部', e) }, // 打开评论 openComment: function (e) { console.log('id', e.detail.activeId) this.setData({ commentShow: !this.data.commentShow, commentId: e.detail.activeId, }); }, goToReading: function (e) { this.setData({ pageNo: 1, videoList: [] }) const id = e.detail.activeId ? e.detail.activeId : e.currentTarget.dataset.id; this.getClassInfo(id) }, onShareAppMessage: function (res) { console.log('点击分享按钮', res) console.log('点击分享按钮', this.data.shareTitle) console.log('点击分享按钮', this.data.shareId) console.log('点击分享按钮', this.data.shareImg) if (res.from === 'button') { return { title: '请欣赏我的课文朗读作品,点赞+评论。', path: `/pages/index/index?readId=${this.data.shareId}`, imageUrl: '../../static/index/share_icon.png' } } else { return { title: '课文朗读,从未如此有趣。', path: '/pages/index/index', } } }, touchMove: function () { return }, openShare: function (e) { console.log('用户点击分享按钮', e) this.setData({ shareTitle: e.detail.currentTarget.dataset.title, shareId: e.detail.currentTarget.dataset.id, shareImg: e.detail.currentTarget.dataset.img }) }, onPlay: function (e) { // 下边视频列表onplay console.log('视频播放视频播放') if (this.videoCtx) { this.videoCtx.stop(); } else { this.videoCtx = wx.createVideoContext('myVideo', this); this.videoCtx.stop(); } }, collectTap: function (e) { console.log('点击收藏', e) const index = e.detail.index; let str = `videoList[${index}].isFavorite` this.setData({ [str]: e.detail.isCollect }) }, likeTap: function (e) { console.log('点赞', e) const index = e.detail.index; let likeStr = `videoList[${index}].isLike`; let likeNumStr = `videoList[${index}].likes`; this.setData({ [likeStr]: true, [likeNumStr]: this.data.videoList[index].likes + 1 }) }, })