HTML5制作简单视频播放器

1.主页面

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>视频播放器</title>
		<link rel="stylesheet" type="text/css" href="css/23-1.css"/>
		<link rel="stylesheet" type="text/css" href="font-awesome-4.7.0/css/font-awesome.css"/>
		<script src="js/jquery-1.9.1.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/23.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<body>
	<h3 class="playerTitle">视频播放器</h3>
	<div class="player">
		<video src="MP4/Houndmouth-Sedona(高清).mp4"></video>
		<div id="controls" class="controls">
			<!--播放/暂停按钮-->
			<a href="javascript:;" class="switch fa fa-play"></a>
			<!--最大化按钮-->
			<a href="javascript:;" class="expand fa fa-expand"></a>
			<!--进度条哦-->
			<div class="progress">
				<!--响应事件-->
				<div class="bar"></div>
				<!--已加载-->
				<div class="loaded"></div>
				<div class="elapse"></div>
			</div>
			<div class="time">
				<span class="currentTiime">00:00:00</span>
				\
				<span class="totalTime">00:00:00</span>
			</div>
		</div>
	</div>
	</body>
</html>

2.CSS

body{
	padding: 0;
	margin: 0;
	font-family: "微软雅黑",helvetica,simhei,simsun,sans-serif;
	background-color: #F7F7F7;
}
a{
	text-decoration: none;
}
.playerTitle{
	width: 100%;
	margin: 0 auto;
	line-height: 100px;
	font-size: 40px;
	text-align: center;
}
.player{
	width: 720px;
	height: 360px;
	margin: 0 auto;
	background: url(../img/23.gif) center no-repeat;
	background-size: cover;
	position: relative;
}
video{
	height: 100%;
	margin: 0 auto;
	display: none;
}
.controls{
	width: 720px;
	height: 40px;
	position: absolute;
	left: 0px;
	bottom: -40px;
	background-color: #000;
}
.controls > .switch{
	width: 20px;
	height: 20px;
	display: block;
	font-size: 20px;
	color: #fff;
	position: absolute;
	left: 10px;
	top: 10px;
}
.controls > .expand{
	width: 20px;
	height: 20px;
	display: block;
	font-size: 20px;
	color: #fff;
	position: absolute;
	right: 10px;
	top: 10px;
}
.controls > .progress{
	width: 430px;
	height: 10px;
	position: absolute;
	left: 40px;
	bottom: 15px;
	background-color: #555;
}
.controls > .progress > .bar{
	width: 100%;
	height: 100%;
	background-color: #999;
	border-radius: 3px;
	cursor: pointer;
	position: absolute;
	left: 0;
	top: 0;
	/*设置透明度为0*/
	opacity: 0;
	/*设置优先级*/
	z-index: 999;
}
.controls > .progress > .loaded{
	width: 60%;
	height: 100%;
	background-color: #999;
	border-radius: 3px;
	position: absolute;
	left: 0;
	top: 0;
	z-index: 2;
}
.controls > .progress > .elapse{
	width: 0%;
	height: 100%;
	background-color: #fff;
	border-radius: 3px;
	position: absolute;
	left: 0;
	top: 0;
	z-index: 3;
}
.controls > .time{
	height: 20px;
	position: absolute;
	left: 490px;
	top: 10px;
	color: #fff;
	font-size: 14px;
}

3.JS

