reading.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import httpRequestApi from '../../../utils/APIClient';
  2. Page({
  3. data: {
  4. title: '',
  5. id: '',
  6. img: '',
  7. fullScreenBtn: false,
  8. playBtn: false,
  9. gesture: true,
  10. muted: true,
  11. gesture: false,
  12. centerBtn: false,
  13. recordFlag: 0,
  14. recordSource: '',
  15. videoCtr: 'recordingVideoEnd',
  16. btnFlag: false,
  17. btnImgFlag: false,
  18. microphonePng: '../../../static/image/microphone.png',
  19. recordingGif: '../../../static/image/readingNow.gif',
  20. videoUrl: '',
  21. readingText: ''
  22. },
  23. onLoad: function (option) {
  24. console.log(option);
  25. this.videoCtx = null;
  26. const uid = wx.getStorageSync('uid')
  27. httpRequestApi.getClassDetail(uid, option.id).success(res => {
  28. let reg = /\\n/g
  29. this.setData({
  30. title: res.data.data.title,
  31. videoUrl: res.data.data.playUrl,
  32. img: res.data.data.iconImg,
  33. id: res.data.data.id,
  34. // readingText: res.data.data.lessonText.replace(reg,'')
  35. readingText: res.data.data.lessonText,
  36. grade: res.data.data.gradeClassify
  37. })
  38. console.log(this.data.readingText)
  39. console.log(this.data.img)
  40. }, () => {
  41. wx.setNavigationBarTitle({
  42. title: this.data.title //页面标题为路由参数
  43. })
  44. })
  45. this.recorderManager = wx.getRecorderManager();
  46. // 监听录音部分
  47. // 录音开始
  48. this.recorderManager.onStart(() => {
  49. // this.saveVideo();
  50. this.setData({
  51. btnImgFlag: true
  52. })
  53. console.log('recorder start')
  54. })
  55. // 录音结束
  56. this.recorderManager.onStop((res) => {
  57. this.videoCtx.stop();
  58. console.log('recorder stop', res)
  59. const recordFile = res.tempFilePath;
  60. this.setData({
  61. recordFlag: 0,
  62. recordSource: recordFile,
  63. btnFlag: true,
  64. btnImgFlag: false
  65. })
  66. })
  67. },
  68. // onShow:function(){
  69. // },
  70. // 录音中视频播放结束 (控制录音同时结束)
  71. recordingVideoEnd: function () {
  72. console.log(this.data.videoCtr)
  73. console.log('recordingVideoEnd');
  74. //
  75. if (this.data.recordFlag === 0) {
  76. // this.recordStop();
  77. this.playingVideoEnd();
  78. // this.setData({
  79. // videoCtr: 'playingVideoEnd'
  80. // })
  81. }
  82. // 录音结束
  83. if (this.data.recordFlag === 1) {
  84. this.recordStop();
  85. // this.recordStop();
  86. // this.innerAudioContext.stop();
  87. }
  88. },
  89. // 播放中视频播放结束 (控制录音同时结束)
  90. playingVideoEnd: function () {
  91. console.log('playingVideoEnd')
  92. this.innerAudioContext.stop();
  93. },
  94. /***
  95. * recordFlag:
  96. * 0 初始状态
  97. * 1 录音中
  98. * 2 录音结束
  99. ***/
  100. audioRecord: function () {
  101. console.log(this.data.recordFlag)
  102. if (this.data.recordFlag === 0) {
  103. // this.recordStart();
  104. // this.saveVideo();
  105. this.videoComplete();
  106. }
  107. // 录音结束后
  108. if (this.data.recordFlag === 1) {
  109. this.recordStop();
  110. }
  111. },
  112. // 录音开始
  113. /**
  114. * duration: 时长 最长10分钟
  115. sampleRate: 44100, 采样率
  116. numberOfChannels: 1, 录音通道
  117. encodeBitRate: 192000, 码率
  118. format: 'mp3', 格式
  119. frameSize: 50 制定帧大小
  120. */
  121. recordStart: function () {
  122. console.log('录音开始');
  123. const options = {
  124. duration: 600000,
  125. sampleRate: 44100,
  126. numberOfChannels: 1,
  127. encodeBitRate: 192000,
  128. format: 'mp3',
  129. frameSize: 50
  130. }
  131. this.recorderManager.start(options);
  132. },
  133. // 录音结束
  134. recordStop: function () {
  135. console.log('录音结束')
  136. this.recorderManager.stop();
  137. },
  138. // 播放录音
  139. audioPlay: function () {
  140. console.log('音频播放');
  141. this.innerAudioContext = wx.createInnerAudioContext();
  142. this.innerAudioContext.onError((res) => {
  143. // 播放音频失败的回调
  144. })
  145. this.innerAudioContext.src = this.data.recordSource; // 这里可以是录音的临时路径
  146. console.log(this.innerAudioContext.src);
  147. this.videoCtx.play();
  148. this.innerAudioContext.play();
  149. },
  150. videoComplete: function () {
  151. // let videoUrl = 'http://efunimgs.ai160.com/ott/test/002tPr2Xlx07oP7B4ro40104120022hP0k010.mp4';
  152. this.setData({
  153. recordFlag: 1
  154. }, () => {
  155. this.videoCtx = wx.createVideoContext('myVideo', this);
  156. this.videoCtx.play();
  157. this.recordStart();
  158. })
  159. },
  160. // 上传
  161. upload: function () {
  162. const recordSource = this.data.recordSource;
  163. wx.uploadFile({
  164. url: 'https://readerbase.efunbox.cn/file/upload',
  165. filePath: recordSource,
  166. name: '朗读录音',
  167. header: {
  168. uid: wx.getStorageSync('uid')
  169. },
  170. success: (res) => {
  171. const formateRes = JSON.parse(res.data);
  172. let audioPath = formateRes.data;
  173. let uid = wx.getStorageSync('uid');
  174. shareWorks(uid, audioPath);
  175. }
  176. })
  177. let shareWorks = (uid, audio) => {
  178. console.log(this.data.img)
  179. const data = {
  180. "lessonId": this.data.id,
  181. "originVideo": this.data.videoUrl,
  182. "audioPath": audio,
  183. "title": this.data.title,
  184. "iconImg": this.data.img,
  185. "summary": this.data.grade,
  186. };
  187. httpRequestApi.postWork(uid, data).success(res => {
  188. wx.showToast({
  189. title: '上传成功',
  190. icon: 'success',
  191. duration: 1000
  192. })
  193. })
  194. };
  195. }
  196. })