reading.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. import httpRequestApi from '../../../utils/APIClient';
  2. import {
  3. formatDate
  4. } from '../../../utils/util';
  5. Page({
  6. data: {
  7. title: '',
  8. id: '',
  9. img: '',
  10. fullScreenBtn: false,
  11. playBtn: true,
  12. gesture: true,
  13. muted: false,
  14. gesture: false,
  15. centerBtn: true,
  16. recordFlag: 0,
  17. recordSource: '',
  18. videoCtr: 'recordingVideoEnd',
  19. btnFlag: false,
  20. btnImgFlag: false,
  21. microphonePng: '../../../static/image/microphone.png',
  22. recordingGif: '../../../static/image/readingNow.gif',
  23. videoUrl: '',
  24. readingText: '',
  25. videoList: [],
  26. pageNo: 1,
  27. nextMargin: getApp().globalData.nextMargin,
  28. lowerThresHold: 100,
  29. isVideoListShow: true,
  30. overall: '', // 评测总分
  31. integrity: '', //完成度
  32. tone: '', //语调声调
  33. fluency: '', //流利度
  34. accuracy: '', // 正确分,发音分
  35. star: [0, 0, 0, 0, 0],
  36. ifTextShow: true,
  37. ifScoreDialogShow: false,
  38. ifScoreShow: false
  39. },
  40. onLoad: function (option) {
  41. console.log(option);
  42. this.videoCtx = null;
  43. const uid = wx.getStorageSync('uid')
  44. httpRequestApi.getClassDetail(option.id).success(res => {
  45. console.log(res)
  46. let reg = /\\n/g
  47. this.setData({
  48. title: res.data.data.title,
  49. videoUrl: res.data.data.originVideo || res.data.data.playUrl,
  50. originVideo: res.data.data.originVideo,
  51. img: res.data.data.iconImg,
  52. id: res.data.data.id,
  53. readingText: res.data.data.lessonText,
  54. grade: res.data.data.grade,
  55. exampleId: res.data.data.exampleId,
  56. summary: res.data.data.summary
  57. // productId: res.data.data.product.id
  58. })
  59. console.log(this.data.readingText)
  60. console.log(this.data.img)
  61. this.getReadInfo(option.id)
  62. httpRequestApi.userIntoPage('pages/main/reading/reading', '朗读页面').success((res) => {
  63. })
  64. })
  65. },
  66. onHide: function () {
  67. console.log('onhide')
  68. if (this.data.btnImgFlag) {
  69. this.ss.stopRecord();
  70. }
  71. this.ss.destroyEngine();
  72. this.setData({
  73. recordFlag: 0
  74. })
  75. },
  76. onUnload: function () {
  77. console.log('onUnload')
  78. if (this.data.btnImgFlag) {
  79. this.ss.stopRecord();
  80. }
  81. this.ss.destroyEngine();
  82. },
  83. onShow: function () {
  84. this.setData({
  85. statusbarobj: {
  86. isshowbtn: false, //是否显示按钮
  87. title: "小学语文课文朗读", //标题
  88. },
  89. })
  90. this.videoCtx = wx.createVideoContext('myVideo', this);
  91. let data = requirePlugin("myPlugin");
  92. const obj = {
  93. appid: 'a415',
  94. userid: wx.getStorageSync('uid'),
  95. getEvalMessage: (res) => {
  96. this.getRecordScore(res)
  97. // console.log('评测结果',res)
  98. },
  99. recorderCallback: (type, data) => {
  100. // 录音测评监控回调
  101. this.ssRecorderCallback(type, data)
  102. }
  103. }
  104. this.ss = new data.ssEngine(obj);
  105. },
  106. ssRecorderCallback: function (type, data) {
  107. console.log('录音测评监控回调', type)
  108. console.log('录音测评监控回调', data)
  109. /* 录音开始 */
  110. if (type === 'onStart') {
  111. this.setData({
  112. btnImgFlag: true,
  113. btnFlag: false
  114. })
  115. console.log('recorder start')
  116. }
  117. if (type === 'onStop') {
  118. this.videoCtx.stop();
  119. this.setData({
  120. recordFlag: 0,
  121. recordSource: data.tempFilePath,
  122. btnFlag: true,
  123. btnImgFlag: false
  124. })
  125. console.log('recorder start')
  126. }
  127. },
  128. // 录音中视频播放结束 (控制录音同时结束)
  129. recordingVideoEnd: function () {
  130. console.log(this.data.videoCtr)
  131. console.log('recordingVideoEnd');
  132. //
  133. if (this.data.recordFlag === 0) {
  134. // this.recordStop();
  135. this.playingVideoEnd();
  136. return;
  137. }
  138. // 录音结束
  139. if (this.data.recordFlag === 1) {
  140. this.recordStop();
  141. }
  142. },
  143. // 播放中视频播放结束 (控制录音同时结束)
  144. playingVideoEnd: function () {
  145. console.log('playingVideoEnd')
  146. this.ss.stopPlay();;
  147. },
  148. /***
  149. * recordFlag:
  150. * 0 初始状态
  151. * 1 录音中
  152. * 2 录音结束
  153. ***/
  154. audioRecord: function () {
  155. console.log('recordFlag', this.data.recordFlag)
  156. if (this.data.recordFlag === 0) {
  157. // this.recordStart();
  158. // this.saveVideo();
  159. console.log('是否有视频', this.videoCtx)
  160. if (this.videoCtx) {
  161. this.videoCtx.stop();
  162. this.videoCtx.seek(0);
  163. }
  164. // if (this.recorderManager) {
  165. // this.recorderManager.stop();
  166. // }
  167. // if (this.innerAudioContext) {
  168. // this.innerAudioContext.stop();
  169. // }
  170. const that = this;
  171. wx.getSetting({
  172. success(res) {
  173. if (res.authSetting['scope.record']) {
  174. that.videoComplete();
  175. }
  176. // else {
  177. // wx.showToast({
  178. // title: 'onError',
  179. // icon: 'fail',
  180. // duration: 500
  181. // })
  182. // }
  183. }
  184. })
  185. return;
  186. }
  187. // 录音结束后
  188. if (this.data.recordFlag === 1) {
  189. wx.showLoading({
  190. title: '作品转码中',
  191. mask: true
  192. })
  193. this.recordStop();
  194. }
  195. },
  196. // 录音开始
  197. /**
  198. * duration: 时长 最长10分钟
  199. sampleRate: 44100, 采样率
  200. numberOfChannels: 1, 录音通道
  201. encodeBitRate: 192000, 码率
  202. format: 'mp3', 格式
  203. frameSize: 50 制定帧大小
  204. */
  205. recordStart: function () {
  206. console.log('录音开始');
  207. const options = {
  208. duration: 600000,
  209. sampleRate: 44100,
  210. numberOfChannels: 1,
  211. encodeBitRate: 192000,
  212. format: 'mp3',
  213. frameSize: 50
  214. }
  215. this.ss.startRecord({
  216. coreType: 'cn.sent.score', //测评题型
  217. evalTime: 240000, //测评的录音时间,时间到自动停止测评
  218. refText: this.data.readingText, //测评文本
  219. warrantId: 'c11163aa6c834a028da4a4b30955be96', //鉴权id
  220. })
  221. },
  222. // 录音结束
  223. recordStop: function () {
  224. this.ss.stopRecord();
  225. console.log('录音结束')
  226. wx.hideLoading()
  227. },
  228. // 获取测评结果
  229. getRecordScore(res) {
  230. console.log('测评结果', res)
  231. const result = res.result;
  232. const overall = result.overall; // 总分
  233. const integrity = result.integrity; //完成度
  234. const tone = result.tone; // 语调声调
  235. const accuracy = result.accuracy; // 发音分
  236. const fluency = result.fluency.overall; //流利度
  237. let starArray = [];
  238. let score = overall / 20;
  239. if (score <= 0) {
  240. starArray = [0, 0, 0, 0, 0]
  241. } else {
  242. for (let i = 1; i < 5; i += 0.9) {
  243. if (i < score) {
  244. starArray.push(1);
  245. } else {
  246. starArray.push(0)
  247. }
  248. }
  249. }
  250. this.setData({
  251. overall,
  252. integrity,
  253. tone,
  254. accuracy,
  255. fluency,
  256. ifScoreDialogShow: true,
  257. star: starArray
  258. })
  259. },
  260. closeScoreDialog() {
  261. this.setData({
  262. ifScoreDialogShow: false,
  263. ifScoreShow: true,
  264. ifTextShow: false
  265. })
  266. },
  267. // 播放录音
  268. audioPlay: function () {
  269. /* 用了先声智能以后录音播放由先声只能接管 */
  270. this.setData({
  271. videoUrl: this.data.videoUrl
  272. })
  273. this.videoCtx.seek(0)
  274. const recordSource = this.data.recordSource
  275. wx.uploadFile({
  276. url: 'https://reader.lingjiao.cn/readerBase/file/upload',
  277. filePath: recordSource,
  278. name: '朗读录音',
  279. header: {
  280. uid: wx.getStorageSync('uid')
  281. },
  282. success: (res) => {
  283. const formateRes = JSON.parse(res.data);
  284. let audioPath = formateRes.data;
  285. this.setData({
  286. audioPath,
  287. audioPlaying: true
  288. })
  289. this.ss.startPlay(audioPath);
  290. this.videoCtx.play();
  291. }
  292. })
  293. },
  294. videoComplete: function () {
  295. // let videoUrl = 'http://efunimgs.ai160.com/ott/test/002tPr2Xlx07oP7B4ro40104120022hP0k010.mp4';
  296. this.setData({
  297. recordFlag: 1,
  298. videoUrl: this.data.originVideo,
  299. centerBtn: false,
  300. playBtn: false,
  301. isVideoListShow: false
  302. }, () => {
  303. this.videoCtx.play();
  304. this.recordStart();
  305. })
  306. },
  307. // 上传
  308. upload: function () {
  309. if (this.videoCtx) {
  310. this.videoCtx.stop();
  311. }
  312. if (this.data.audioPlaying) {
  313. this.ss.stopPlay();
  314. this.setData({
  315. audioPlaying: false
  316. })
  317. }
  318. wx.showLoading({
  319. title: '作品分享中',
  320. mask: true
  321. })
  322. if (this.data.audioPath) {
  323. this.shareWorks(uid, this.data.audioPath)
  324. } else {
  325. const recordSource = this.data.recordSource;
  326. wx.uploadFile({
  327. url: 'https://reader.lingjiao.cn/readerBase/file/upload',
  328. filePath: recordSource,
  329. name: '朗读录音',
  330. header: {
  331. uid: wx.getStorageSync('uid')
  332. },
  333. success: (res) => {
  334. const formateRes = JSON.parse(res.data);
  335. let audioPath = formateRes.data;
  336. this.shareWorks(audioPath);
  337. }
  338. })
  339. }
  340. },
  341. shareWorks: function (audio) {
  342. const data = {
  343. "lessonId": this.data.id,
  344. "originVideo": this.data.videoUrl,
  345. "audioPath": audio,
  346. "title": this.data.title,
  347. "iconImg": this.data.img,
  348. "summary": this.data.summary,
  349. "productId": this.data.productId,
  350. "grade": this.data.grade,
  351. "exampleId": this.data.exampleId
  352. };
  353. httpRequestApi.postWork(data).success(res => {
  354. wx.hideLoading({
  355. success: () => {
  356. wx.showToast({
  357. title: '上传成功',
  358. icon: 'success',
  359. duration: 1000,
  360. success: () => {
  361. console.log(res);
  362. const _data = this.data;
  363. const scoreData = {
  364. "userReadId": res.data.data.id,
  365. "complete": _data.integrity,
  366. "accuracy": _data.accuracy,
  367. "speed": _data.fluency,
  368. "intonation": _data.tone,
  369. "score": _data.overall
  370. }
  371. httpRequestApi.postWorksScore(scoreData).success(res => {
  372. console.log(res)
  373. })
  374. wx.redirectTo({
  375. url: `../../social/works/works?id=${res.data.data.id}&flowerCount=${res.data.count}`
  376. })
  377. }
  378. })
  379. }
  380. });
  381. });
  382. },
  383. // 获取本课朗读内容
  384. getReadInfo: function (id, pageNo, pageSize) {
  385. // const uid = wx.getStorageSync('uid');
  386. const data = {
  387. exampleId: this.data.id || id,
  388. pageNo: this.data.pageNo,
  389. pageSize: 3,
  390. type: 'READ'
  391. };
  392. httpRequestApi.getClassRead(this.uid, data).success(res => {
  393. const readInfo = res.data.data.list;
  394. console.log(res)
  395. readInfo.forEach(item => {
  396. const temp = {};
  397. temp.title = item.userRead ? item.userRead.title : '';
  398. temp.img = item.userRead.iconImg;
  399. temp.plays = item.userRead.playAmount ? item.userRead.playAmount : 0;
  400. temp.likes = item.userRead.likeAmount ? item.userRead.likeAmount : 0;
  401. temp.classId = item.userRead.lessonId;
  402. temp.time = formatDate(item.userRead.gmtCreated, 3);
  403. temp.avatar = item.user ? item.user.avatar : '';
  404. temp.uid = item.user ? item.user.uid : '';
  405. temp.url = item.userRead.originVideo;
  406. // temp.avatar = item.user.avatar;
  407. temp.nickName = item.user ? item.user.wechatName : '';
  408. temp.id = item.userRead.id;
  409. // recommendWorks.push(temp);
  410. // that.data.hotData.hotWorks.push(temp);
  411. this.data.videoList.push(temp);
  412. });
  413. this.setData({
  414. videoList: this.data.videoList
  415. })
  416. });
  417. },
  418. // 评论区点击
  419. commentTap: function (e) {
  420. console.log('点击评论区', e)
  421. if (e.target.dataset.type === 'blank') {
  422. this.setData({
  423. commentShow: false
  424. })
  425. }
  426. },
  427. scrollToLower: function (e) {
  428. console.log('滑动到底部', e)
  429. this.setData({
  430. pageNo: this.data.pageNo + 1
  431. }, () => {
  432. this.getReadInfo()
  433. })
  434. },
  435. scrollToUpper: function (e) {
  436. console.log('滑动到顶部', e)
  437. },
  438. // 打开评论
  439. openComment: function (e) {
  440. console.log('id', e.detail.activeId)
  441. this.setData({
  442. commentShow: !this.data.commentShow,
  443. commentId: e.detail.activeId,
  444. });
  445. },
  446. })