index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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(videoInfo.userReadExtend){
  138. if (false) {
  139. this.videoContext = wx.createVideoContext('myVideo')
  140. } else {
  141. this.innerAudioContext = wx.createInnerAudioContext();
  142. this.innerAudioContext.src = videoInfo.userRead.audioPath
  143. this.innerAudioContext.onEnded(res => {
  144. this.finishRecord()
  145. })
  146. this.innerAudioContext.onStop((res) => {
  147. this.finishRecord()
  148. });
  149. }
  150. },
  151. // 开始录制
  152. setCountDown() {
  153. if (this.data.state) {
  154. this.finishRecord()
  155. return
  156. }
  157. if (this.data.readingReset) {
  158. this.clearReset()
  159. this.getHeight()
  160. }
  161. this.setData({
  162. 'countDown.state': true
  163. })
  164. this.stl = setInterval(() => {
  165. if (this.data.countDown.num == 0) {
  166. clearInterval(this.stl)
  167. this.setData({
  168. state: true,
  169. countDown: {
  170. state: false,
  171. num: 3
  172. }
  173. })
  174. this.soundRecording()
  175. this.playMediaState()
  176. this.startRecording()
  177. } else {
  178. this.setData({
  179. 'countDown.num': --this.data.countDown.num
  180. })
  181. }
  182. }, 1000)
  183. },
  184. // 录音
  185. soundRecording() {
  186. /*调用微信开始录音接口,并启动语音评测*/
  187. let timeStamp = new Date().getTime()
  188. let sig = sha1(`16075689600000da${timeStamp}caa8e60da6042731c230fe431ac9c7fd`)
  189. let app = {
  190. applicationId: '16075689600000da',
  191. sig, //签名字符串
  192. alg: 'sha1',
  193. timestamp: timeStamp + '',
  194. userId: wx.getStorageSync('uid')
  195. }
  196. let lessonText = this.data.videoInfo.userRead.lessonText;
  197. wsEngine.start({
  198. request: {
  199. coreType: "cn.pred.raw",
  200. refText: lessonText,
  201. rank: 100,
  202. attachAudioUrl: 1,
  203. result: {
  204. details: {
  205. gop_adjust: 1
  206. }
  207. }
  208. },
  209. app,
  210. audio: {
  211. audioType: "mp3",
  212. channel: 1,
  213. sampleBytes: 2,
  214. sampleRate: 16000
  215. },
  216. success: (res) => {
  217. /*引擎启动成功,可以启动录音机开始录音,并将音频片传给引擎*/
  218. const options = {
  219. sampleRate: 44100, //采样率
  220. numberOfChannels: 1, //录音通道数
  221. encodeBitRate: 192000, //编码码率
  222. format: 'mp3', //音频格式,有效值aac/mp3
  223. frameSize: 50 //指定帧大小,单位 KB
  224. };
  225. //开始录音,在开始录音回调中feed音频片
  226. recorderManager.start(options);
  227. },
  228. fail: (res) => {
  229. console.log("fail============= " + res);
  230. },
  231. });
  232. //监听录音开始事件
  233. recorderManager.onStart(() => {});
  234. //监听录音结束事件
  235. recorderManager.onStop((res) => {
  236. console.log('录音结束', res);
  237. this.setData({
  238. tempFilePath: res.tempFilePath,
  239. });
  240. //录音机结束后,驰声引擎执行结束操作,等待评测返回结果
  241. wsEngine.stop({
  242. success: () => {
  243. console.log('====== wsEngine stop success ======');
  244. },
  245. fail: (res) => {
  246. console.log('录音结束报错', res);
  247. },
  248. });
  249. });
  250. //监听已录制完指定帧大小的文件事件。如果设置了 frameSize,则会回调此事件。
  251. recorderManager.onFrameRecorded((res) => {
  252. const {
  253. frameBuffer
  254. } = res
  255. //TODO 调用feed接口传递音频片给驰声评测引擎
  256. wsEngine.feed({
  257. data: frameBuffer, // frameBuffer为微信录音机回调的音频数据
  258. success: () => {
  259. console.log('feed success.监听已录制完指定帧大小的文件事件')
  260. },
  261. fail: (res) => {
  262. console.log('监听已录制完指定帧大小报错', res)
  263. },
  264. });
  265. });
  266. },
  267. // 结束录制
  268. finishRecord() {
  269. recorderManager.stop();
  270. this.stopMediaState()
  271. clearTimeout(this.setTimeoutObj)
  272. clearInterval(this.stl)
  273. this.setData({
  274. state: false,
  275. currentRow: null,
  276. scrollTop: 0
  277. })
  278. },
  279. // 测试的
  280. pkResult() {
  281. wx.redirectTo({
  282. url: `/pages/pkResult/index`,
  283. })
  284. },
  285. // 字体换行
  286. startRecording() {
  287. if (this.data.currentRow == null) {
  288. this.setData({
  289. currentRow: 0
  290. })
  291. }
  292. let row = this.data.article[this.data.currentRow]
  293. if (!row.readTime) {
  294. return
  295. }
  296. this.setTimeoutObj = setTimeout(() => {
  297. this.setData({
  298. currentRow: ++this.data.currentRow
  299. })
  300. this.setData({
  301. scrollTop: this.rowH * this.data.currentRow
  302. })
  303. this.startRecording()
  304. },
  305. row.readTime);
  306. },
  307. // 视频播放结束
  308. videoEnd() {
  309. this.finishRecord()
  310. },
  311. // 获取测评结果
  312. getRecordScore(res) {
  313. const result = res.result;
  314. const integrity = Math.floor(result.integrity); //完成度
  315. const tone = Math.floor(result.tone); // 语调声调
  316. const accuracy = Math.floor(result.overall); // 准确度 发音分
  317. const fluency = Math.floor(result.fluency.overall); //流利度
  318. let myOverall = Math.floor(integrity * 0.3 + accuracy * 0.5 + fluency * 0.1 + tone * 0.1);
  319. let detail = {
  320. integrity,
  321. tone,
  322. accuracy,
  323. fluency,
  324. myOverall,
  325. tempFilePath: this.data.tempFilePath,
  326. title: this.data.videoInfo.userRead.title,
  327. id: this.data.videoInfo.userRead.exampleId,
  328. }
  329. this.setReadDetail(detail)
  330. wx.redirectTo({
  331. url: this.data.readingType == 'public' ? '/pages/score/index' : '/pages/pkResult/index',
  332. })
  333. },
  334. videoPlay() {
  335. console.log('触发');
  336. if (this.data.readingReset) {
  337. console.log('触发2');
  338. this.resultAudioContext = wx.createInnerAudioContext();
  339. this.resultAudioContext.src = this.data.readDetail.tempFilePath; // 这里可以是录音的临时路径
  340. this.resultAudioContext.play();
  341. }
  342. },
  343. // 清除试听状态
  344. clearReset() {
  345. if (this.resultAudioContext) {
  346. this.resultAudioContext.stop()
  347. }
  348. this.setData({
  349. readingReset: false
  350. })
  351. },
  352. // 控制视频或音频的播放状态
  353. playMediaState() {
  354. // if(videoInfo.userReadExtend){
  355. if (false) {
  356. this.videoContext.play()
  357. } else {
  358. this.innerAudioContext.play();
  359. }
  360. },
  361. // 控制视频或音频的暂停状态
  362. stopMediaState() {
  363. // if(videoInfo.userReadExtend){
  364. if (false) {
  365. this.videoContext.stop()
  366. this.videoContext.seek(0)
  367. } else {
  368. this.innerAudioContext.stop()
  369. }
  370. },
  371. // 获取设备高度与行高度
  372. getHeight() {
  373. var query = wx.createSelectorQuery();
  374. query.select('.content').boundingClientRect((rect) => {
  375. this.setData({
  376. contentH: rect.height
  377. })
  378. }).exec()
  379. query.select('.row').boundingClientRect((rect) => {
  380. this.rowH = rect.height
  381. }).exec()
  382. },
  383. /**
  384. * 生命周期函数--监听页面卸载
  385. */
  386. onUnload() {
  387. wsEngine.reset()
  388. recorderManager.stop();
  389. if (this.innerAudioContext) {
  390. this.innerAudioContext.stop()
  391. }
  392. clearTimeout(this.setTimeoutObj)
  393. clearInterval(this.stl)
  394. },
  395. onShareAppMessage() {
  396. }
  397. })