如何使用H265視頻播放器EasyPlayer.JS調用videojs播放EasyNVR轉發的視頻流?

經過了多年的研發探索,TSINGSEE青犀視頻團隊開發了三種不同的視頻流媒體服務器軟件EasyNVR,EasyGBS,EasyDSS,三個平臺都可以進行網頁無插件直播,有很好的的穩定性和可靠性,同時我們也有自己的網頁播放器EasyPlayer.js ,能夠很好集成在頁面內。

EasyPlayerJS.png

項目地址:
https://www.npmjs.com/package/@easydarwin/easyplayer
EasyWasmPlayer:https://www.npmjs.com/package/@easydarwin/easywasmplayer

近期經常有客戶詢問關於使用videojs播放的問題,下面我來說明一下videojs進行web播放的demo。以下爲demo示例:

<!DOCTYPE html>
	<html>
	
	<head>
	<title>EasyNVR</title>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<link rel="stylesheet" href="plugins/video-js-5.19.2/video-js.css"/>
	<script src="plugins/video-js-5.19.2/video.js"></script>
	<script src="plugins/video-js-5.19.2/videojs-contrib-hls4.js"></script>
	<script src="plugins/videojs-hotkeys/videojs.hotkeys.min.js"></script>
	<script type="text/javascript" src="plugins/jquery-3.3.1.min.js"></script>
	</head>
	
	<body>
	<div class="content-wrapper">
	<div class="video-wrapper" style="padding-bottom:56.25%;position:relative;margin:0 auto;">
	<div class="video-inner" style="position:absolute;top:0;bottom:0;left:0;right:0;">
	<video id="videojs" class="video-js vjs-default-skin vjs-big-play-centered" style="width: 100%; height: 100%;" controls preload="none"
	poster="" x5-video-player-fullscreen=”true”,x5-video-player-type=”h5”>
	<source src="" type=""></source>
	<p class="vjs-no-js">
	To view this video please enable JavaScript, and consider upgrading to a web browser that
	<a href="http://videojs.com/html5-video-support/" target="_blank">
	supports HTML5 video
	</a>
	</p>
	</video> 
	</div>
	</div>
	</div>
	</body>
	<script>
	
	videojs.options.flash.swf = 'plugins/video-js-5.19.2/video-js-fixed.swf';
	videojs.options.techOrder = ['html5','flash'];
	$(function(){
	
	var VideoUrl = getQueryString('url')
	if (getQueryString("title")) {
	$("title").html(decodeURI(getQueryString("title")));
	}
	if(VideoUrl){
	if(VideoUrl.indexOf("http") == 0){
	setupPlayer(VideoUrl);
	}else if(VideoUrl.indexOf("rtmp") == 0){
	setupPlayer(VideoUrl);
	}
	}else{
	alert("請輸入正確的的RTMP、HLS流地址!");
	}
	})
	截取地址欄中url的參數值 
	function getQueryString(name) { 
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
	var r = window.location.search.substr(1).match(reg); 
	if (r != null) 
	// return unescape(r[2]); 
	return decodeURI(r[2]); 
	return null; 
	} 
	function setupPlayer(videoUrl) {
	videoUrl = videoUrl || "rtmp://121.40.50.44/live/stream_1";
	if(videoUrl.indexOf("rtmp") == 0){
	$("#videojs").find("source").attr("src",videoUrl).attr("type","rtmp/mp4");
	player = videojs("videojs",{
	notSupportedMessage : '您的瀏覽器沒有安裝或開啓Flash,戳我開啓!',
	techOrder : ["flash"],
	autoplay : true
	});
	videojs('videojs').ready(function() {
	this.hotkeys({
	volumeStep: 0.1,
	seekStep: 5,
	enableVolumeScroll: false,
	enableModifiersForNumbers: false
	});
	});
	player.on("error",function(e){
	var $e = $(".vjs-error .vjs-error-display .vjs-modal-dialog-content");
	var $a = $("<a href='http://www.adobe.com/go/getflashplayer' target='_blank'></a>").text($e.text());
	$e.empty().append($a);
	}) 
	} else {
	var timeout = 10000;
	var step = 500;
	var cnt = 0;
	function test(){
	cnt += step;
	$.ajax(videoUrl,{
	type : "HEAD",
	global : false,
	complete :function(xhr,ts){
	if(cnt > timeout){
	alert("請求數據失敗");
	return;
	}
	//xhr.status == 0 , when cross domain request not found
	if(xhr.status == 404 || xhr.status == 0 || (xhr.status != 200 && !isPC())){
	console.log("video is no ready, waiting...");
	setTimeout(test,step);
	}else{
	$("#videojs").find("source").attr("src", videoUrl).attr("type","application/x-mpegURL");
	player = videojs("videojs",{
	autoplay : true
	}); 
	}
	}
	})
	}
	test();
	}
	}
	</script>
	</html>

這裏需要注意正確的引用相關插件和依賴。

設置好播放依賴工具:

4.png

根據實時傳輸過來的地址來進行播放器相關屬性的初始化

5.png

實際應用效果:

6.png

在播放鏈接中加入url=“播放地址”參數進行視頻直播,播放實例:

7.png

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章