文章目录
前情提要
组件:video
小程序里要放置视频,用video组件。video组件很多属性,如:
src
,string类型,表示视频资源的地址。bindplay
,当开始或继续播放时触发play事件。
API:wx.createVideoContext()创建VideoContext实例
VideoContext
实例,可调用 wx.createVideoContext()
创建。
VideoContext
实例通过 id
与一个video
组件绑定,便可操作对应的video
组件。
VideoContext
实例包含如下方法:
play()
,播放视频。stop()
,停止视频。
API:wx.showLoading()
wx.showLoading(Object object)
,显示loading提示框,调用 wx.hideLoading()
才能关闭提示框。
wx.showLoading()
接收一个对象作为参数,该对象包含如下属性:
title
,提示的内容,值是字符串类型,必填。mask
,是否显示透明蒙层,布尔值,默认是false
,即默认不显示透明蒙层。success
,接口调用成功时的回调函数。fail
,接口调用失败时的回调函数。complete
,接口调用结束(不论接口调用成功或失败)时的回调函数。
搭建视频服务器
- 全局安装serve:
npm install -g serve
- 在任何你想的地方新建文件夹:
resources
。resources下新建文件夹:images
,用于存放静态图片资源;resources下新建文件夹:videos
,用于存放视频文件。 - 启动服务器:
serve resources
。
小程序项目
代码涉及的文件有:
- 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}}"></video>
</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%;
border-radius: 10rpx;
}
pages/index/index.js
Page({
data:{
tabList:[],
tabId:'',
videoList:[]
},
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: "http://localhost:3000/images/1.jpg", videoUrl: "http://localhost:3000/videos/1.mp4"},
{vid: "28DCBBE25A686078549195E54B5F1612", coverUrl: "http://localhost:3000/images/2.jpg", videoUrl: "http://localhost:3000/videos/2.mp4"},
{vid: "C5B8699EBF70B139B702841EA41E7C4B", coverUrl: "http://localhost:3000/images/3.jpg", videoUrl: "http://localhost:3000/videos/3.mp4"},
{vid: "77C5143F298F169DDD0DD3EF116187E9", coverUrl: "http://localhost:3000/images/4.jpg", videoUrl: "http://localhost:3000/videos/4.mp4"},
];
const result2 = [
{vid: "1C612837556283740A191AD4FF774F2C", coverUrl: "http://localhost:3000/images/5.jpg", videoUrl: "http://localhost:3000/videos/5.mp4"},
{vid: "8EB59877C11DF774B68C7EA6F2F46574", coverUrl: "http://localhost:3000/images/6.jpg", videoUrl: "http://localhost:3000/videos/6.mp4"},
{vid: "F50333C85BC61F7DBC4EE65ED481747E", coverUrl: "http://localhost:3000/images/7.jpg", videoUrl: "http://localhost:3000/videos/7.mp4"},
{vid: "0C6590B104C45A471AF29C1758DC36E0", coverUrl: "http://localhost:3000/images/8.jpg", videoUrl: "http://localhost:3000/videos/8.mp4"}
]
//用定时器模拟响应耗时
setTimeout(() => {
wx.hideLoading();
if(flag === 1) this.setData({videoList:result1});
else if(flag === 2) this.setData({videoList:result2});
},500);
}
})
问题描述
在播放一个视频的时候,可以播放另一个视频,存在多个视频同时播放的问题。
解决方法
解决思路:在播放另一个视频的时候,将关闭上一个视频,保证同一时间内只有一个视频在播放。
解决措施:代码变更涉及的文件有
- pages/index/index.html,
video
组件添加bindplay
属性。 - pages/index/index.js,定义
bindplay
的事件处理函数handlePlay
。
pages/index/index.html
<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}}" bindplay="handlePlay"></video>
</view>
</scroll-view>
</view>
pages/index/index.js
Page({
data:{
tabList:[],
tabId:'',
videoList:[]
},
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: "http://localhost:3000/images/1.jpg", videoUrl: "http://localhost:3000/videos/1.mp4"},
{vid: "28DCBBE25A686078549195E54B5F1612", coverUrl: "http://localhost:3000/images/2.jpg", videoUrl: "http://localhost:3000/videos/2.mp4"},
{vid: "C5B8699EBF70B139B702841EA41E7C4B", coverUrl: "http://localhost:3000/images/3.jpg", videoUrl: "http://localhost:3000/videos/3.mp4"},
{vid: "77C5143F298F169DDD0DD3EF116187E9", coverUrl: "http://localhost:3000/images/4.jpg", videoUrl: "http://localhost:3000/videos/4.mp4"},
];
const result2 = [
{vid: "1C612837556283740A191AD4FF774F2C", coverUrl: "http://localhost:3000/images/5.jpg", videoUrl: "http://localhost:3000/videos/5.mp4"},
{vid: "8EB59877C11DF774B68C7EA6F2F46574", coverUrl: "http://localhost:3000/images/6.jpg", videoUrl: "http://localhost:3000/videos/6.mp4"},
{vid: "F50333C85BC61F7DBC4EE65ED481747E", coverUrl: "http://localhost:3000/images/7.jpg", videoUrl: "http://localhost:3000/videos/7.mp4"},
{vid: "0C6590B104C45A471AF29C1758DC36E0", coverUrl: "http://localhost:3000/images/8.jpg", videoUrl: "http://localhost:3000/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;
//bindtap,播放或继续播放时触发play事件。
//播放下一个视频时,关闭上一个视频,但继续播放原视频除外。
this.videoContext && this.vid!==vid && this.videoContext.stop();
this.vid = vid;
this.videoContext = wx.createVideoContext(vid);
}
})