/*通过jq来实现功能*/
$(function(){
	/*1.获取播放器*/
	var video=$("video")[0];
	
	/*2.实现播放与暂停*/
	$(".switch").click(function(){
		/*实现播放与暂停的切换,如果是暂停 >> 播放 ,如果是播放 >> 暂停*/
		if(video.paused){
			video.play();
			/*移除暂停样式,添加播放样式*/
		}
		else{
			video.pause();
			/*移除播放样式,添加暂停样式*/
		}
		/*设置标签的样式*/
		/*toggleClass:在两个样式之间做切换
		 * fa-play:播放
		 * fa-pause:暂停
		 * */
		$(this).toggleClass("fa-play fa-pause");
	});
	
	/*3.实现全屏操作*/
	$(".expand").click(function(){
		/*全屏处理,不同浏览器需要添加不同前缀 >> 能力测试*/
		if(video.requestFullscreen){
			video.requestFullscreen();
		}
		else if(video.webkitRequestFullScreen){
			video.webkitRequestFullScreen();
		}
		else if(video.msRequestFullscreen){
			video.msRequestFullscreen();
		}
		else if(video.mozRequestFullScreen){
			video.mozRequestFullScreen();
		}
	});
	
	/*4.实现播放业务逻辑:当视频文件可以播放时触发下面的事件(start)*/
	video.oncanplay=function(){
		/*由于是本地文件,视频一下子就加载出来,所以看不到过程,添加一个延时显示即可*/
		setTimeout(function(){
			/*1.将视频文件设置为显示,加载图消失*/
			video.style.display="block";
			$(".player").css("background","black");
			/*2.获取当前视频文件的总时长,duration*/
			var total=video.duration; /*00:04:23*/
			/*console.log(total);*/
			/*3.计算时分秒 60*60=3600
			 * 3700:3700/3600
			 * 3700:3700%3600 >> 100/60
			 * Math.floor():显示整数
			 * */
			/*console.log(Math.floor(3700/3600));*/
			/*补0操作:时分秒都是两位,当不足两位时需要补0*/
			/*var hour=Math.floor(total/3600);
			hour=hour<10?"0"+hour:hour;
			var minute=Math.floor(total%3600/60);
			minute=minute<10?"0"+minute:minute;
			var second=Math.floor(total%60);
			second=second<10?"0"+second:second;*/
			var result=getResult(total);
			/*测试结果*/
			/*console.log(hour+":"+minute+":"+second);*/
			
			/*4.将计算记结果展示在指定的dom元素中*/
			$(".totalTime").html(result);
		},2000);
	}
	
	/*通过总时长计算时分秒*/
	function getResult(time){
		var hour=Math.floor(time/3600);
		hour=hour<10?"0"+hour:hour;
		var minute=Math.floor(time%3600/60);
		minute=minute<10?"0"+minute:minute;
		var second=Math.floor(time%60);
		second=second<10?"0"+second:second;
		/*返回结果*/
		return hour+":"+minute+":"+second;
	}
	
	/*5.实现播放过程中的业务逻辑,当视频播放时会触发ontimeupdate事件(进行中)
	 
	 * 如果修改currentTime值也会触发这个事件,只要currentTime值变化,就会触发此事件
	 * */
	video.ontimeupdate=function(){
		/*1.获取当前播放时间*/
		var current=video.currentTime;
		console.log(current);
		/*2.计算出时分秒,调用getResult(time)函数*/
		var result=getResult(current);
		/*3.将计算记结果展示在指定的dom元素中*/
		$(".currentTiime").html(result);
		/*4.设置当前播放进度条样式*/
		var percent=current/video.duration*100+"%"
		$(".elapse").css("width",percent);	
	};
	
	/*实现视频的跳播*/
	$(".bar").click(function(e){
		/*问题:1.如何获取当前点击位置,用偏移值ofsetX
		 * 2.点击的时候总是从头开始:Hbuilder对视频的支持不够,需要从源文件中打开
		 * */
		/*1.获取当前鼠标相对于父元素的left偏移值*/
		var offset=e.offsetX;
		/*2.计算偏移值相对于总父元素总宽度的比例*/
		var percent=offset/$(this).width();
		/*3.计算这个位置对应的视频进度值*/
		var current=percent*video.duration;
		/*4.设置当前视频的currenTime*/
		video.currentTime=current;
	});
	
	/*7.播放完毕后重置播放器的状态*/
	video.onended=function(){
		video.currentTime=0;
		$(".switch").removeClass("fa-pause").addClass("fa-play");
		video.style.display="none";
	}
});

4.展示
在这里插入图片描述

5.注意
1.需要引用jquery.js.
2.MP3/MP4/IMG需要自己创建文件夹

  • 10
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值