reading.js 6.0 KB

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