文章目录
前情提要
组件:video
小程序里要放置视频,用video组件。video组件很多属性,如:
src
,视频资源的地址,string类型,必填。poster
,视频封面的图片,string类型,非必填。object-fit
,当视频大小与容器大小不一直时,视频的表现形式,支持以下3个值:contain
,包含(默认值)fill
,填充cover
,覆盖
bindplay
,当开始或继续播放时触发play事件。
API:wx.createVideoContext()创建VideoContext实例
VideoContext
实例,可调用 wx.createVideoContext()
创建。
VideoContext
实例通过 id
与一个video
组件绑定,便可操作对应的video
组件。
VideoContext
实例包含如下方法:
play()
,播放视频。stop()
,停止视频。
搭建视频服务器
- 全局安装serve:
npm install -g serve
- 在任何你想的地方新建文件夹:
resources
。resources下新建文件夹:images
,用于存放静态图片资源;resources下新建文件夹:videos
,用于存放视频文件。 - 启动服务器:
serve resources
。
小程序项目
问题描述:一个页面存在多个video组件时,video无法正常播放,一直在加载转圈。
解决方法:使用image列表代替video列表,只有当image被点选时,才用video组件替代image。
代码涉及的文件有:
- app.json
- pages/index/index.wxml
- pages/index/index.wxss
- pages/index/index.js
app.json
{
"pages": [
"pages/index/index"
],
"window": {
"navigationBarBackgroundColor": "#0149af",
"navigationBarTitleText": "首页",
"navigationBarTextStyle": "white"
},
"style": "v2",
"sitemapLocation": "sitemap.json"
}
pages/index/index.wxml
<view class="cameraContainer">
<view class="header">
<input type="text" class="search" placeholder="搜一搜 这里啥都有"/>
<image src="/static/images/search.png"></image>
</view>
<view class="tabContainer">
<scroll-view class="tabs" enable-flex scroll-x>
<view class="tab {{item.id===tabId?'active':''}}" wx:for="{{tabList}}" wx:key="id"
id="{{item.id}}" bindtap="changeTab">
{{item.name}}
</view>
</scroll-view>
</view>
<scroll-view class="videoList" scroll-y>
<view class="videoItem" wx:for="{{videoList}}" wx:key="vid">
<video src="{{item.videoUrl}}" id="{{item.vid}}" poster="{{item.coverUrl}}" object-fit="cover" bindplay="handlePlay" wx:if="{{videoId === item.vid}}"></video>
<image src="{{item.coverUrl}}" id="{{item.vid}}" bindtap="handlePlay" wx:else></image>
</view>
</scroll-view>
</view>
pages/index/index.wxss
.cameraContainer{
padding: 10rpx;
}
.header{
display: flex;
align-items: center;
border: 1rpx solid #aaa;
border-radius: 6rpx;
padding: 6rpx 10rpx;
}
.header image{
width: 36rpx;
height: 36rpx;
padding: 0 10rpx;
}
.header .search{
flex: 1;
height: 36rpx;
line-height: 36rpx;
font-size: 26rpx;
}
.tabContainer{
margin-top: 20rpx;
}
.tabs{
display: flex;
height: 50rpx;
}
.tab{
height: 40rpx;
line-height: 40rpx;
margin-right: 30rpx;
white-space: nowrap;
font-size: 28rpx;
}
.active{
border-bottom: 4rpx solid #0149af;
}
.videoItem{
margin-top: 10rpx;
}
.videoItem video{
width: 100%;
height: 450rpx;
border-radius: 10rpx;
}
.videoItem image{
width: 100%;
height: 450rpx;
border-radius: 10rpx;
}
pages/index/index.js
const host = "http://localhost:3000";
Page({
data:{
tabList:[],
tabId:'',
videoList:[],
videoId:""
},
onLoad(){
this.getTabList();
this.getVideoList(1);
},
changeTab(e){
// 切换tab时,因为请求视频数据可能耗时较长,故给出信息,提示正在加载
wx.showLoading({
title: '正在加载'
})
const tabId = e.target.id;
this.setData({
tabId,
videoList:[] //在新的视频数据返回前,将videoList置空,以免页面呈现旧的视频数据
});
if(tabId === "1") this.getVideoList(1);
else if(tabId === "2") this.getVideoList(2);
else this.getVideoList(2);
},
getTabList(){
let result = [
{"id": "1","name": "现场"},
{"id": "2","name": "广告"},
{"id": "3","name": "舞蹈"},
{"id": "4","name": "MV",},
{"id": "5","name": "生活"},
{"id": "6","name": "游戏"},
{"id": "7","name": "演唱会",},
{"id": "8","name": "粤语现场"},
{"id": "9","name": "华语现场"},
{"id": "10","name": "欧美现场"},
{"id": "11","name": "流行现场"},
{"id": "12","name": "民谣现场"}
]
this.setData({
tabList:result,
tabId:result.length && result[0].id || ''
})
},
getVideoList(flag){
const result1 = [
{vid: "D4FD37BC59E440F367E485680D172FFC", coverUrl: host+"/images/1.jpg", videoUrl: host+"/videos/1.mp4"},
{vid: "28DCBBE25A686078549195E54B5F1612", coverUrl: host+"/images/2.jpg", videoUrl: host+"/videos/2.mp4"},
{vid: "C5B8699EBF70B139B702841EA41E7C4B", coverUrl: host+"/images/3.jpg", videoUrl: host+"/videos/3.mp4"},
{vid: "77C5143F298F169DDD0DD3EF116187E9", coverUrl: host+"/images/4.jpg", videoUrl: host+"/videos/4.mp4"},
];
const result2 = [
{vid: "1C612837556283740A191AD4FF774F2C", coverUrl: host+"/images/5.jpg", videoUrl: host+"/videos/5.mp4"},
{vid: "8EB59877C11DF774B68C7EA6F2F46574", coverUrl: host+"/images/6.jpg", videoUrl: host+"/videos/6.mp4"},
{vid: "F50333C85BC61F7DBC4EE65ED481747E", coverUrl: host+"/images/7.jpg", videoUrl: host+"/videos/7.mp4"},
{vid: "0C6590B104C45A471AF29C1758DC36E0", coverUrl: host+"/images/8.jpg", videoUrl: host+"/videos/8.mp4"}
]
//用定时器模拟响应耗时
setTimeout(() => {
wx.hideLoading();
if(flag === 1) this.setData({videoList:result1});
else if(flag === 2) this.setData({videoList:result2});
},500);
},
handlePlay(event){
let vid = event.target.id;
this.setData({videoId:vid})
this.videoContext = wx.createVideoContext(vid);
this.videoContext.play()
}
})
相关链接
同一页面存在多个video时,video无法正常播放一直在加载转圈
使用VideoContext解决多个视频同时播放的问题
微信小程序中使用video组件
使用scroll-view实现tabs
使用微信小程序的video组件