reading.js 7.5 KB

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