reading.js 5.6 KB

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