5.使用hlsm.js 播放器

  1. 先推流。 
 
然后下载去github  下载hls.js   编写代码
 
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="./index.css">
<title>Document</title>
</head>
<body>
<div class="player">
<video id="video" width="400" height="200"></video>
<em class="btn"></em>
</div>
<script src="./hls.js@latest"></script>
<script src="./index.js"></script>
</body>
</html>
 
 
Index.css
 
html *,body *{
padding: 0;
margin: 0;
}
.player{
width: 400px;
height: 200px;
position: relative;
}
.player video{
width: 100%;
height: 100%;
}
.player .btn{
display: none;
width: 40px;
height: 40px;
border-radius: 50%;
position: absolute;
left: 50%;
top: 50%;
margin: -35px auto auto -35px;
padding: 15px;
background: rgba(255,255,255,0.5);
line-height: 40px;
}
.player .btn:hover{
background: rgba(255,255,255,0.7);
}
.player .btn:before{
border: 20px solid #ddd;
border-top-color: transparent;
border-bottom-color: transparent;
border-right-color: transparent;
content: " ";
display: block;
margin-left: 10px;
width: 0;
height: 0;
}
.player .btn:before:hover{
border-left-color: #fff;
}
.player.pause .btn{
display: block;
}
.player .state{
position: absolute;
bottom: 20px;
left: 40px;
font-size: 14px;
color: #000;
}
.player.pause .state{
display: none;
}
 
 
 
 
index.js
var Hls = window.Hls
var video = document.querySelector('video')
var btn = document.querySelector('.btn')
var player = document.querySelector('.player')
if (Hls.isSupported()) {
var hls = new Hls()
hls.loadSource(url)
hls.attachMedia(video)
hls.on(Hls.Events.MANIFEST_PARSED, function () {
// video.play()
})
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
video.src = url
video.addEventListener('canplay', function () {
// video.play()
})
}
 
btn.addEventListener('click', function () {
if (video.paused) {
video.play()
} else {
video.pause()
}
})
 
video.addEventListener('click', function () {
if (video.paused) {
video.play()
} else {
video.pause()
}
})
 
video.addEventListener('play', function () {
player.className = 'player'
})
 
video.addEventListener('pause', function () {
player.className = 'player pause'
})
 
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/collen/p/9031953.html