// miniprogram/pages/previewWork/previewWork.js Page({ /** * 页面的初始数据 */ data: { muted: 'false', path:'', cloudPath: '', src: getApp().globalData.workPath }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.setData({ path:options.path }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { this.videoContext = wx.createVideoContext('myVideo'); this.myVoice = wx.createInnerAudioContext(''); }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ startPreview() { this.videoContext.play(); this.myVoice.src = getApp().globalData.workPath; this.myVoice.play(); this.myVoice.onPlay(() => { wx.showToast({ title: '开始播放', }); }) console.log("作品路径为" + getApp().globalData.workPath); /* wx.playVoice({ filePath: getApp().globalData.workPath, success: function () { wx.showToast({ title: '播放结束', }) }, }) */ // this.videoContext.play(); }, stopPreview(){ this.videoContext.stop(); }, onAdd(cloudpath) {//保存到云端,更新数据库 const db = wx.cloud.database(); console.log("cloudpath=" + cloudpath); db.collection('user-video').add({ data: { VideoPath: this.data.path, CloudPath:cloudpath, }, success: res => { // 在返回结果中会包含新创建的记录的 _id this.setData({ cloudPath: cloudpath }) wx.showToast({ title: '新增记录成功', }) console.log('[数据库] [新增记录] 成功,记录 _id: ', res._id) }, fail: err => { wx.showToast({ icon: 'none', title: '新增记录失败' }) console.error('[数据库] [新增记录] 失败:', err) } }) }, saveWork() {//保存作品并上传云存储 var path; var length=this.data.path.length-4; var index = this.data.path.substring(80,length);//得到视频路径尾部的序号 console.log("index=" + index); wx.cloud.uploadFile({ cloudPath: 'VideoWork/' + getApp().globalData.openid + index +'.mp3', filePath: getApp().globalData.workPath, success: res => { path = res.fileID; console.log("保存成功"+path); this.onAdd(path); }, fail: err => { console.log("保存失败"); } }) }, onShareAppMessage: function () { }, onShareAppMessage: function () { } })