小程序video的各种事件 如何让小程序视频只播放前五秒

小程序video的各种事件 如何让小程序视频只播放前五秒,第1张

imeUpdate: function (e) {

      //实时播放进度 秒数

        var currentTime = parseInt(e.detail.currentTime)

        console.log("视频播放到第" + currentTime + "秒")//查看正在播放时间,以秒为单位

        if(currentTime>300){

          let videoContext = wx.createVideoContext('myVideo', this)

          console.log(videoContext)

          videoContext.pause

          念消videoContext.seek("1")

          videoContext.pause

          wx.showModal({

            title: '充值提示',

              content: "非会员只能看前五分钟的内容",//提示内容

              confirmColor: '#2EA7E0',//确定按钮的颜色

    胡高仔          showCancel: false,//不显示取消按钮

               success (res) {

              if (res.confirm) {

                 console.log('用户点击确定按钮')

               } else if (res.cancel) {

                 console.log('用户点击取消按钮')

               }

             }

            })

        }

   裤汪 },

<video  bindtimeupdate="timeUpdate"  id="myVideo" src="{{kino_mazmuni.info.resource_url}}"  initial-time="{{kino_mazmuni.duration_time}}" title="{{kino_mazmuni.info.video_name}}" object-fit="fill"  enable-auto-rotation="true" vslide-gesture="true"  enable-play-gesture="true" auto-pause-if-navigate="true"  show-screen-lock-button="true"  duration="15"   enable-auto-rotation="true" duration="true" show-fullscreen-btn  show-casting-button="true"   autoplay="true"  poster='https://apqak.alkuyi.com/alkuyi_video_poster.png' objectFit="cover" class="bofangqi">

官网: https://developers.weixin.qq.com/miniprogram/dev/component/video.html

相关API:wx.createVideoContext(string id, Object this)

controls:是否显示默认播放控件(播放/暂停按钮、播放进度、时间)

muted:是否静音

show-play-btn:是否显示视频底部控氏隐制栏的播放按钮

show-center-play-btn:是否显示视频中间的播放按钮祥枝

show-mute-btn:是否显示静音按钮

事歼宴厅件:

bindended:当播放到末尾时触发 ended 事件

接口:

wx.createVideoContext(string id, Object this)

https://gitee.com/susuhhhhhh/wxmini_demo

首先说一下我的应用场景, 在小程序中会有多个页面存在视频组件的情况, 为了防止视频播放时多个视频组件同时播放的情况,现记录一下方案:

一. 可以在需要视频播放的地方弄一个视频播放的按钮, 当视频播放的时候再孝搜初始化视频组件, 保证整个应用内只有一个视频组件(视频组件的位置需要计算), 整个方法只是理论下的情况

二. 在app.js中 设置两个对象, currentVideoId(播放视频组件的id)和videoContext(当前播放视频的对象, 通过wx.createVideoContext方法获得), 在页面的js中控制, 保证video组件绑定play方法:

 <video src="url" bindplay="play"></video>

在play方法中实现即可:

var id = e.currentTarget.id

      if (id != null) {

        if (app.currentVideoId != id) {

          if (app.videoContext != null) {

            app.videoContext.stop()

            console.warn("暂停")

          }

          app.currentVideoId = id

          app.videoContext = wx.createVideoContext(id)

        }

   瞎燃   }

因为currentVideoId 和videoContext是唯一的对象, 方法中播放监听事件会暂停之前的播放, 然后再赋值, 等下次video播放监听进来的时候, 会进行判断

三. 包含富文本组件的视频组件同时播放的情况

依据上面的例子, 当自定义富文本组件中包含多个video组件时也用上面的方法会产生错误磨慎虚, 虽然log会显示将执行视频暂停或者停止播放, 但是没有效果, 查看 视频api , 提示

根据api显示, id为video组件的id, 还有一个"Object this", 但是依据上面的api使用, 直接用id确实可以获取video对象, 但是api解释中提示, "在自定义组件下,当前组件实例的this,以 *** 作组件内 video组件", 在自定义组件中, 要加this, 所以总结: 在上面api使用中, 如果video在pages页面中, wx.createVideoContext参数只写id没有问题, 但是当video在自定义组件中, 要必须加this, 才能正确返回VideoContext对象


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12493338.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存