0%

web前端HTML播放HLS在线视频流(m3u8)

m3u8为视频流文件,里面包含多个ts文件,ts为视频流切片,很多播放器是无法直接播放m3u8文件的,这里提供两种方式进行在线m3u8视频播放(预览)

1、hls.js

前端页面引入js:

1
<script type="text/javascript" src="./js/HLC_paly/hls.js"></script>

播放代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<video id="HLS_Player" autoplay="autoplay" loop muted controls="controls" width="100%" height="100%"></video>

<script type="text/javascript" >
HLS_Player = document.getElementById('HLS_Player');
if (Hls.isSupported()) {
HLS_Controller = new Hls();
HLS_Controller.loadSource("视屏流地址");
HLS_Controller.attachMedia(HLS_Player);
HLS_Controller.on(Hls.Events.MANIFEST_PARSED, function() {
HLS_Player.play();
});

}
// hls.js is not supported on platforms that do not have Media Source Extensions (MSE) enabled.
// When the browser has built-in HLS support (check using `canPlayType`), we can provide an HLS manifest (i.e. .m3u8 URL) directly to the video element through the `src` property.
// This is using the built-in support of the plain video element, without using hls.js.
// Note: it would be more normal to wait on the 'canplay' event below however on Safari (where you are most likely to find built-in HLS support) the video.src URL must be on the user-driven
// white-list before a 'canplay' event will be emitted; the last video event that can be reliably listened-for when the URL is not on the white-list is 'loadedmetadata'.
else if (HLS_Player.canPlayType('application/vnd.apple.mpegurl')) {
HLS_Player.src = 'http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8';
HLS_Player.addEventListener('loadedmetadata', function() {
HLS_Player.play();
});
}
</script>

2、chimee

这个插件不仅支持HTML,而且还支持手机移动端

PC端调用

1
<script src="http://lib.baomitu.com/chimee-player/1.1.10/chimee-player.browser.js"></script>

调用播放视屏流

1
2
3
4
5
6
7
8
9
10
11
<div id="wrapper"></div>
<script>
new ChimeePlayer({
wrapper: '#wrapper', // video dom容器
src: 'http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8',
box: 'hls',
isLive: true,
autoplay: true,
controls: true
});
</script>