video.js 自定義使用
video.js
- 直接使用
- 自定義控制條和樣式
- 彈幕相關
- plugin寫法
video.js 是開源外掛,據說甚至可以集合到gitbook上
note:
一個需要注意的點是下載下來的demo.html直接右鍵瀏覽器是打不開的,需要用iis或者nginx 或者beego這些服務端的玩意載入後,在前端瀏覽器才能看到視訊頁面。
參考文章:
- HTML5視訊播放器Video.Js的使用
- api使用
- brightcover 播放器是基於video.js核心封裝的,所以它的文件也能參考
- download-video btn
- customize-appearance
- customize-appearance
直接使用
注意點:載入flash動畫後,chrome ie firefox播放介面才能統一,否則各有各的介面
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>無標題文件</title>
<link href="video-js/video-js.css" rel="stylesheet" type="text/css"/>
<script src="js/jquery.js" type="text/javascript" charset="utf-8"></script>
<script src="video-js/video.js" type="text/javascript" charset="utf-8"></script>
<script src="video-js/ie8/videojs-ie8.js"></script>
<!-- 載入hls視訊外掛 -->
<script src="video-js/videojs-contrib-hls.js"></script>
<!-- 載入flash播放器 -->
<script type="text/javascript">
videojs.options.flash.swf = "static/video-js/video-js.swf";
</script>
</head>
<body>
<div id="videocontainer">
<video id="example_video" class="video-js vjs-default-skin vjs-big-play-centered"
preload="auto" controls
width="800" height="600" align="middle" poster=""
data-setup="{ "html5" : { "nativeTextTracks" : false } }">
<source src="你的名字.mp4" type="video/mp4"/>
<!-- 載入hls視訊-->
<source src="http://live.hkstv.hk.lxdns.com/live/hks/playlist.m3u8"
type="application/x-mpegURL">
<!-- 載入rtmp視訊-->
<source src="rtmp://live.hkstv.hk.lxdns.com/live/hks" type="rtmp/flv"/>
</video>
</div>
</div>
</body>
</html>
自定義控制條
元件結構
Player
PosterImage
TextTrackDisplay
LoadingSpinner
BigPlayButton
ControlBar
PlayToggle
FullscreenToggle
CurrentTimeDisplay
TimeDivider
DurationDisplay
RemainingTimeDisplay
ProgressControl
SeekBar
LoadProgressBar
PlayProgressBar
SeekHandle
VolumeControl
VolumeBar
VolumeLevel
VolumeHandle
MuteToggle
使用:以新增按鈕為例子
var player = videojs("example_video", {
"controls": true,
"autoplay": false,
"preload": "auto",
"loop": false,
controlBar: {
captionsButton: false,
chaptersButton: false,
playbackRateMenuButton: true,
LiveDisplay: true,
subtitlesButton: false,
remainingTimeDisplay: true,
progressControl: true,
volumeMenuButton: {
inline: false,
vertical: true
},//豎著的音量條
fullscreenToggle: true
}
}, function () {
/*次處用於自定義控制條等*/
/*
做法1:直接append新增
*/
$(".vjs-control-bar").append('<button class="vjs-control " id="danmu_send_opt"><u>按鈕1</u></button>');
/*
做法2:dom新增
*/
var controlBar,
newElement = document.createElement('div'),
newLink = document.createElement('a'),
newImage = document.createElement('img');
// Assign id and classes to div for icon
newElement.id = 'downloadButton';
newElement.className = 'downloadStyle vjs-control';
// Assign properties to elements and assign to parents
newImage.setAttribute('src','http://solutions.brightcove.com/bcls/brightcove-player/download-video/file-download.png');
newLink.setAttribute('href','http://www.baidu.com');
newLink.appendChild(newImage);
newElement.appendChild(newLink);
// Get control bar and insert before elements
// Remember that getElementsByClassName() returns an array
controlBar = document.getElementsByClassName('vjs-control-bar')[0];
// Change the class name here to move the icon in the controlBar
insertBeforeNode = document.getElementsByClassName('vjs-fullscreen-control')[0];
// Insert the icon div in proper location
controlBar.insertBefore(newElement,insertBeforeNode);
//controlBar.appendChild(newElement);
/*
做法3:簡化2的寫法
*/
var newbtn = document.createElement('btn');
newbtn.innerHTML = '<button class="vjs-control" id="downloadButton"></button>';
var controlBar = document.getElementsByClassName('vjs-control-bar')[0];
insertBeforeNode = document.getElementsByClassName('vjs-fullscreen-control')[0];
controlBar.insertBefore(newbtn,insertBeforeNode);
});
自定義控制條完整例子:
新增滑條音訊控制和彈幕樣式開關
自定義樣式:
video.js 採用flex佈局,所以float這種不起作用
如果想要使用float,需要修改預設的video.js.css
並且後面的按鈕都要進行樣式調整
.vjs-has-started .vjs-control-bar {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
/*原為flex*/
display: block;
visibility: visible;
opacity: 1;
-webkit-transition: visibility 0.1s, opacity 0.1s;
-moz-transition: visibility 0.1s, opacity 0.1s;
-o-transition: visibility 0.1s, opacity 0.1s;
transition: visibility 0.1s, opacity 0.1s;
}
……省略無關部分
.video-js .vjs-play-control {
/*增加float*/
float:left;
cursor: pointer;
-webkit-box-flex: none;
-moz-box-flex: none;
-webkit-flex: none;
-ms-flex: none;
flex: none;
}
.video-js .vjs-fullscreen-control {
cursor: pointer;
-webkit-box-flex: none;
-moz-box-flex: none;
-webkit-flex: none;
-ms-flex: none;
flex: none;
/*增加float*/
float: right;
}
彈幕
使用開源jquery.danmu.js
video.js整合例子
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="video-js/video-js.css" rel="stylesheet" type="text/css">
<script src="video-js/video.js" type="text/javascript" charset="utf-8"></script>
<script src="video-js/ie8/videojs-ie8.js"></script>
<script src="video-js/videojs-contrib-hls.js"></script>
<script src="danmu/jquery-1.11.1.min.js"></script>
<script src="../../../../../桌面/tanmu/DanmuPlayer/src/js/jquery.danmu.js"></script>
<script type="text/javascript">
videojs.options.flash.swf = "video-js/video-js.swf";
</script>
<title>RtmpPlayerTest</title>
</head>
<body>
<video id="example_video" class="video-js vjs-default-skin vjs-big-play-centered" controls preload="auto" width="1024" height="768"
poster=""
data-setup='{ "html5" : { "nativeTextTracks" : false } }'>>
<source src="rtmp://live.hkstv.hk.lxdns.com/live/hks" type="rtmp/flv" />
<!-- <source src="http://live.hkstv.hk.lxdns.com/live/hks/playlist.m3u8" type="application/x-mpegURL" /> -->
<div id="danmu">
</div>
<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 id="danmu" >
</div>
<div class="text-center ctr">
<br>
<button type="button" onclick="resumer() ">彈幕開始/繼續</button>
<button type="button" onclick="pauser()">彈幕暫停</button>
顯示彈幕:<input type='checkbox' checked='checked' id='ishide' value='is' onchange='changehide()'>
彈幕透明度:
<input type="range" name="op" id="op" onchange="op()" value="100"> <br>
當前彈幕執行時間(分秒):<span id="time"></span>
設定當前彈幕時間(分秒): <input type="text" id="set_time" max=20/>
<button type="button" onclick="settime()">設定</button>
<br>
發彈幕:
<select name="color" id="color">
<option value="white">白色</option>
<option value="red">紅色</option>
<option value="green">綠色</option>
<option value="blue">藍色</option>
<option value="yellow">黃色</option>
</select>
<select name="size" id="text_size">
<option value="1">大文字</option>
<option value="0">小文字</option>
</select>
<select name="position" id="position">
<option value="0">滾動</option>
<option value="1">頂端</option>
<option value="2">底端</option>
</select>
<input type="textarea" id="text" max=300/>
<button type="button" onclick="send()">傳送</button>
</div>
</body>
<script type="text/javascript">
var player = videojs('example_video');
</script>
<script>
(function () {
$("#danmu").danmu({
// left:$("#danmuarea").offset().left,
// top:$("#danmuarea").offset().top,
// height: 445,
// width: 800,
left: 0,
top: 0,
height: "100%",
width: "100%",
zindex: 100,
speed: 30000,
opacity: 1,
font_size_small: 16,
font_size_big: 24,
top_botton_danmu_time: 6000
}
);
})(jQuery);
query();
timedCount();
var first = true;
function timedCount() {
$("#time").text($('#danmu').data("nowtime"));
t = setTimeout("timedCount()", 50)
}
function starter() {
$('#danmu').danmu('danmu_start');
}
function pauser() {
$('#danmu').danmu('danmu_pause');
}
function resumer() {
$('#danmu').danmu('danmu_resume');
}
function stoper() {
$('#danmu').danmu('danmu_stop');
}
function getime() {
alert($('#danmu').data("nowtime"));
}
function getpaused() {
alert($('#danmu').data("paused"));
}
function add() {
var newd =
{"text": "new2", "color": "green", "size": "1", "position": "0", "time": 60};
$('#danmu').danmu("add_danmu", newd);
}
function insert() {
var newd = {"text": "new2", "color": "green", "size": "1", "position": "0", "time": 50};
str_newd = JSON.stringify(newd);
$.post("stone.php", {danmu: str_newd}, function (data, status) {
alert(data)
});
}
function query() {
$.get("query.php", function (data, status) {
var danmu_from_sql = eval(data);
for (var i = 0; i < danmu_from_sql.length; i++) {
var danmu_ls = eval('(' + danmu_from_sql[i] + ')');
$('#danmu').danmu("add_danmu", danmu_ls);
}
});
}
function send() {
var text = document.getElementById('text').value;
var color = document.getElementById('color').value;
var position = document.getElementById('position').value;
var time = $('#danmu').data("nowtime") + 5;
var size = document.getElementById('text_size').value;
var text_obj = '{ "text":"' + text + '","color":"' + color + '","size":"' + size + '","position":"' + position + '","time":' + time + '}';
$.post("stone.php", {danmu: text_obj});
var text_obj = '{ "text":"' + text + '","color":"' + color + '","size":"' + size + '","position":"' + position + '","time":' + time + ',"isnew":""}';
var new_obj = eval('(' + text_obj + ')');
$('#danmu').danmu("add_danmu", new_obj);
document.getElementById('text').value = '';
}
function op() {
var op = document.getElementById('op').value;
$('#danmu').data("opacity", op);
}
function changehide() {
var op = document.getElementById('op').value;
op = op / 100;
if (document.getElementById("ishide").checked) {
jQuery('#danmu').data("opacity", op);
jQuery(".flying").css({
"opacity": op
});
} else {
jQuery('#danmu').data("opacity", 0);
jQuery(".flying").css({
"opacity": 0
});
}
}
function settime() {
var t = document.getElementById("set_time").value;
t = parseInt(t)
console.log(t)
$('#danmu').data("nowtime", t);
}
</script>
</html>
plugin
相關文章
- GitLab使用自定義埠Gitlab
- MySQL使用之五_自定義函式和自定義過程MySql函式
- 使用 FVWM 自定義 Linux 桌面Linux
- jquery自定義事件的使用jQuery事件
- Facade 門面自定義使用
- 使用 Dockerfile 自定義 Nginx 映象DockerNginx
- 聊聊自定義SPI如何使用自定義標籤注入到spring容器中Spring
- 小程式自定義元件的使用元件
- draw.io 使用自定義字型自定義字型
- Angular 自定義管道 pipes 的使用Angular
- 在Flutter中使用自定義IconFlutter
- KingbaseES 自定義運算子使用示例
- springBoot自定義註解的使用Spring Boot
- Service或自定義物件使用LiveData物件LiveData
- 使用 voyager 如何自定義,使用者表
- Flutter自定義Widget和使用方法Flutter
- 如何使用小程式自定義元件功能元件
- Android自定義View之Canvas的使用AndroidViewCanvas
- Avalonia的自定義使用者元件元件
- 使用 Flutter 自定義一個 ProgressBar - IntervalProgressBarFlutter
- Vue 自定義元件使用 v-modelVue元件
- Angular rxjs裡自定義operator的使用AngularJS
- Vue 自定義元件directive使用總結Vue元件
- keycloak~使用自定義的註冊頁
- 使用stub快速生成自定義檔案
- 使用 CSS 自定義屬性(變數)CSS變數
- springboot如何使用自定義配置檔案Spring Boot
- matlab自定義函式建立與使用Matlab函式
- vue3.0 中使用video.js 開啟立即播放 方案VueIDEJS
- 使用微信js-sdk自定義分享JS
- [使用技巧] GitHub pages 自定義域名配置 httpsGithubHTTP
- Mybatis使用小技巧-自定義結果集MyBatis
- uni-app使用小程式自定義元件APP元件
- 使用ADD_CUSTOM_COMMAND 新增自定義命令
- MyBatis使用自定義TypeHandler轉換型別MyBatis型別
- 【自定義使用者控制元件】CNMButton控制元件
- Java 自定義註解及使用場景Java
- PhpCms自定義欄位的使用說明PHP
- Java中使用lambda表示式自定義排序Java排序