123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- // pages/demo/demo.js
- import VODUpload from '../../utils/aliyun-upload-sdk-1.0.1.min'
- const app = getApp()
- const util = require("../../utils/util")
- const urlDef = require("../../utils/urls")
- const recordManager = wx.getRecorderManager()
- const audio = wx.createInnerAudioContext()
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- video: "",
- poster: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000',
- name: '此时此刻',
- author: '许巍',
- audiosrc: 'https://www.1ting.com/api/audio?/2020/05/18/18a_yx/01.mp3',
- },
- changeVideo:function(){
- var that = this
- var uploader = new VODUpload({
- //阿里账号ID,必须有值
- userId: "WaWQOn6gXod13WLEp8cr4ljUdvcbXJ",
- //网络原因失败时,重新上传次数,默认为3
- retryCount: 3,
- //网络原因失败时,重新上传间隔时间,默认为2秒
- retryDuration: 2,
- //开始上传
- 'onUploadstarted': function (uploadInfo) {
- console.log(uploadInfo)
- // console.log("onUploadStarted:" + uploadInfo.file.name + ", endpoint:" + uploadInfo.endpoint + ", bucket:" + uploadInfo.bucket + ", object:" + uploadInfo.object)
- //上传方式1,需要根据uploadInfo.videoId是否有值,调用点播的不同接口获取uploadauth和uploadAddress,如果videoId有值,调用刷新视频上传凭证接口,否则调用创建视频上传凭证接口
- if (uploadInfo.videoId) {
- console.log('true');
- //如果uploadInfo.videoId存在,调用刷新视频上传凭证接口
- } else {
- console.log('false');
- //如果uploadInfo.videoId不存在,调用获取视频上传地址和凭证接口
- }
- //从点播服务获取的uploadAuth、uploadAddress和videoId,设置SDK
- uploader.setUploadAuthAndAddress(uploadInfo, that.data.uploadAuth, that.data.uploadAddress, that.data.videoId);
- },
- //文件上传成功
- 'onUploadSucceed': function (uploadInfo) {
- console.log("上传成功:" + JSON.stringify(uploadInfo))
- that.setData({
- poster: uploadInfo.coverUrl,
- src: uploadInfo.url
- })
- // console.log("onUploadSucceed: " + uploadInfo.file.name + ", endpoint:" + uploadInfo.endpoint + ", bucket:" + uploadInfo.bucket + ", object:" + uploadInfo.object);
- },
- //文件上传失败
- 'onUploadFailed': function (uploadInfo, code, message) {
- console.log("onUploadFailed: file:" + uploadInfo.file.name + ",code:" + code + ", message:" + message);
- },
- //文件上传进度,单位:字节
- 'onUploadProgress': function (uploadInfo, totalSize, loadedPercent) {
- // console.log("onUploadProgress:file:" + uploadInfo.file.name + ", fileSize:" + totalSize + ", percent:" + Math.ceil(loadedPercent * 100) + "%");
- },
- //上传凭证超时
- 'onUploadTokenExpired': function (uploadInfo) {
- console.log("onUploadTokenExpired");
- //实现时,根据uploadInfo.videoId调用刷新视频上传凭证接口重新获取UploadAuth
- //从点播服务刷新的uploadAuth,设置到SDK里
- uploader.resumeUploadWithAuth(uploadAuth);
- },
- //全部文件上传结束
- 'onUploadEnd': function (uploadInfo) {
- console.log("onUploadEnd: uploaded all the files");
- }
- });
- wx.chooseVideo({
- camera: ['album'],
- success(res){
- var file = {url: res.tempFilePath, coverUrl: res.thumbTempFilePath};
- var userData = '{"Vod":{}}';
- const urls = urlDef.urls;
- let createUrl = urls.video_create_upload + '?title=' + res.tempFilePath + '&fileName=' + res.tempFilePath;
- util.apiPost(createUrl).then((rs) => {
-
- let d = JSON.parse(rs.data);
- // console.log('createUploadVideo ' + JSON.stringify(d));
- that.setData({
- uploadAuth : d.UploadAuth,
- uploadAddress : d.UploadAddress,
- videoId : d.VideoId,
- })
- uploader.addFile(file, null, null, null, userData)
- uploader.startUpload();
- // uploader.setUploadAuthAndAddress(uploadInfo, uploadAuth, uploadAddress, videoId);
- }, e => {
- console.log('createUploadVideo error ' + JSON.stringify(e));
- });
- }
- })
- },
- audioPlay: function () {
-
- // audio.src = 'https://webfs.yun.kugou.com/202102261333/6839aa7db664ae01c1f11d5185d92c13/G129/M08/1B/0B/IYcBAFxqmLKABFylADPZC9WDX8A077.mp3';
- audio.src = this.data.audioSrc
- audio.play()
- // this.audioCtx.play()
- },
- audioPause: function () {
- this.audioCtx.pause()
- },
- audio14: function () {
- this.audioCtx.seek(14)
- },
- audioStart: function () {
- this.audioCtx.seek(0)
- },
- startRecord:function(){
- var that = this
- wx.getSetting({
- success(res) {
- if (!res.authSetting['scope.record']) {
- wx.authorize({
- scope: 'scope.record',
- success () {
- // 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问
- that.getRecord()
- }
- })
- } else {
- that.getRecord()
- }
- }
- })
-
-
- },
- getRecord: function () {
- const options = {}
- recordManager.start(options)
- },
- endRecord: function () {
- recordManager.stop()
- recordManager.onStop((res) => { //监听录音停止的事件
- console.log("监听录音停止事件",res)
- if (res.duration < 1000) {
- wx.showToast({
- title: '录音时间太短'
- })
- return;
- } else {
-
- var tempFilePath = res.tempFilePath; // 文件临时路径
- console.log("文件临时路径", tempFilePath)
- audio.src = tempFilePath
- audio.play()
- }
- });
- },
- playAudio:function(){
- this.audioContext.play()
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- this.audioCtx = wx.createAudioContext('myAudio')
-
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
-
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
- }
- })
|