reading.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. onHide: function(){
  69. if(this.innerAudioContext){
  70. this.innerAudioContext.stop();
  71. }
  72. },
  73. onUnload: function(){
  74. if(this.innerAudioContext){
  75. this.innerAudioContext.stop();
  76. }
  77. },
  78. // onShow:function(){
  79. // },
  80. // 录音中视频播放结束 (控制录音同时结束)
  81. recordingVideoEnd: function () {
  82. console.log(this.data.videoCtr)
  83. console.log('recordingVideoEnd');
  84. //
  85. if (this.data.recordFlag === 0) {
  86. // this.recordStop();
  87. this.playingVideoEnd();
  88. return;
  89. }
  90. // 录音结束
  91. if (this.data.recordFlag === 1) {
  92. this.recordStop();
  93. }
  94. },
  95. // 播放中视频播放结束 (控制录音同时结束)
  96. playingVideoEnd: function () {
  97. console.log('playingVideoEnd')
  98. this.innerAudioContext.stop();
  99. },
  100. /***
  101. * recordFlag:
  102. * 0 初始状态
  103. * 1 录音中
  104. * 2 录音结束
  105. ***/
  106. audioRecord: function () {
  107. console.log(this.data.recordFlag)
  108. if (this.data.recordFlag === 0) {
  109. // this.recordStart();
  110. // this.saveVideo();
  111. this.videoComplete();
  112. return;
  113. }
  114. // 录音结束后
  115. if (this.data.recordFlag === 1) {
  116. wx.showLoading({
  117. title: '作品转码中',
  118. mask: true
  119. })
  120. this.recordStop();
  121. }
  122. },
  123. // 录音开始
  124. /**
  125. * duration: 时长 最长10分钟
  126. sampleRate: 44100, 采样率
  127. numberOfChannels: 1, 录音通道
  128. encodeBitRate: 192000, 码率
  129. format: 'mp3', 格式
  130. frameSize: 50 制定帧大小
  131. */
  132. recordStart: function () {
  133. console.log('录音开始');
  134. const options = {
  135. duration: 600000,
  136. sampleRate: 44100,
  137. numberOfChannels: 1,
  138. encodeBitRate: 192000,
  139. format: 'mp3',
  140. frameSize: 50
  141. }
  142. this.recorderManager.start(options);
  143. },
  144. // 录音结束
  145. recordStop: function () {
  146. console.log('录音结束')
  147. this.recorderManager.stop();
  148. wx.hideLoading()
  149. },
  150. // 播放录音
  151. audioPlay: function () {
  152. console.log('音频播放');
  153. this.innerAudioContext = wx.createInnerAudioContext();
  154. this.innerAudioContext.onError((res) => {
  155. // 播放音频失败的回调
  156. })
  157. this.innerAudioContext.src = this.data.recordSource; // 这里可以是录音的临时路径
  158. console.log(this.innerAudioContext.src);
  159. this.videoCtx.play();
  160. this.innerAudioContext.play();
  161. },
  162. videoComplete: function () {
  163. // let videoUrl = 'http://efunimgs.ai160.com/ott/test/002tPr2Xlx07oP7B4ro40104120022hP0k010.mp4';
  164. this.setData({
  165. recordFlag: 1
  166. }, () => {
  167. this.videoCtx = wx.createVideoContext('myVideo', this);
  168. this.videoCtx.play();
  169. this.recordStart();
  170. })
  171. },
  172. // 上传
  173. upload: function () {
  174. wx.showLoading({
  175. title: '作品分享中',
  176. mask: true
  177. })
  178. const recordSource = this.data.recordSource;
  179. wx.uploadFile({
  180. url: 'https://readerbase.efunbox.cn/file/upload',
  181. filePath: recordSource,
  182. name: '朗读录音',
  183. header: {
  184. uid: wx.getStorageSync('uid')
  185. },
  186. success: (res) => {
  187. const formateRes = JSON.parse(res.data);
  188. let audioPath = formateRes.data;
  189. let uid = wx.getStorageSync('uid');
  190. shareWorks(uid, audioPath);
  191. }
  192. })
  193. let shareWorks = (uid, audio) => {
  194. console.log(this.data.img)
  195. const data = {
  196. "lessonId": this.data.id,
  197. "originVideo": this.data.videoUrl,
  198. "audioPath": audio,
  199. "title": this.data.title,
  200. "iconImg": this.data.img,
  201. "summary": this.data.grade,
  202. };
  203. httpRequestApi.postWork(uid, data).success(res => {
  204. wx.hideLoading({
  205. success: ()=>{
  206. wx.showToast({
  207. title: '上传成功',
  208. icon: 'success',
  209. duration: 1000,
  210. success: ()=>{
  211. console.log(res);
  212. wx.navigateTo({
  213. url: `../../social/works/works?id=${res.data.data.id}`
  214. })
  215. }
  216. })
  217. }
  218. });
  219. })
  220. };
  221. }
  222. })