Video
视频。
属性
支持通用属性
| 属性名 | 类型 | 默认值 | 说明 | 平台支持 |
|---|---|---|---|---|
| src | String | 要播放视频的资源地址 | all | |
| initial-time | Number | 指定视频初始播放位置 | u?,w | |
| duration | Number | 指定视频时长 | u?,w | |
| controls | Boolean | true | 是否显示默认播放控件(播放/暂停按钮、播放进度、时间) | u?,w |
| danmu-list | Object Array | 弹幕列表 | u?,w | |
| danmu-btn | Boolean | false | 是否显示弹幕按钮,只在初始化时有效,不能动态变更 | u?,w |
| enable-danmu | Boolean | false | 是否展示弹幕,只在初始化时有效,不能动态变更 | u?,w |
| autoplay | Boolean | false | 是否自动播放 | u?,w |
| loop | Boolean | false | 是否循环播放 | u?,w |
| muted | Boolean | false | 是否静音播放 | u?,w |
| page-gesture | Boolean | false | 在非全屏模式下,是否开启亮度与音量调节手势 | u?,w |
| objectFit | String | contain | 当视频大小与 video 容器大小不一致时,视频的表现形式。contain:包含,fill:填充,cover:覆盖 | u?,w |
| poster | String | 默认控件上的封面,如果controls为false,则poster无效 | u?,w |
video标签认宽度300px、高度225px,设置宽高需要通过wxss设置width和height。
事件
| 事件名 | 说明 | 平台支持 |
|---|---|---|
| onfullscreenchange | 全屏切换event.detail={fullScreen:'全屏状态'} | u?,w |
| onplay | 开始或继续播放 | u?,w |
| onpause | 暂停 | u?,w |
| ontimeupdate | 播放进度改变 | u?,w |
| onended | 播放结束 | u?,w |
示例
<view class="section tc">
<video src="" controls ></video>
<view class="btn-area">
<button onclick="bindButtonTap">获取视频</button>
</view>
</view>
<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="" enable-danmu danmu-btn controls></video>
<view class="btn-area">
<button onclick="bindButtonTap">获取视频</button>
<input onblur="bindInputBlur"/>
<button onclick="bindSendDanmu">发送弹幕</button>
</view>
</view>
function getRandomColor () {
let 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: function (res) {
this.videoContext = uni.createVideoContext('myVideo')
},
inputValue: '',
data: {
src: '',
danmuList: [
{
text: '第 1s 出现的弹幕',
color: '#ff0000',
time: 1
},
{
text: '第 3s 出现的弹幕',
color: '#ff00ff',
time: 3
}]
},
bindInputBlur: function(e) {
this.inputValue = e.detail.value
},
bindButtonTap: function() {
var that = this
plus.gallery.chooseVideo({
sourceType: ['album', 'camera'],
maxDuration: 60,
camera: ['front','back'],
success: function(res) {
that.setData({
src: res.tempFilePath
})
}
})
},
bindSendDanmu: function () {
this.videoContext.sendDanmu({
text: this.inputValue,
color: getRandomColor()
})
}
})