index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. import {
  2. getreadInfo
  3. } from '~/api/video'
  4. import {
  5. createStoreBindings
  6. } from 'mobx-miniprogram-bindings'
  7. import {
  8. store
  9. } from '~/store/index'
  10. const aiengine = require('~/utils/ChivoxAiEngine')
  11. const sha1 = require('~/utils/sha1');
  12. // 文章行高
  13. let rowH = 0
  14. let videoContext = null
  15. // 滚动变色定时器
  16. let stl = null
  17. // 倒计时
  18. let setTimeoutObj = null
  19. // 录音
  20. let innerAudioContext = null
  21. let resultAudioContext = null
  22. /*创建基础引擎*/
  23. let wsEngine = aiengine.createWsEngine({});
  24. /*微信录音*/
  25. let recorderManager = wx.getRecorderManager();
  26. Page({
  27. data: {
  28. videoInfo: {},
  29. currentRow: null,
  30. state: false,
  31. countDown: {
  32. state: false,
  33. num: 3,
  34. },
  35. contentH: 0,
  36. scrollTop: 0,
  37. //如果readingReset为true就是重读
  38. readingReset: false,
  39. //readingType为public是普通阅读,为pk是pk逻辑
  40. readingType: 'public',
  41. article: [{
  42. id: 1,
  43. text: '传说在很久很久以前,',
  44. time: '0'
  45. }, {
  46. id: 2,
  47. text: '天和地还没有分开,',
  48. time: '4010'
  49. }, {
  50. id: 3,
  51. text: '整个宇宙混沌一团,',
  52. time: '7080'
  53. }, {
  54. id: 4,
  55. text: '像个大鸡蛋。',
  56. time: '10150'
  57. }, {
  58. id: 5,
  59. text: '有个叫盘古的大神,',
  60. time: '13150'
  61. }, {
  62. id: 6,
  63. text: '昏睡了一万八千年。',
  64. time: '16190'
  65. }, {
  66. id: 7,
  67. text: '一天,大神醒来,睁眼一看,',
  68. time: '20030'
  69. }, {
  70. id: 8,
  71. text: '周围黑乎乎一片,',
  72. time: '24210'
  73. }, {
  74. id: 9,
  75. text: '什么也看不见。',
  76. time: '27300'
  77. }]
  78. },
  79. onLoad(options) {
  80. let videoId = options.videoId
  81. this.getreadInfo(videoId)
  82. let data = this.data.article
  83. data = data.map((item, index) => {
  84. item.readTime = data[index + 1] ? data[index + 1].time - item.time : ''
  85. return item
  86. })
  87. this.setData({
  88. article: data,
  89. readingReset: options.reset || false,
  90. readingType: options.readingType || 'public'
  91. })
  92. // 手工绑定
  93. this.storeBindings = createStoreBindings(this, {
  94. store,
  95. fields: {
  96. userInfo: 'userInfo',
  97. readDetail: 'readDetail'
  98. },
  99. actions: {
  100. setReadDetail: 'setReadDetail'
  101. }
  102. })
  103. if (!options.reset) {
  104. this.getHeight()
  105. }
  106. // 录音授权
  107. wx.getSetting({
  108. success(res) {
  109. if (!res.authSetting['scope.record']) {
  110. wx.authorize({
  111. scope: 'scope.record',
  112. success() {
  113. // 用户已经同意小程序使用录音功能,后续调用接口不会弹窗询问
  114. wx.getRecorderManager()
  115. }
  116. })
  117. }
  118. }
  119. })
  120. /*监听评测结果:必须在基础引擎创建后,调用任何评测接口前设置监听,否则有可能收不到相关事件。*/
  121. wsEngine.onResult((res) => {
  122. this.getRecordScore(res)
  123. });
  124. wsEngine.onErrorResult((res) => {
  125. console.log("===收到错误结果=============", res)
  126. });
  127. },
  128. // 获取阅读内容
  129. async getreadInfo(videoId) {
  130. let videoInfo = await getreadInfo(videoId)
  131. wx.setNavigationBarTitle({
  132. title: videoInfo.userRead.title
  133. })
  134. this.setData({
  135. videoInfo
  136. })
  137. if (!this.data.videoInfo.userReadExtend) {
  138. this.videoContext = wx.createVideoContext('myVideo')
  139. } else {
  140. this.innerAudioContext = wx.createInnerAudioContext();
  141. this.innerAudioContext.src = videoInfo.userRead.audioPath
  142. this.innerAudioContext.onEnded(res => {
  143. this.finishRecord()
  144. })
  145. this.innerAudioContext.onStop((res) => {
  146. this.finishRecord()
  147. });
  148. }
  149. },
  150. // 开始录制
  151. setCountDown() {
  152. if (this.data.state) {
  153. this.finishRecord()
  154. return
  155. }
  156. if (this.data.readingReset) {
  157. this.clearReset()
  158. this.getHeight()
  159. }
  160. this.setData({
  161. 'countDown.state': true
  162. })
  163. this.stl = setInterval(() => {
  164. if (this.data.countDown.num == 0) {
  165. clearInterval(this.stl)
  166. this.setData({
  167. state: true,
  168. countDown: {
  169. state: false,
  170. num: 3
  171. }
  172. })
  173. this.soundRecording()
  174. this.playMediaState()
  175. this.startRecording()
  176. } else {
  177. this.setData({
  178. 'countDown.num': --this.data.countDown.num
  179. })
  180. }
  181. }, 1000)
  182. },
  183. // 录音
  184. soundRecording() {
  185. /*调用微信开始录音接口,并启动语音评测*/
  186. let timeStamp = new Date().getTime()
  187. let sig = sha1(`16075689600000da${timeStamp}caa8e60da6042731c230fe431ac9c7fd`)
  188. let app = {
  189. applicationId: '16075689600000da',
  190. sig, //签名字符串
  191. alg: 'sha1',
  192. timestamp: timeStamp + '',
  193. userId: wx.getStorageSync('uid')
  194. }
  195. let lessonText = this.data.videoInfo.userRead.lessonText;
  196. wsEngine.start({
  197. request: {
  198. coreType: "cn.pred.raw",
  199. refText: lessonText,
  200. rank: 100,
  201. attachAudioUrl: 1,
  202. result: {
  203. details: {
  204. gop_adjust: 1
  205. }
  206. }
  207. },
  208. app,
  209. audio: {
  210. audioType: "mp3",
  211. channel: 1,
  212. sampleBytes: 2,
  213. sampleRate: 16000
  214. },
  215. success: (res) => {
  216. /*引擎启动成功,可以启动录音机开始录音,并将音频片传给引擎*/
  217. const options = {
  218. sampleRate: 44100, //采样率
  219. numberOfChannels: 1, //录音通道数
  220. encodeBitRate: 192000, //编码码率
  221. format: 'mp3', //音频格式,有效值aac/mp3
  222. frameSize: 50 //指定帧大小,单位 KB
  223. };
  224. //开始录音,在开始录音回调中feed音频片
  225. recorderManager.start(options);
  226. },
  227. fail: (res) => {
  228. console.log("fail============= " + res);
  229. },
  230. });
  231. //监听录音开始事件
  232. recorderManager.onStart(() => {});
  233. //监听录音结束事件
  234. recorderManager.onStop((res) => {
  235. console.log('录音结束', res);
  236. this.setData({
  237. tempFilePath: res.tempFilePath,
  238. });
  239. //录音机结束后,驰声引擎执行结束操作,等待评测返回结果
  240. wsEngine.stop({
  241. success: () => {
  242. console.log('====== wsEngine stop success ======');
  243. },
  244. fail: (res) => {
  245. console.log('录音结束报错', res);
  246. },
  247. });
  248. });
  249. //监听已录制完指定帧大小的文件事件。如果设置了 frameSize,则会回调此事件。
  250. recorderManager.onFrameRecorded((res) => {
  251. const {
  252. frameBuffer
  253. } = res
  254. //TODO 调用feed接口传递音频片给驰声评测引擎
  255. wsEngine.feed({
  256. data: frameBuffer, // frameBuffer为微信录音机回调的音频数据
  257. success: () => {
  258. console.log('feed success.监听已录制完指定帧大小的文件事件')
  259. },
  260. fail: (res) => {
  261. console.log('监听已录制完指定帧大小报错', res)
  262. },
  263. });
  264. });
  265. },
  266. // 结束录制
  267. finishRecord() {
  268. recorderManager.stop();
  269. this.stopMediaState()
  270. clearTimeout(this.setTimeoutObj)
  271. clearInterval(this.stl)
  272. this.setData({
  273. state: false,
  274. currentRow: null,
  275. scrollTop: 0
  276. })
  277. },
  278. // 测试的
  279. pkResult() {
  280. wx.redirectTo({
  281. url: `/pages/pkResult/index`,
  282. })
  283. },
  284. // 字体换行
  285. startRecording() {
  286. if (this.data.currentRow == null) {
  287. this.setData({
  288. currentRow: 0
  289. })
  290. }
  291. let row = this.data.article[this.data.currentRow]
  292. if (!row.readTime) {
  293. return
  294. }
  295. this.setTimeoutObj = setTimeout(() => {
  296. this.setData({
  297. currentRow: ++this.data.currentRow
  298. })
  299. this.setData({
  300. scrollTop: this.rowH * this.data.currentRow
  301. })
  302. this.startRecording()
  303. },
  304. row.readTime);
  305. },
  306. // 视频播放结束
  307. videoEnd() {
  308. this.finishRecord()
  309. },
  310. // 获取测评结果
  311. getRecordScore(res) {
  312. const result = res.result;
  313. const integrity = Math.floor(result.integrity); //完成度
  314. const tone = Math.floor(result.tone); // 语调声调
  315. const accuracy = Math.floor(result.overall); // 准确度 发音分
  316. const fluency = Math.floor(result.fluency.overall); //流利度
  317. let myOverall = Math.floor(integrity * 0.3 + accuracy * 0.5 + fluency * 0.1 + tone * 0.1);
  318. let detail = {
  319. integrity,
  320. tone,
  321. accuracy,
  322. fluency,
  323. myOverall,
  324. tempFilePath: this.data.tempFilePath,
  325. title: this.data.videoInfo.userRead.title,
  326. id: this.data.videoInfo.userRead.exampleId,
  327. }
  328. this.setReadDetail(detail)
  329. wx.redirectTo({
  330. url: this.data.readingType == 'public' ? '/pages/score/index' : '/pages/pkResult/index',
  331. })
  332. },
  333. videoPlay() {
  334. if (this.data.readingReset) {
  335. this.resultAudioContext = wx.createInnerAudioContext();
  336. this.resultAudioContext.src = this.data.readDetail.tempFilePath; // 这里可以是录音的临时路径
  337. this.resultAudioContext.play();
  338. }
  339. },
  340. // 清除试听状态
  341. clearReset() {
  342. if (this.resultAudioContext) {
  343. this.resultAudioContext.stop()
  344. }
  345. this.setData({
  346. readingReset: false
  347. })
  348. },
  349. // 控制视频或音频的播放状态
  350. playMediaState() {
  351. if (!this.data.videoInfo.userReadExtend) {
  352. this.videoContext.play()
  353. } else {
  354. this.innerAudioContext.play();
  355. }
  356. },
  357. // 控制视频或音频的暂停状态
  358. stopMediaState() {
  359. if (!this.data.videoInfo.userReadExtend) {
  360. this.videoContext.stop()
  361. this.videoContext.seek(0)
  362. } else {
  363. this.innerAudioContext.stop()
  364. }
  365. },
  366. // 获取设备高度与行高度
  367. getHeight() {
  368. var query = wx.createSelectorQuery();
  369. query.select('.content').boundingClientRect((rect) => {
  370. this.setData({
  371. contentH: rect.height
  372. })
  373. }).exec()
  374. query.select('.row').boundingClientRect((rect) => {
  375. this.rowH = rect.height
  376. }).exec()
  377. },
  378. /**
  379. * 生命周期函数--监听页面卸载
  380. */
  381. onUnload() {
  382. wsEngine.reset()
  383. recorderManager.stop();
  384. if (this.innerAudioContext) {
  385. this.innerAudioContext.stop()
  386. }
  387. clearTimeout(this.setTimeoutObj)
  388. clearInterval(this.stl)
  389. },
  390. })