// pages/newtest/newtest.js Page({ /** * 页面的初始数据 */ data: { sliderValue: 0, //控制进度条slider的值, updateState: false, //防止视频播放过程中导致的拖拽失效 playStates: false //控制播放 & 暂停按钮的显示 }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { this.videoContext = wx.createVideoContext('myVideo'); this.setData({ updateState: true }) }, videoUpdate(e) { if (this.data.updateState) { //判断拖拽完成后才触发更新,避免拖拽失效 let sliderValue = e.detail.currentTime / e.detail.duration * 100; let time = e.detail.currentTime.toFixed(0); // let time = sliderValue.toFixed(0); console.log('试看=>'+time); if (time >= 20) { this.videoContext.pause(); } this.setData({ duration: e.detail.duration, playStates:false, sliderValue:time }) } }, sliderChanging(e) { const times=this.data.sliderValue; if(times>=20){ this.videoContext.pause(); console.log(times); }else{ this.videoContext.play(); } this.setData({ updateState: false //拖拽过程中,不允许更新进度条 }) }, sliderChange(e) { if (this.data.duration) { // const times=e.detail.value / 100 * this.data.duration; const times=this.data.sliderValue; if(times>=20){ this.videoContext.pause(); console.log(times); }else{ this.videoContext.play(); } this.setData({ sliderValue: e.detail.value, updateState: false //完成拖动后允许更新滚动条 }) } }, videoOpreation() { const times= this.data.sliderValue; console.log(times); if(times>=20){ this.videoContext.pause(); // this.data.playStates ? this.videoContext.pause() : this.videoContext.play(); this.setData({ playStates: !this.data.playStates }) } }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })