当不需要动态加载视频源的时候 直接引入插件的地址即可
<div class="videoBox">
<video class="video-js vjs-big-play-centered" controls preload="auto" width="640" height="264" data-setup="{}">
<source id="changeVideo" src="http://1256144954.vod2.myqcloud.com/0df5a8e8vodgzp1256144954/f13da5507447398155231379472/playlist.m3u8" type="application/x-mpegURL">
video>
div>
<script src="video.min.js">script>
<script src="videojs.hls.min.js">script>
当需要动态加载视频源
var player = videojs('myPlayer');
var changeVideo = function(vdoSrc){
console.log(vdoSrc);
if(/\.m3u8$/.test(vdoSrc)){//判断视频源是否是m3u8的格式
player.src({
src: vdoSrc,
type: 'application/x-mpegURL'//在重新添加视频源的时候需要给新的type的值
})
}else{
player.src(vdoSrc)
}
player.load();
player.play();
}
var loadVdo = document.getElementById('loadVdo');
var src = 'http://1256144954.vod2.myqcloud.com/0df5a8e8vodgzp1256144954/f13da5507447398155231379472/playlist.m3u8';
loadVdo.addEventListener('click',function(){
changeVideo(src);
})
插件下载地址:https://github.com/videojs/videojs-contrib-hls/releases/tag/v5.14.1
官网案例地址:http://videojs.github.io/videojs-contrib-hls/
以下是播放列表:(友情支持-小平平)
主要实现:上一个 下一个 点击具体视频的按钮切换 是否自动播放>
插入js文件
<script src="jquery.js">script>
<script src="video.min.js">script>
<script src="videojs.hls.min.js">script>
HTML代码部分
<label>
<input id="autoNext" type="checkbox" name="autoNext"/>自动播放下一小节
label>
<div class="chapter">
<button class="playVideo active">1button>
<button class="playVideo">2button>
<button class="playVideo">4button>
div>
JS部分
var currentIndex = 0;//当前播放的小节
var vdoList = [
{id:1,src:'http://1256144954.vod2.myqcloud.com/0df5a8e8vodgzp1256144954/f13da5507447398155231379472/playlist.m3u8'},
{id:2,src:'video_data/03.mp4'},
{id:4,src:'video_data/iu.mp4'}
];
var player = videojs('myPlayer',{},function(){
//在菜单项暂停键前后添加上一节下一节
$(".vjs-play-control").before('previous">上一个');
$(".vjs-play-control").after('next">下一个');
});
var changeVideo = function(vdoSrc){
//判断播放的是哪一个
currentIndex = getSrc('current',vdoSrc,vdoList);
console.log(currentIndex)
if(/\.m3u8$/.test(vdoSrc)){//判断视频源是否是m3u8的格式
player.src({
src: vdoSrc,
type: 'application/x-mpegURL'//在重新添加视频源的时候需要给新的type的值
})
}else{
player.src(vdoSrc)//普通视频
}
player.load();
player.play();
//添加点击样式 eq是从0开始
$('.playVideo').removeClass('active').eq(currentIndex-1).addClass('active');
}
player.on('ended',function(){
if( $('#autoNext').is(':checked') && (currentIndex + 1) <= vdoList.length ){
var src = getSrc('src',currentIndex + 1,vdoList)
changeVideo( src );
}
})
$('.playVideo').on('click',function(){
var index = $(this).index() + 1;
src = getSrc('src',index,vdoList);
changeVideo( src );
})
上一个按钮
这里用事件委托以及事件冒泡的原理给上一个按钮绑定点击事件
下一个按钮同理
$('body').on('click','#previous',function(){
if( (currentIndex - 1) > 0 ){
var src = getSrc('src',currentIndex - 1, vdoList);
changeVideo( src );
}else{
console.log('没有上一个了')
}
})
下一个点击按钮
//下一个
$('body').on('click','#next',function(){
if( (currentIndex + 1) <= vdoList.length ){
var src = getSrc('src',currentIndex + 1, vdoList);
changeVideo( src );
}else{
console.log('没有下一个了');
}
})
获取数据的src以及当前第几个视频
// 获取数组中对象的视频源或者当前id
var getSrc = function(type,params,vdoList){
for( var i = 0;i < vdoList.length; i++){
if( type == 'current' ){
if( vdoList[i].src == params ){
return i+1;
}
}else if( type == 'src' ){
if( (i+1) == params ){
return vdoList[i].src;
}
}
}
}
踩的坑: