demo.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // pages/demo/demo.js
  2. import VODUpload from '../../utils/aliyun-upload-sdk-1.0.1.min'
  3. const app = getApp()
  4. const util = require("../../utils/util")
  5. const urlDef = require("../../utils/urls")
  6. const recordManager = wx.getRecorderManager()
  7. const audio = wx.createInnerAudioContext()
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. video: "",
  14. poster: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000',
  15. name: '此时此刻',
  16. author: '许巍',
  17. audiosrc: 'https://www.1ting.com/api/audio?/2020/05/18/18a_yx/01.mp3',
  18. },
  19. changeVideo:function(){
  20. var that = this
  21. var uploader = new VODUpload({
  22. //阿里账号ID,必须有值
  23. userId: "WaWQOn6gXod13WLEp8cr4ljUdvcbXJ",
  24. //网络原因失败时,重新上传次数,默认为3
  25. retryCount: 3,
  26. //网络原因失败时,重新上传间隔时间,默认为2秒
  27. retryDuration: 2,
  28. //开始上传
  29. 'onUploadstarted': function (uploadInfo) {
  30. console.log(uploadInfo)
  31. // console.log("onUploadStarted:" + uploadInfo.file.name + ", endpoint:" + uploadInfo.endpoint + ", bucket:" + uploadInfo.bucket + ", object:" + uploadInfo.object)
  32. //上传方式1,需要根据uploadInfo.videoId是否有值,调用点播的不同接口获取uploadauth和uploadAddress,如果videoId有值,调用刷新视频上传凭证接口,否则调用创建视频上传凭证接口
  33. if (uploadInfo.videoId) {
  34. console.log('true');
  35. //如果uploadInfo.videoId存在,调用刷新视频上传凭证接口
  36. } else {
  37. console.log('false');
  38. //如果uploadInfo.videoId不存在,调用获取视频上传地址和凭证接口
  39. }
  40. //从点播服务获取的uploadAuth、uploadAddress和videoId,设置SDK
  41. uploader.setUploadAuthAndAddress(uploadInfo, that.data.uploadAuth, that.data.uploadAddress, that.data.videoId);
  42. },
  43. //文件上传成功
  44. 'onUploadSucceed': function (uploadInfo) {
  45. console.log("上传成功:" + JSON.stringify(uploadInfo))
  46. that.setData({
  47. poster: uploadInfo.coverUrl,
  48. src: uploadInfo.url
  49. })
  50. // console.log("onUploadSucceed: " + uploadInfo.file.name + ", endpoint:" + uploadInfo.endpoint + ", bucket:" + uploadInfo.bucket + ", object:" + uploadInfo.object);
  51. },
  52. //文件上传失败
  53. 'onUploadFailed': function (uploadInfo, code, message) {
  54. console.log("onUploadFailed: file:" + uploadInfo.file.name + ",code:" + code + ", message:" + message);
  55. },
  56. //文件上传进度,单位:字节
  57. 'onUploadProgress': function (uploadInfo, totalSize, loadedPercent) {
  58. // console.log("onUploadProgress:file:" + uploadInfo.file.name + ", fileSize:" + totalSize + ", percent:" + Math.ceil(loadedPercent * 100) + "%");
  59. },
  60. //上传凭证超时
  61. 'onUploadTokenExpired': function (uploadInfo) {
  62. console.log("onUploadTokenExpired");
  63. //实现时,根据uploadInfo.videoId调用刷新视频上传凭证接口重新获取UploadAuth
  64. //从点播服务刷新的uploadAuth,设置到SDK里
  65. uploader.resumeUploadWithAuth(uploadAuth);
  66. },
  67. //全部文件上传结束
  68. 'onUploadEnd': function (uploadInfo) {
  69. console.log("onUploadEnd: uploaded all the files");
  70. }
  71. });
  72. wx.chooseVideo({
  73. camera: ['album'],
  74. success(res){
  75. var file = {url: res.tempFilePath, coverUrl: res.thumbTempFilePath};
  76. var userData = '{"Vod":{}}';
  77. const urls = urlDef.urls;
  78. let createUrl = urls.video_create_upload + '?title=' + res.tempFilePath + '&fileName=' + res.tempFilePath;
  79. util.apiPost(createUrl).then((rs) => {
  80. let d = JSON.parse(rs.data);
  81. // console.log('createUploadVideo ' + JSON.stringify(d));
  82. that.setData({
  83. uploadAuth : d.UploadAuth,
  84. uploadAddress : d.UploadAddress,
  85. videoId : d.VideoId,
  86. })
  87. uploader.addFile(file, null, null, null, userData)
  88. uploader.startUpload();
  89. // uploader.setUploadAuthAndAddress(uploadInfo, uploadAuth, uploadAddress, videoId);
  90. }, e => {
  91. console.log('createUploadVideo error ' + JSON.stringify(e));
  92. });
  93. }
  94. })
  95. },
  96. audioPlay: function () {
  97. // audio.src = 'https://webfs.yun.kugou.com/202102261333/6839aa7db664ae01c1f11d5185d92c13/G129/M08/1B/0B/IYcBAFxqmLKABFylADPZC9WDX8A077.mp3';
  98. audio.src = this.data.audioSrc
  99. audio.play()
  100. // this.audioCtx.play()
  101. },
  102. audioPause: function () {
  103. this.audioCtx.pause()
  104. },
  105. audio14: function () {
  106. this.audioCtx.seek(14)
  107. },
  108. audioStart: function () {
  109. this.audioCtx.seek(0)
  110. },
  111. startRecord:function(){
  112. var that = this
  113. wx.getSetting({
  114. success(res) {
  115. if (!res.authSetting['scope.record']) {
  116. wx.authorize({
  117. scope: 'scope.record',
  118. success () {
  119. // 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问
  120. that.getRecord()
  121. }
  122. })
  123. } else {
  124. that.getRecord()
  125. }
  126. }
  127. })
  128. },
  129. getRecord: function () {
  130. const options = {}
  131. recordManager.start(options)
  132. },
  133. endRecord: function () {
  134. recordManager.stop()
  135. recordManager.onStop((res) => { //监听录音停止的事件
  136. console.log("监听录音停止事件",res)
  137. if (res.duration < 1000) {
  138. wx.showToast({
  139. title: '录音时间太短'
  140. })
  141. return;
  142. } else {
  143. var tempFilePath = res.tempFilePath; // 文件临时路径
  144. console.log("文件临时路径", tempFilePath)
  145. audio.src = tempFilePath
  146. audio.play()
  147. }
  148. });
  149. },
  150. playAudio:function(){
  151. this.audioContext.play()
  152. },
  153. /**
  154. * 生命周期函数--监听页面加载
  155. */
  156. onLoad: function (options) {
  157. this.audioCtx = wx.createAudioContext('myAudio')
  158. },
  159. /**
  160. * 生命周期函数--监听页面初次渲染完成
  161. */
  162. onReady: function () {
  163. },
  164. /**
  165. * 生命周期函数--监听页面显示
  166. */
  167. onShow: function () {
  168. },
  169. /**
  170. * 生命周期函数--监听页面隐藏
  171. */
  172. onHide: function () {
  173. },
  174. /**
  175. * 生命周期函数--监听页面卸载
  176. */
  177. onUnload: function () {
  178. },
  179. /**
  180. * 页面相关事件处理函数--监听用户下拉动作
  181. */
  182. onPullDownRefresh: function () {
  183. },
  184. /**
  185. * 页面上拉触底事件的处理函数
  186. */
  187. onReachBottom: function () {
  188. },
  189. /**
  190. * 用户点击右上角分享
  191. */
  192. onShareAppMessage: function () {
  193. }
  194. })