123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- // pages/doTask/doTask.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 audioContext = wx.createInnerAudioContext() //音频播放对象
- var uploader = null //阿里云视频上传对象
- const urls = urlDef.urls;
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- talkFlag: false,
- closeName: '开始录音',
- videoList: [],
- videoFlag: 0,
- imgList: [],
- audioList:[],
- audioPause: false
- },
- getRecord: function () {
- const that = this
- if (that.data.talkFlag) {
- recordManager.stop()
- recordManager.onStop((res) => { //监听录音停止的事件
- console.log("监听录音停止事件",res)
- if (res.duration < 1000) {
- wx.showToast({
- title: '录音时间太短',
- icon: none
- })
- return;
- } else {
- console.log(res)
- var tempFilePath = res.tempFilePath; // 文件临时路径
- console.log("文件临时路径", tempFilePath)
- wx.uploadFile({
- filePath: tempFilePath,
- name: 'name',
- url: urls.file_upload,
- header: util.getHeaders(),
- success(res) {
- wx.showToast({
- title: '上传成功',
- })
-
- const rs = JSON.parse(JSON.parse(JSON.stringify(res.data)))
- console.log(rs)
- const list = [rs.data[0].url]
- that.setData({
- audioList:that.data.audioList.concat(list)
- })
- },
- fail(res) {
- wx.showToast({
- title: '上传失败',
- icon: 'none'
- })
- },
- complete(res) {
- wx.hideLoading({
- success: (res) => {},
- })
- }
- })
- }
- });
- that.setData({
- talkFlag: false,
- closeName: '开始录音'
- })
- } else {
- wx.getSetting({
- success(res) {
- if (!res.authSetting['scope.record']) {
- wx.authorize({
- scope: 'scope.record',
- success() {
- // 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问
- const options = {}
- recordManager.start(options)
- },
- fail(){
- wx.openSetting({
- withSubscriptions: true,
- })
- }
- })
- } else {
- that.setData({
- talkFlag: true,
- closeName: '正在录音,再次点击结束录音'
- })
- const options = {}
- recordManager.start(options)
- }
- }
- })
- }
- },
- showAudio:function(e){
- const index = e.currentTarget.dataset.index
- if(this.data.curAudioIndex != index){
- this.setData({
- curAudioIndex: index,
- })
- audioContext.src = this.data.audioList[index]
- audioContext.play()
- audioContext.onEnded((res)=>{
- this.setData({
- curAudioIndex: '-1',
- })
- })
- }else{
- if(this.data.audioPause){
- audioContext.play()
- this.setData({
- audioPause: false
- })
- } else {
- audioContext.pause()
- this.setData({
- audioPause: true
- })
- }
- }
- },
- delAudio:function(e){
- const index = e.currentTarget.dataset.index
- this.data.audioList.splice(index,1);
- this.setData({
- audioList: this.data.audioList
- })
- },
- uploadTap: function () {
- const that = this
-
- wx.chooseImage({
- count: 6,
- sourceType: ['album', 'camera'],
- success(res) {
- wx.showLoading({
- title: '上传中...',
- })
- for (var i in res.tempFilePaths) {
- wx.uploadFile({
- filePath: res.tempFilePaths[i],
- name: 'name',
- url: urls.file_upload,
- header: util.getHeaders(),
- success(res) {
- wx.showToast({
- title: '上传成功',
- })
- const rs = JSON.parse(JSON.parse(JSON.stringify(res.data)))
- const list = [rs.data[0].url]
- that.setData({
- imgList: that.data.imgList.concat(list)
- })
- },
- fail(res) {
- wx.showToast({
- title: '上传失败',
- icon: none
- })
- },
- complete(res) {
- wx.hideLoading({
- success: (res) => {},
- })
- }
- })
- }
- }
- })
- },
- showImg: function (e) {
- const index = e.currentTarget.dataset.index
- wx.previewImage({
- current: this.data.imgList[index],
- urls: this.data.imgList,
- })
- },
- delImg: function (e) {
- const index = e.currentTarget.dataset.index
- this.data.imgList.splice(index, 1);
- this.setData({
- imgList: this.data.imgList
- })
- },
- upLoadVideo: function () {
- const that = this
- 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;
- console.log('createUploadVideo ... ');
- 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));
- });
- }
- })
- },
- showVideo: function (e) {
- const index = e.currentTarget.dataset.index
- const curVideo = this.data.videoList[index].src
- this.setData({
- curVideo
- })
- this.videoContext.requestFullScreen({ // 设置全屏时视频的方向,不指定则根据宽高比自动判断。
- direction: 90 // 屏幕逆时针90度
- });
- },
- // 监听视频是否为全屏,否则直接关闭
- bindfullscreenchange: function () {
- if (this.data.videoFlag == 0) {
- this.setData({
- videoFlag: 1
- })
- } else {
- this.setData({
- videoFlag: 0,
- curVideo: ""
- })
- }
- },
- delVideo: function (e) {
- const index = e.currentTarget.dataset.index
- this.data.videoList.splice(index, 1);
- this.setData({
- videoList: this.data.videoList
- })
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- this.videoContext = wx.createVideoContext('play-video'); // 创建 video 上下文 VideoContext 对象。
- const that = this
- const createUpLoad = new VODUpload({
- //阿里账号ID,必须有值
- userId: "WaWQOn6gXod13WLEp8cr4ljUdvcbXJ",
- //网络原因失败时,重新上传次数,默认为3
- retryCount: 3,
- //网络原因失败时,重新上传间隔时间,默认为2秒
- retryDuration: 2,
- //开始上传
- 'onUploadstarted': function (uploadInfo) {
- wx.showLoading({
- title: '上传中...',
- mask: true
- })
- // console.log("onUploadStarted:" + uploadInfo.file.name + ", endpoint:" + uploadInfo.endpoint + ", bucket:" + uploadInfo.bucket + ", object:" + uploadInfo.object)
- //上传方式1,需要根据uploadInfo.videoId是否有值,调用点播的不同接口获取uploadauth和uploadAddress,如果videoId有值,调用刷新视频上传凭证接口,否则调用创建视频上传凭证接口
- if (uploadInfo.videoId) {
- //如果uploadInfo.videoId存在,调用刷新视频上传凭证接口
- } else {
- //如果uploadInfo.videoId不存在,调用获取视频上传地址和凭证接口
- }
- //从点播服务获取的uploadAuth、uploadAddress和videoId,设置SDK
- console.log(that.data)
- uploader.setUploadAuthAndAddress(uploadInfo, that.data.uploadAuth, that.data.uploadAddress, that.data.videoId);
- },
- //文件上传成功
- 'onUploadSucceed': function (uploadInfo) {
- // console.log("上传成功:" + JSON.stringify(uploadInfo))
- var json = {
- src: uploadInfo.url,
- img: uploadInfo.coverUrl
- }
- var list = [json]
- that.setData({
- videoList: that.data.videoList.concat(list)
- })
- wx.showToast({
- title: '上传成功',
- })
- // 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);
- wx.showToast({
- title: '上传失败',
- })
- },
- //文件上传进度,单位:字节
- 'onUploadProgress': function (uploadInfo, totalSize, loadedPercent) {
- // console.log("onUploadProgress:file:" + uploadInfo.file.name + ", fileSize:" + totalSize + ", percent:" + Math.ceil(loadedPercent * 100) + "%");
- },
- //上传凭证超时
- 'onUploadTokenExpired': function (uploadInfo) {
- wx.showToast({
- title: '上传超时',
- })
- console.log("onUploadTokenExpired");
- //实现时,根据uploadInfo.videoId调用刷新视频上传凭证接口重新获取UploadAuth
- //从点播服务刷新的uploadAuth,设置到SDK里
- uploader.resumeUploadWithAuth(uploadAuth);
- },
- //全部文件上传结束
- 'onUploadEnd': function (uploadInfo) {
- console.log("onUploadEnd: uploaded all the files");
- }
- });
- uploader = createUpLoad
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
- }
- })
|