index.js 9.1 KB

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