HomeDocs
跳到主要内容

native-video 视频

简介

非同层原生视频组件

基础库 4.0.0 版本起支持使用 dlt.createVideoContext 对 native-video 组件进行操作。

属性

属性名类型默认值必填说明最低版本
srcstring要播放视频的资源地址,支持网络路径、本地临时路径4.0.0
loopbooleanfalse是否循环播放4.0.0
mutedbooleanfalse是否静音播放4.0.0
autoplaybooleanfalse是否自动播放4.0.0
posterstring视频封面的图片的资源地址4.0.0
directionnumber0设置全屏方向;0: 正常竖直;90:屏幕逆时针 90 度;-90:屏幕顺时针 90 度4.0.0
initial-timenumber0指定视频初始播放位置4.0.0
cachebooleanfalse是否缓存视频4.0.0
bindplayeventhandle当开始/继续播放时触发 play 事件4.0.0
bindpauseeventhandle当暂停播放时触发 pause 事件4.0.0
bindendedeventhandle当播放到末尾时触发 ended 事件4.0.0
bindtimeupdateeventhandle播放进度变化时触发,event.detail = {currentTime, duration} 。触发频率 250ms 一次4.0.0
bindwaitingeventhandle视频出现缓冲时触发4.0.0
binderroreventhandle视频播放出错时触发4.0.0
bindprogresseventhandle加载进度变化时触发,只支持一段加载。4.0.0
bindloadedmetadataeventhandle视频元数据加载完成时触发。event.detail = {width, height, duration}4.0.0
bindseekcompleteeventhandleseek 完成时触发4.0.0
createdsuccesseventhandle视频组件创建完成后会返回,event.createdsuccess = true:创建成功;event.createdsuccess = false: 创建失败4.0.0

使用示例

<!--index.dlt-->
<native-video
data-url="data--url--content"
data-content="data--detail-content"
id="myVideo"
:autoplay="autoplay"
:poster="poster"
:src="src"
bind:bindplay="bindplay"
bind:bindpause="bindpause"
bind:bindended="bindended"
bind:binderror="binderror"
bind:bindtimeupdate="bindtimeupdate"
bind:createdsuccess="createdsuccess"
>
</native-video>

<view bind:tap="triggerPlay"> 点击播放 </view>
<view bind:tap="triggerPause"> 点击暂停 </view>
<view bind:tap="triggerStop"> 点击停止 </view>
<view bind:tap="triggerSeek"> 跳转到指定位置 </view>
// index.js
Page({
data: {
autoplay: true,
src: '/images/big_buck_bunny.mp4',
poster: '/images/test.png',
},
onReady() {
console.log('page ready');
this.videoContext = dlt.createVideoContext('myVideo');
},
bindplay(event) {
console.log('bindplay', event);
},
bindpause(event) {
console.log('bindpause', event);
},
bindended(event) {
console.log('bindended', event);
},
binderror(event) {
console.log('binderror', event);
},
bindtimeupdate(event) {
console.log('bindtimeupdate', event);
},
createdsuccess(event) {
// 成功时 event.createdsuccess 为 true;失败 event.createdsuccess 为 false
console.log('createdsuccess result:', event.createdsuccess); // createdsuccess result:true
console.log('createdsuccess target:', event.target); // 视频元素 dom 信息
},
// 通过 videoContext 调用视频方法
triggerPlay() {
this.videoContext && this.videoContext.play();
},
triggerPause() {
this.videoContext && this.videoContext.pause();
},
triggerStop() {
this.videoContext && this.videoContext.stop();
},
triggerSeek() {
// 设置播放进度到 10 秒位置处
this.videoContext && this.videoContext.seek(10);
},
});

/**
* event 示例:
{
detail: {
currentTime: 0.871904,
duration: 60.095011,
height: 100,
width: 100,
},
target: {
dataset: {
content: "data--detail-content",
url: "data--url--content",
},
id: "myVideo",
offsetLeft: 16,
offsetTop: 318,
},
timeStamp: 11574.799999982119,
type: "timeupdate",
}
*/

错误码对应关系

errCode含义
-1STATE_ERROR
-2STATE_NETWORK_ERROR
-3STATE_PARSE_ERROR
-4STATE_URL_NULL
Privacy agreementDeveloper agreementcontact us: developer_service.mi@transsion.com © 2024 MiniApp. All Rights Reserved.