123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- import {
- getreadInfo
- } from '~/api/video'
- import {
- createStoreBindings
- } from 'mobx-miniprogram-bindings'
- import {
- store
- } from '~/store/index'
- const aiengine = require('~/utils/ChivoxAiEngine')
- const sha1 = require('~/utils/sha1');
- let rowH = 0
- let videoContext = null
- let stl = null
- let setTimeoutObj = null
- let innerAudioContext = null
- let wsEngine = aiengine.createWsEngine({});
- let recorderManager = wx.getRecorderManager();
- Page({
- data: {
- videoInfo: {},
- currentRow: null,
- state: false,
- countDown: {
- state: false,
- num: 3,
- },
- contentH: 0,
- scrollTop: 0,
-
- readingReset: false,
-
- readingType: 'public',
- article: [{
- id: 1,
- text: '传说在很久很久以前,',
- time: '0'
- }, {
- id: 2,
- text: '天和地还没有分开,',
- time: '4010'
- }, {
- id: 3,
- text: '整个宇宙混沌一团,',
- time: '7080'
- }, {
- id: 4,
- text: '像个大鸡蛋。',
- time: '10150'
- }, {
- id: 5,
- text: '有个叫盘古的大神,',
- time: '13150'
- }, {
- id: 6,
- text: '昏睡了一万八千年。',
- time: '16190'
- }, {
- id: 7,
- text: '一天,大神醒来,睁眼一看,',
- time: '20030'
- }, {
- id: 8,
- text: '周围黑乎乎一片,',
- time: '24210'
- }, {
- id: 9,
- text: '什么也看不见。',
- time: '27300'
- }]
- },
- onLoad(options) {
- let videoId = options.videoId
- this.getreadInfo(videoId)
- let data = this.data.article
- data = data.map((item, index) => {
- item.readTime = data[index + 1] ? data[index + 1].time - item.time : ''
- return item
- })
- this.setData({
- article: data,
- readingReset: options.reset || false,
- readingType: options.readingType || 'public'
- })
-
- this.storeBindings = createStoreBindings(this, {
- store,
- fields: {
- userInfo: 'userInfo',
- readDetail: 'readDetail'
- },
- actions: {
- setReadDetail: 'setReadDetail'
- }
- })
- if (!options.reset) {
- this.getHeight()
- }
- this.videoContext = wx.createVideoContext('myVideo')
-
- wx.getSetting({
- success(res) {
- if (!res.authSetting['scope.record']) {
- wx.authorize({
- scope: 'scope.record',
- success() {
-
- wx.getRecorderManager()
- }
- })
- }
- }
- })
-
- wsEngine.onResult((res) => {
- this.getRecordScore(res)
- });
- wsEngine.onErrorResult((res) => {
- console.log("===收到错误结果=============", res)
- });
- },
-
- async getreadInfo(videoId) {
- let videoInfo = await getreadInfo(videoId)
- wx.setNavigationBarTitle({
- title: videoInfo.userRead.title
- })
- this.setData({
- videoInfo
- })
- },
-
- setCountDown() {
- if (this.data.state) {
- this.finishRecord()
- return
- }
- if (this.data.readingReset) {
- this.clearReset()
- this.getHeight()
- }
- this.setData({
- 'countDown.state': true
- })
- this.stl = setInterval(() => {
- if (this.data.countDown.num == 0) {
- clearInterval(this.stl)
- this.setData({
- state: true,
- countDown: {
- state: false,
- num: 3
- }
- })
- this.soundRecording()
- this.videoContext.play()
- this.startRecording()
- } else {
- this.setData({
- 'countDown.num': --this.data.countDown.num
- })
- }
- }, 1000)
- },
-
- soundRecording() {
-
- let timeStamp = new Date().getTime()
- let sig = sha1(`16075689600000da${timeStamp}caa8e60da6042731c230fe431ac9c7fd`)
- let app = {
- applicationId: '16075689600000da',
- sig,
- alg: 'sha1',
- timestamp: timeStamp + '',
- userId: wx.getStorageSync('uid')
- }
- let lessonText = this.data.videoInfo.userRead.lessonText;
- wsEngine.start({
- request: {
- coreType: "cn.pred.raw",
- refText: lessonText,
- rank: 100,
- attachAudioUrl: 1,
- result: {
- details: {
- gop_adjust: 1
- }
- }
- },
- app,
- audio: {
- audioType: "mp3",
- channel: 1,
- sampleBytes: 2,
- sampleRate: 16000
- },
- success: (res) => {
-
- const options = {
- sampleRate: 44100,
- numberOfChannels: 1,
- encodeBitRate: 192000,
- format: 'mp3',
- frameSize: 50
- };
-
- recorderManager.start(options);
- },
- fail: (res) => {
- console.log("fail============= " + res);
- },
- });
-
- recorderManager.onStart(() => {});
-
- recorderManager.onStop((res) => {
- console.log('录音结束', res);
- this.setData({
- tempFilePath: res.tempFilePath,
- });
-
- wsEngine.stop({
- success: () => {
- console.log('====== wsEngine stop success ======');
- },
- fail: (res) => {
- console.log('录音结束报错', res);
- },
- });
- });
-
- recorderManager.onFrameRecorded((res) => {
- const {
- frameBuffer
- } = res
-
- wsEngine.feed({
- data: frameBuffer,
- success: () => {
- console.log('feed success.监听已录制完指定帧大小的文件事件')
- },
- fail: (res) => {
- console.log('监听已录制完指定帧大小报错', res)
- },
- });
- });
- },
-
- finishRecord() {
- recorderManager.stop();
- this.videoContext.stop()
- this.videoContext.seek(0)
- clearTimeout(this.setTimeoutObj)
- clearInterval(this.stl)
- this.setData({
- state: false,
- currentRow: null,
- scrollTop: 0
- })
- },
-
- pkResult() {
- wx.redirectTo({
- url: `/pages/pkResult/index`,
- })
- },
-
- startRecording() {
- if (this.data.currentRow == null) {
- this.setData({
- currentRow: 0
- })
- }
- let row = this.data.article[this.data.currentRow]
- if (!row.readTime) {
- return
- }
- this.setTimeoutObj = setTimeout(() => {
- this.setData({
- currentRow: ++this.data.currentRow
- })
- this.setData({
- scrollTop: this.rowH * this.data.currentRow
- })
- this.startRecording()
- },
- row.readTime);
- },
-
- videoEnd() {
- this.finishRecord()
- },
-
- getRecordScore(res) {
- const result = res.result;
- const integrity = Math.floor(result.integrity);
- const tone = Math.floor(result.tone);
- const accuracy = Math.floor(result.overall);
- const fluency = Math.floor(result.fluency.overall);
- let myOverall = Math.floor(integrity * 0.3 + accuracy * 0.5 + fluency * 0.1 + tone * 0.1);
- let detail = {
- integrity,
- tone,
- accuracy,
- fluency,
- myOverall,
- tempFilePath: this.data.tempFilePath,
- title: this.data.videoInfo.userRead.title,
- id: this.data.videoInfo.userRead.exampleId,
- }
- this.setReadDetail(detail)
- wx.redirectTo({
- url: this.data.readingType == 'public' ? '/pages/score/index' : '/pages/pkResult/index',
- })
- },
- videoPlay() {
- if (this.data.readingReset) {
- this.innerAudioContext = wx.createInnerAudioContext();
- this.innerAudioContext.src = this.data.readDetail.tempFilePath;
- this.innerAudioContext.play();
- }
- },
-
- clearReset() {
- if (this.innerAudioContext) {
- this.innerAudioContext.stop()
- }
- this.videoContext.stop()
- this.videoContext.seek(0)
- this.setData({
- readingReset: false
- })
- },
-
- getHeight() {
- var query = wx.createSelectorQuery();
- query.select('.content').boundingClientRect((rect) => {
- this.setData({
- contentH: rect.height
- })
- }).exec()
- query.select('.row').boundingClientRect((rect) => {
- this.rowH = rect.height
- }).exec()
- },
-
- onUnload() {
- wsEngine.reset()
- recorderManager.stop();
- if (this.innerAudioContext) {
- this.innerAudioContext.stop()
- }
- clearTimeout(this.setTimeoutObj)
- clearInterval(this.stl)
- },
- onShareAppMessage() {
- }
- })
|