index.js 9.4 KB

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