微信小程序使用视频播放器video组件

在app.json中配置好页面路由和权限。

1.app.json

 {
  "pages":[
    "pages/video/video"
],
"permission": {
    "scope.writePhotosAlbum": {
      "desc": "读取相册"
    }
  }
}

使用video组件

2.video.wxml


<view class="section tc">
  <video
    id="myVideo"
    src="http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400"
    danmu-list="{{danmuList}}"
    enable-danmu
    danmu-btn
    controls
  ></video>
  <view class="btn-area">
    <button bindtap="bindButtonTap">获取视频</button>
    <input bindblur="bindInputBlur" />
    <button bindtap="bindSendDanmu">发送弹幕</button>
  </view>
</view>

3. audio.js

function getRandomColor() {
  const rgb = []
  for (let i = 0; i < 3; ++i) {
    let color = Math.floor(Math.random() * 256).toString(16)
    color = color.length == 1 ? '0' + color : color
    rgb.push(color)
  }
  return '#' + rgb.join('')
}

Page({
  onReady(res) {
    this.videoContext = wx.createVideoContext('myVideo')
  },
  inputValue: '',
  data: {
    src: '',
    danmuList: [
      {
        text: '第 1s 出现的弹幕',
        color: '#ff0000',
        time: 1
      },
      {
        text: '第 3s 出现的弹幕',
        color: '#ff00ff',
        time: 3
      }]
  },
  bindInputBlur(e) {
    this.inputValue = e.detail.value
  },
  bindButtonTap() {
    const that = this
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      camera: ['front', 'back'],
      success(res) {
        that.setData({
          src: res.tempFilePath
        })
      }
    })
  },
  bindSendDanmu() {
    this.videoContext.sendDanmu({
      text: this.inputValue,
      color: getRandomColor()
    })
  }
})

效果:

在这里插入图片描述

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
好的,微信小程序开发视频播放器的步骤如下: 1. 在小程序中引入video组件小程序的wxml文件中引入video组件,例如: ``` <video src="{{videoUrl}}" poster="{{posterUrl}}" controls></video> ``` 2. 设置视频地址和封面图 在小程序的js文件中设置视频地址和封面图,例如: ``` Page({ data: { videoUrl: 'http://example.com/video.mp4', posterUrl: 'http://example.com/poster.jpg' } }) ``` 3. 添加播放/暂停功能 在小程序的wxml文件中添加播放/暂停按钮,例如: ``` <video src="{{videoUrl}}" poster="{{posterUrl}}" id="myVideo"></video> <button bindtap="playVideo">播放</button> <button bindtap="pauseVideo">暂停</button> ``` 在小程序的js文件中添加播放/暂停功能,例如: ``` Page({ data: { videoUrl: 'http://example.com/video.mp4', posterUrl: 'http://example.com/poster.jpg' }, playVideo: function() { var video = wx.createVideoContext('myVideo') video.play() }, pauseVideo: function() { var video = wx.createVideoContext('myVideo') video.pause() } }) ``` 4. 添加全屏/退出全屏功能 在小程序的wxml文件中添加全屏/退出全屏按钮,例如: ``` <video src="{{videoUrl}}" poster="{{posterUrl}}" id="myVideo" enablefullscreen></video> <button bindtap="enterFullScreen">全屏</button> <button bindtap="exitFullScreen">退出全屏</button> ``` 在小程序的js文件中添加全屏/退出全屏功能,例如: ``` Page({ data: { videoUrl: 'http://example.com/video.mp4', posterUrl: 'http://example.com/poster.jpg', isFullScreen: false }, enterFullScreen: function() { var video = wx.createVideoContext('myVideo') video.requestFullScreen() this.setData({ isFullScreen: true }) }, exitFullScreen: function() { var video = wx.createVideoContext('myVideo') video.exitFullScreen() this.setData({ isFullScreen: false }) } }) ``` 以上就是微信小程序开发视频播放器的基本步骤。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柚子·小哥哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值