123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820 |
- 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: "小学语文朗读配音", //标题
- },
- isScroll: true,
- noMoreWork: false
- },
- 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,
- shareImg: res.data.data.userRead.shareImg,
- })
- this.getReadInfo(id)
- httpRequestApi.userIntoPage('pages/reading/reading', '朗读页面').success((res) => {
- })
- })
- },
- onHide: function () {
- console.log('onhide')
- if (this.data.btnImgFlag) {
- this.recorderManager.stop();
- }
- if (this.innerAudioContext) {
- this.innerAudioContext.stop();
- }
- // this.ss.destroyEngine();
- this.setData({
- recordFlag: 0
- })
- },
- onUnload: function () {
- console.log('onUnload')
- if (this.data.btnImgFlag) {
- this.recorderManager.stop();
- }
- if (this.innerAudioContext) {
- this.innerAudioContext.stop();
- }
- },
- 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.seek(0);
- 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 ======');
- wx.showLoading({
- title: '作品评测中',
- mask: true
- })
- // 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 完成
- }
- });
- });
- },
- 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.recorderManager) {
- this.recorderManager.stop();
- }
- if (this.innerAudioContext) {
- this.innerAudioContext.stop();
- }
- if (this.data.recordFlag === 0) {
- this.wsEngine = aiengine.createWsEngine({});
- this.wsEngine.onResult(res => {
- wx.hideLoading();
- this.getRecordScore(res)
- })
- this.wsEngine.onErrorResult(res => {
- wx.hideLoading();
- console.log('驰声createWsEngine失败', res);
- })
- this.videoComplete();
- // this.getMicAuth()
- 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,
- result: {
- details: {
- gop_adjust: 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']) {
- _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('测评结果', JSON.stringify(res))
- const result = res.result;
- const overall = result.overall; // 总分
- const integrity = result.integrity; //完成度
- const tone = result.tone; // 语调声调
- // const accuracy = result.accuracy; // 准确度 发音分
- const accuracy = result.overall; // 准确度 发音分
- const fluency = result.fluency.overall; //流利度
- let starArray = [];
- let myOverall = integrity * 0.3 + accuracy * 0.5 + fluency * 0.1 + tone * 0.1;
- // let myOverall = 80;
- console.log('我的总分', myOverall)
- if (myOverall < 5) {
- starArray = [0, 0, 0, 0, 0]
- } else if (5 <= myOverall && myOverall < 30) {
- starArray = [1, 0, 0, 0, 0]
- } else if (30 <= myOverall && myOverall < 50) {
- starArray = [1, 1, 0, 0, 0]
- } else if (50 <= myOverall && myOverall < 70) {
- starArray = [1, 1, 1, 0, 0]
- } else if (70 <= myOverall && myOverall < 90) {
- starArray = [1, 1, 1, 1, 0]
- } else if (90 <= myOverall) {
- starArray = [1, 1, 1, 1, 1]
- }
- // let score = myOverall / 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 () {
- /* 用了先声智能以后录音播放由先声只能接管 */
- if (this.innerAudioContext) {
- this.innerAudioContext.stop();
- this.videoCtx.stop();
- }
- this.innerAudioContext = wx.createInnerAudioContext();
- this.innerAudioContext.src = this.data.recordSource; // 这里可以是录音的临时路径
- console.log('音频路径', this.data.recordSource)
- this.setData({
- videoUrl: this.data.videoUrl,
- muted: true
- })
- console.log(this.innerAudioContext.src);
- this.videoCtx.play();
- this.innerAudioContext.play();
- /* 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 () {
- this.setData({
- recordFlag: 1,
- videoUrl: this.data.originVideo,
- centerBtn: false,
- playBtn: false,
- isVideoListShow: false,
- muted: false,
- ifTextShow: true,
- ifScoreShow: false
- }, () => {
- console.log('播放视频播放视频播放视频')
- // this.videoCtx.seek(1);
- this.videoCtx.stop();
- 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
- })
- const recordSource = this.data.recordSource;
- wx.uploadFile({
- // url: 'https://reader-api.ai160.com/file/upload',
- url: 'https://reader-api.efunbox.cn//file/upload',
- filePath: recordSource,
- name: '朗读录音',
- header: {
- uid: wx.getStorageSync('uid')
- },
- success: (res) => {
- const formateRes = JSON.parse(res.data);
- let audioPath = formateRes.data;
- console.log('音频上传成功')
- 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,
- "shareImg": this.data.shareImg
- };
- httpRequestApi.postWork(data).success(res => {
- wx.hideLoading({
- success: () => {
- wx.showToast({
- title: '作品已上传正在审核中',
- icon: 'none',
- duration: 1000,
- success: () => {
- console.log('上传成功', res);
- if (res.data.count > 0) {
- this.setData({
- flowerNum: res.data.count
- })
- this.flowerAnimationHandler();
- }
- 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.shareAmount = item.userRead.shareAmount;
- temp.favoritesAmount = item.userRead.favoritesAmount;
- temp.commentAmount = item.userRead.commentAmount ? item.userRead.commentAmount : 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;
- temp.shareImg = item.userRead.shareImg;
- // 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,
- noMoreWork: readInfo.length <= 0 ? true : false
- })
- });
- },
- // 评论区点击
- commentTap: function (e) {
- console.log('点击评论区', e)
- if (e.target.dataset.type === 'blank') {
- if (this.data.commentShow && this.data.commentId) {
- httpRequestApi.getClassDetail(this.data.commentId).success(res => {
- console.log('评论回显', res.data.data.userRead.commentAmount)
- let str = `videoList[${this.data.commentIndex}].commentAmount`;
- this.setData({
- [str]:res.data.data.userRead.commentAmount
- })
- })
- }
- this.setData({
- commentShow: false
- })
- }
- },
- scrollToLower: function (e) {
- console.log('滑动到底部', e)
- this.setData({
- pageNo: this.data.pageNo + 1
- }, () => {
- this.getReadInfo()
- })
- },
- onReachBottom: 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,
- commentIndex: e.detail.activeIndex
- });
- },
- 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('onShareAppMessage', this.data.shareTitle)
- console.log('onShareAppMessage', this.data.shareId)
- console.log('onShareAppMessage', this.data.shareImg)
- if (res.from === 'button') {
- return {
- title: '请欣赏我的课文朗读作品,点赞+评论。',
- path: `/pages/index/index?readId=${this.data.shareId}`,
- imageUrl: this.data.shareImg
- }
- } 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.shareimg
- })
- },
- 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) {
- const index = e.detail.index;
- let str = `videoList[${index}].isFavorite`;
- let str2 = `videoList[${index}].favoritesAmount`;
- let favoritesAmount = e.detail.isCollect ? this.data.videoList[index].favoritesAmount + 1 : this.data.videoList[index].favoritesAmount - 1
- this.setData({
- [str]: e.detail.isCollect,
- [str2]: favoritesAmount
- })
- },
- 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
- })
- },
- flowerAnimationHandler: function () {
- this.flowerBox = this.selectComponent("#flower-toast");
- console.log('this.flower', this.flowerBox)
- this.flowerBox.comeOut();
- },
- // 获取焦点事件
- inputFocus(e) {
- this.setData({
- isScroll: false
- })
- this.triggerEvent('inputFocus');
- },
- // 失去焦点事件
- inputBlur(e) {
- this.setData({
- isScroll: true
- })
- this.triggerEvent('inputBlur');
- },
- addShareAmount: function (e) {
- console.log('+++++1', e)
- let str = `videoList[${e.detail.index}].shareAmount`;
- this.setData({
- [str]: this.data.videoList[e.detail.index].shareAmount + 1
- })
- }
- })
|