reading.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. },
  31. onLoad: function (option) {
  32. console.log(option);
  33. this.videoCtx = null;
  34. const uid = wx.getStorageSync('uid')
  35. httpRequestApi.getClassDetail(option.id).success(res => {
  36. console.log(res)
  37. let reg = /\\n/g
  38. this.setData({
  39. title: res.data.data.title,
  40. videoUrl: res.data.data.originVideo || res.data.data.playUrl,
  41. originVideo: res.data.data.originVideo,
  42. img: res.data.data.iconImg,
  43. id: res.data.data.id,
  44. readingText: res.data.data.lessonText,
  45. grade: res.data.data.grade,
  46. exampleId: res.data.data.exampleId,
  47. summary: res.data.data.summary
  48. // productId: res.data.data.product.id
  49. })
  50. console.log(this.data.readingText)
  51. console.log(this.data.img)
  52. this.getReadInfo(option.id)
  53. httpRequestApi.userIntoPage('pages/main/reading/reading', '朗读页面').success((res) => {
  54. })
  55. })
  56. this.recorderManager = wx.getRecorderManager();
  57. // 监听录音部分
  58. // 录音开始
  59. this.recorderManager.onStart(() => {
  60. // this.saveVideo();
  61. this.setData({
  62. btnImgFlag: true,
  63. btnFlag: false
  64. })
  65. console.log('recorder start')
  66. })
  67. // this.recorderManager.onPause(() => {
  68. // wx.showToast({
  69. // title: '录音被暂停',
  70. // icon: 'fail',
  71. // duration: 500
  72. // })
  73. // })
  74. // this.recorderManager.onInterruptionBegin(() => {
  75. // wx.showToast({
  76. // title: '录音被应用中断开始',
  77. // icon: 'fail',
  78. // duration: 500
  79. // })
  80. // })
  81. // this.recorderManager.onInterruptionEnd(() => {
  82. // wx.showToast({
  83. // title: '录音中断结束',
  84. // icon: 'fail',
  85. // duration: 500
  86. // })
  87. // })
  88. let dialogFlag = true;
  89. this.recorderManager.onError(() => {
  90. wx.getSetting({
  91. success(res) {
  92. if (!res.authSetting['scope.record'] && dialogFlag) {
  93. wx.showModal({
  94. title: '录音前请打开麦克风权限',
  95. content: '点击右上角浮点...,选择:设置-麦克风,点击/打开。',
  96. confirmText: '我知道了',
  97. showCancel: false,
  98. success(res) {
  99. console.log('用户点击确定')
  100. // dialogFlag = false;
  101. }
  102. })
  103. dialogFlag = false;
  104. setTimeout(() => {
  105. dialogFlag = true;
  106. }, 1000)
  107. }
  108. // else {
  109. // wx.showToast({
  110. // title: 'onError',
  111. // icon: 'fail',
  112. // duration: 500
  113. // })
  114. // }
  115. }
  116. })
  117. })
  118. // 录音结束
  119. this.recorderManager.onStop((res) => {
  120. // wx.showToast({
  121. // title: 'onStop',
  122. // icon: 'fail',
  123. // duration: 500
  124. // })
  125. this.videoCtx.stop();
  126. console.log('recorder stop', res)
  127. const recordFile = res.tempFilePath;
  128. this.setData({
  129. recordFlag: 0,
  130. recordSource: recordFile,
  131. btnFlag: true,
  132. btnImgFlag: false
  133. })
  134. })
  135. },
  136. onHide: function () {
  137. console.log('onhide')
  138. if (this.data.btnImgFlag) {
  139. this.recorderManager.stop();
  140. }
  141. if (this.innerAudioContext) {
  142. this.innerAudioContext.stop();
  143. }
  144. this.setData({
  145. recordFlag: 0
  146. })
  147. },
  148. onUnload: function () {
  149. console.log('onUnload')
  150. if (this.recorderManager) {
  151. this.recorderManager.stop();
  152. }
  153. if (this.innerAudioContext) {
  154. this.innerAudioContext.stop();
  155. }
  156. },
  157. onShow: function () {
  158. this.setData({
  159. statusbarobj: {
  160. isshowbtn: false, //是否显示按钮
  161. title: "小学语文课文朗读", //标题
  162. },
  163. })
  164. this.videoCtx = wx.createVideoContext('myVideo', this);
  165. },
  166. // 视频缓冲
  167. // videoWaiting: function () {
  168. // this.recorderManager.pause();
  169. // },
  170. // //视频播放
  171. // videoPlay: function () {
  172. // this.recorderManager.resume();
  173. // },
  174. // 录音中视频播放结束 (控制录音同时结束)
  175. recordingVideoEnd: function () {
  176. console.log(this.data.videoCtr)
  177. console.log('recordingVideoEnd');
  178. //
  179. if (this.data.recordFlag === 0) {
  180. // this.recordStop();
  181. this.playingVideoEnd();
  182. return;
  183. }
  184. // 录音结束
  185. if (this.data.recordFlag === 1) {
  186. this.recordStop();
  187. }
  188. },
  189. // 播放中视频播放结束 (控制录音同时结束)
  190. playingVideoEnd: function () {
  191. console.log('playingVideoEnd')
  192. this.innerAudioContext.stop();
  193. },
  194. /***
  195. * recordFlag:
  196. * 0 初始状态
  197. * 1 录音中
  198. * 2 录音结束
  199. ***/
  200. audioRecord: function () {
  201. console.log('recordFlag', this.data.recordFlag)
  202. if (this.data.recordFlag === 0) {
  203. // this.recordStart();
  204. // this.saveVideo();
  205. console.log('是否有视频', this.videoCtx)
  206. if (this.videoCtx) {
  207. this.videoCtx.stop();
  208. this.videoCtx.seek(0);
  209. }
  210. if (this.recorderManager) {
  211. this.recorderManager.stop();
  212. }
  213. if (this.innerAudioContext) {
  214. this.innerAudioContext.stop();
  215. }
  216. const that = this;
  217. wx.getSetting({
  218. success(res) {
  219. if (res.authSetting['scope.record']) {
  220. that.videoComplete();
  221. }
  222. // else {
  223. // wx.showToast({
  224. // title: 'onError',
  225. // icon: 'fail',
  226. // duration: 500
  227. // })
  228. // }
  229. }
  230. })
  231. return;
  232. }
  233. // 录音结束后
  234. if (this.data.recordFlag === 1) {
  235. wx.showLoading({
  236. title: '作品转码中',
  237. mask: true
  238. })
  239. this.recordStop();
  240. }
  241. },
  242. // 录音开始
  243. /**
  244. * duration: 时长 最长10分钟
  245. sampleRate: 44100, 采样率
  246. numberOfChannels: 1, 录音通道
  247. encodeBitRate: 192000, 码率
  248. format: 'mp3', 格式
  249. frameSize: 50 制定帧大小
  250. */
  251. recordStart: function () {
  252. console.log('录音开始');
  253. const options = {
  254. duration: 600000,
  255. sampleRate: 44100,
  256. numberOfChannels: 1,
  257. encodeBitRate: 192000,
  258. format: 'mp3',
  259. frameSize: 50
  260. }
  261. this.recorderManager.start(options);
  262. },
  263. // 录音结束
  264. recordStop: function () {
  265. console.log('录音结束')
  266. this.recorderManager.stop();
  267. wx.hideLoading()
  268. },
  269. // 播放录音
  270. audioPlay: function () {
  271. this.setData({
  272. videoUrl: this.data.videoUrl
  273. })
  274. console.log('音频播放');
  275. if (this.innerAudioContext) {
  276. this.innerAudioContext.stop();
  277. this.videoCtx.stop();
  278. }
  279. this.videoCtx.seek(0)
  280. this.innerAudioContext = wx.createInnerAudioContext();
  281. this.innerAudioContext.src = this.data.recordSource; // 这里可以是录音的临时路径
  282. console.log(this.innerAudioContext.src);
  283. this.videoCtx.play();
  284. this.innerAudioContext.play();
  285. },
  286. videoComplete: function () {
  287. // let videoUrl = 'http://efunimgs.ai160.com/ott/test/002tPr2Xlx07oP7B4ro40104120022hP0k010.mp4';
  288. this.setData({
  289. recordFlag: 1,
  290. videoUrl: this.data.originVideo,
  291. centerBtn: false,
  292. playBtn: false,
  293. isVideoListShow: false
  294. }, () => {
  295. this.videoCtx.play();
  296. this.recordStart();
  297. })
  298. },
  299. // 上传
  300. upload: function () {
  301. if (this.videoCtx) {
  302. this.videoCtx.stop();
  303. }
  304. if (this.recorderManager) {
  305. this.recorderManager.stop();
  306. }
  307. if (this.innerAudioContext) {
  308. this.innerAudioContext.stop();
  309. }
  310. wx.showLoading({
  311. title: '作品分享中',
  312. mask: true
  313. })
  314. const recordSource = this.data.recordSource;
  315. wx.uploadFile({
  316. url: 'https://reader.lingjiao.cn/readerBase/file/upload',
  317. filePath: recordSource,
  318. name: '朗读录音',
  319. header: {
  320. uid: wx.getStorageSync('uid')
  321. },
  322. success: (res) => {
  323. const formateRes = JSON.parse(res.data);
  324. let audioPath = formateRes.data;
  325. let uid = wx.getStorageSync('uid');
  326. shareWorks(uid, audioPath);
  327. }
  328. })
  329. let shareWorks = (uid, audio) => {
  330. if (this.innerAudioContext) {
  331. this.innerAudioContext.stop();
  332. }
  333. const data = {
  334. "lessonId": this.data.id,
  335. "originVideo": this.data.videoUrl,
  336. "audioPath": audio,
  337. "title": this.data.title,
  338. "iconImg": this.data.img,
  339. "summary": this.data.summary,
  340. "productId": this.data.productId,
  341. "grade":this.data.grade,
  342. "exampleId": this.data.exampleId
  343. };
  344. httpRequestApi.postWork(uid, data).success(res => {
  345. wx.hideLoading({
  346. success: () => {
  347. wx.showToast({
  348. title: '上传成功',
  349. icon: 'success',
  350. duration: 1000,
  351. success: () => {
  352. console.log(res);
  353. wx.redirectTo({
  354. url: `../../social/works/works?id=${res.data.data.id}&flowerCount=${res.data.count}`
  355. })
  356. }
  357. })
  358. }
  359. });
  360. })
  361. };
  362. },
  363. // 获取本课朗读内容
  364. getReadInfo: function (id,pageNo, pageSize) {
  365. // const uid = wx.getStorageSync('uid');
  366. const data = {
  367. exampleId: this.data.id || id,
  368. pageNo: this.data.pageNo,
  369. pageSize: 3,
  370. type:'READ'
  371. };
  372. httpRequestApi.getClassRead(this.uid, data).success(res => {
  373. const readInfo = res.data.data.list;
  374. console.log(res)
  375. readInfo.forEach(item => {
  376. const temp = {};
  377. temp.title = item.userRead ? item.userRead.title : '';
  378. temp.img = item.userRead.iconImg;
  379. temp.plays = item.userRead.playAmount ? item.userRead.playAmount : 0;
  380. temp.likes = item.userRead.likeAmount ? item.userRead.likeAmount : 0;
  381. temp.classId = item.userRead.lessonId;
  382. temp.time = formatDate(item.userRead.gmtCreated, 3);
  383. temp.avatar = item.user ? item.user.avatar : '';
  384. temp.uid = item.user ? item.user.uid : '';
  385. temp.url = item.userRead.originVideo;
  386. // temp.avatar = item.user.avatar;
  387. temp.nickName = item.user ? item.user.wechatName : '';
  388. temp.id = item.userRead.id;
  389. // recommendWorks.push(temp);
  390. // that.data.hotData.hotWorks.push(temp);
  391. this.data.videoList.push(temp);
  392. });
  393. this.setData({
  394. videoList: this.data.videoList
  395. })
  396. });
  397. },
  398. // 评论区点击
  399. commentTap: function (e) {
  400. console.log('点击评论区', e)
  401. if (e.target.dataset.type === 'blank') {
  402. this.setData({
  403. commentShow: false
  404. })
  405. }
  406. },
  407. scrollToLower: function (e) {
  408. console.log('滑动到底部', e)
  409. this.setData({
  410. pageNo: this.data.pageNo + 1
  411. }, () => {
  412. this.getReadInfo()
  413. })
  414. },
  415. scrollToUpper: function (e) {
  416. console.log('滑动到顶部', e)
  417. },
  418. // 打开评论
  419. openComment: function (e) {
  420. console.log('id', e.detail.activeId)
  421. this.setData({
  422. commentShow: !this.data.commentShow,
  423. commentId: e.detail.activeId,
  424. });
  425. },
  426. })