reading.js 6.1 KB

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