Video 视频
基础用法
<div style="width:400px; height: 225px; margin-bottom: 100px;">
<se-video
src="http://chimee.org/vod/1.mp4"
kernel="mp4"
title="小森林·春秋"
controls
show-fullscreen-btn
show-mute-btn
muted
@ended="onEnded"
@timeupdate="onTimeupdate"
@fullscreenchange="onFullscreenchange"
@waiting="onWaiting"
@progress="onProgress"
@error="onError"
></se-video>
</div>
<se-video
ref="myVideo"
src="https://node.imgio.in/demo/birds.m3u8"
kernel="hls"
:poster="poster"
:title="title"
:controls="controls"
:muted="muted"
:loop="loop"
:autoplay="autoplay"
:initial-time="initTime"
:object-fit="objectFit"
:show-fullscreen-btn="showFullscreenBtn"
:show-mute-btn="showMuteBtn"
@play="onPlay"
@pause="onPause"
@ended="onEnded"
@timeupdate="onTimeupdate"
@fullscreenchange="onFullscreenchange"
@waiting="onWaiting"
@progress="onProgress"
@error="onError"
></se-video>
<br />
<se-button @click="emitPlay">播放</se-button>
<se-button @click="emitPause">暂停</se-button>
<se-button @click="emitStop">停止</se-button>
<se-button @click="emitSeek">跳转到3s</se-button>
<se-button @click="requestFullScreen">进入全屏</se-button>
<se-button @click="exitFullScreen">退出全屏</se-button>
<br />
<br />
<label>
设置视频标题:
<se-input :value="title" @blur="titleChange" />
</label>
<label>
是否显示控制面板:
<se-switch :checked="controls" @change="controlsChange" />
</label>
<label>
是否静音:
<se-switch :checked="muted" @change="mutedChange" />
</label>
<label>
是否显示音量按钮:
<se-switch :checked="showMuteBtn" @change="showMuteBtnChange" />
</label>
<label>
是否显示全屏按钮:
<se-switch :checked="showFullscreenBtn" @change="showFullscreenBtnChange" />
</label>
<label>
是否循环播放:
<se-switch :checked="loop" @change="loopChange" />
</label>
<label>
设置倍速:
<se-select :value="rate" @change="rateChange" size="mini">
<se-option
v-for="(option, index) in options"
:key="index"
:value="option.value"
:label="option.label"
></se-option>
</se-select>
</label>
<label>
设置播放地址:
<se-select :value="src" @change="srcChange" size="mini">
<se-option
v-for="(option, index) in srcOptions"
:key="index"
:value="option.value"
:label="option.label"
></se-option>
</se-select>
</label>
<label>
设置视频封面:
<se-select :value="poster" @change="posterChange" size="mini">
<se-option
v-for="(option, index) in posterOptions"
:key="index"
:value="option.value"
:label="option.label"
></se-option>
</se-select>
</label>
<label>
设置视频的表现形式:
<se-select :value="objectFit" @change="objectFitChange" size="mini">
<se-option
v-for="(option, index) in objectFitOptions"
:key="index"
:value="option.value"
:label="option.label"
></se-option>
</se-select>
</label>
<script>
export default {
data() {
return {
videoContext: null,
rate: 1,
options: [
{ value: 0.5, label: 0.5 },
{ value: 0.8, label: 0.8 },
{ value: 1.0, label: 1.0 },
{ value: 1.25, label: 1.25 },
{ value: 1.5, label: 1.5 },
{ value: 2, label: 2 }
],
src: 'https://www.w3cschool.cn/statics/demosource/mov_bbb.mp4',
srcOptions: [
{ value: 'https://www.w3cschool.cn/statics/demosource/mov_bbb.mp4', label: '影片1' },
{ value: 'https://www.w3school.com.cn/i/movie.ogg', label: '影片2' }
],
poster: 'https://p3.ssl.qhimg.com/t01202f3bb176275d06.png',
posterOptions: [
{ value: 'https://p3.ssl.qhimg.com/t01202f3bb176275d06.png', label: '封面1' },
{ value: 'https://p0.ssl.qhimg.com/t011ac6ee181d99574e.jpg', label: '封面2' }
],
objectFit: 'contain',
objectFitOptions: [
{ value: 'contain', label: '默认' },
{ value: 'fill', label: '填充' },
{ value: 'cover', label: '等比例填充' }
],
controls: true,
title: '小森林·春秋',
muted: false,
loop: false,
autoplay: false,
initTime: 0,
showFullscreenBtn: true,
showMuteBtn: true
}
},
methods: {
onPlay(e) {
console.log('on play')
},
onPause() {
console.log('on pause')
},
onEnded() {
console.log('on end')
},
onTimeupdate({ detail }) {
console.log('on timeupdate', detail)
},
onFullscreenchange({ detail }) {
console.log('on fullscreen', detail)
},
onWaiting() {
console.log('on waiting')
},
onProgress({ detail }) {
console.log('on progress', detail)
},
onError(error) {
console.log(`on error`, error)
},
emitPlay() {
this.videoContext.play()
},
emitPause() {
this.videoContext.pause()
},
emitStop() {
this.videoContext.stop()
},
emitSeek() {
this.videoContext.seek(3)
},
requestFullScreen() {
this.videoContext.requestFullScreen()
},
exitFullScreen() {
this.videoContext.exitFullScreen()
},
rateChange(v) {
this.rate = v
this.videoContext.playbackRate(v)
},
srcChange(v) {
this.src = v
},
controlsChange(v) {
this.controls = v
},
titleChange(e) {
this.title = e.detail.value
},
mutedChange(v) {
this.muted = v
},
loopChange(v) {
this.loop = v
},
posterChange(v) {
this.poster = v
},
objectFitChange(v) {
this.objectFit = v
},
showMuteBtnChange(v) {
this.showMuteBtn = v
},
showFullscreenBtnChange(v) {
this.showFullscreenBtn = v
}
},
mounted() {
this.videoContext = this.$createVideoContext('myVideo')
}
}
</script>
<style>
label + label {
display: block;
margin-top: 15px;
margin-bottom: 15px;
}
</style>
Props
属性 | 类型 | 默认值 | 必填 | 说明 |
---|
src | string | default | 是 | 要播放视频的资源地址 |
kernel | string | mp4 | 否 | 解码器核心 mp4/hls |
controls | boolean | true | 否 | 是否显示默认播放控件(播放/暂停按钮、播放进度、时间) |
autoplay | boolean | false | 否 | 是否自动播放 |
loop | boolean | false | 否 | 是否循环播放 |
muted | boolean | false | 否 | 是否静音播放 |
initial-time | number | 0 | 否 | 指定视频初始播放位置 |
show-fullscreen-btn | boolean | true | 否 | 是否显示全屏按钮 |
object-fit | string | contain | 否 | 当视频大小与 video 容器大小不一致时,视频的表现形式, 可选值:contain / fill / cover |
poster | string | - | 否 | 视频封面的图片网络资源地址 |
show-mute-btn | boolean | false | 否 | 是否显示静音按钮 |
title | string | - | 否 | 视频的标题,全屏时在顶部展示 |
Events
事件名称 | 描述 | 回调函数参数 |
---|
play | 当开始/继续播放时触发 play 事件 | e: Event |
pause | 当暂停播放时触发 pause 事件 | e: Event |
ended | 当播放到末尾时触发 ended 事件 | e: Event |
timeupdate | 播放进度变化时触发,event.detail = {currentTime, duration} 。 | e: Event |
fullscreenchange | 视频进入和退出全屏时触发,event.detail = {fullScreen, direction} ,direction 有效值为 horizontal | e: Event |
waiting | 视频出现缓冲时触发 | e: Event |
error | 视频播放出错时触发 | e: Event |
progress | 加载进度变化时触发,只支持一段加载。event.detail = { buffered } ,百分比 | e: Event |
Global Methods
事件名称 | 描述 | 参数 | 返回值 |
---|
$createVideoContext | 创建 video 上下文 VideoContext 对象 | $createVideoContext(id: string, this: Object) id : video 组件的 ref;this : 在自定义组件下,当前组件实例的 this,以操作组件内 video 组件 | VideoContext |
VideoContext
事件名称 | 描述 | 参数 |
---|
VideoContext.play | 播放视频 | - |
VideoContext.pause | 暂停视频 | - |
VideoContext.stop | 停止视频 | - |
VideoContext.seek | 跳转到指定位置 | position: number |
VideoContext.playbackRate | 设置倍速播放 | rate: number |
VideoContext.requestFullScreen | 进入全屏 | - |
VideoContext.exitFullScreen | 退出全屏 | - |
对微信小程序属性和方法的支持程度如下:
属性 | 是否支持 |
---|
src | ✔ |
duration | |
controls | ✔ |
danmu-list | |
danmu-btn | |
enable-danmu | |
autoplay | ✔ |
loop | ✔ |
muted | ✔ |
initial-time | ✔ |
show-progress | |
show-fullscreen-btn | ✔ |
show-play-btn | |
show-center-play-btn | |
object-fit | ✔ |
poster | ✔ |
show-mute-btn | ✔ |
title | ✔ |
play-btn-position | |
方法 | 是否支持 |
---|
VideoContext.play | ✔ |
VideoContext.pause | ✔ |
VideoContext.stop | ✔ |
VideoContext.seek | ✔ |
VideoContext.playbackRate | ✔ |
VideoContext.requestFullScreen | ✔ |
VideoContext.exitFullScreen | ✔ |
VideoContext.hideStatusBar | |
VideoContext.showStatusBar | |
VideoContext.sendDanmu | |