130 lines
4.2 KiB
HTML
130 lines
4.2 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, shrink-to-fit=no">
|
||
<title>腾讯云视频点播示例-动态加载、隐藏、销毁播放器</title>
|
||
<!-- 引入播放器 css 文件 -->
|
||
<link href="//imgcache.qq.com/open/qcloud/video/tcplayer/tcplayer.min.css" rel="stylesheet">
|
||
<!-- 如需在IE8、9浏览器中初始化播放器,浏览器需支持Flash并在页面中引入 -->
|
||
<!--[if lt IE 9]>
|
||
<script src="//imgcache.qq.com/open/qcloud/video/tcplayer/ie8/videojs-ie8.js"></script>
|
||
<![endif]-->
|
||
<!-- 如果需要在 Chrome Firefox 等现代浏览器中通过H5播放hls,需要引入 hls.js -->
|
||
<script src="//imgcache.qq.com/open/qcloud/video/tcplayer/libs/hls.min.0.12.4.js"></script>
|
||
<!-- 引入播放器 js 文件 -->
|
||
<script src="//imgcache.qq.com/open/qcloud/video/tcplayer/tcplayer.v4.min.js"></script>
|
||
<!-- 示例 CSS 样式可自行删除 -->
|
||
<style>
|
||
html,body{
|
||
margin: 0;
|
||
padding: 0;
|
||
}
|
||
.tcplayer {
|
||
margin: 0 auto;
|
||
}
|
||
@media screen and (max-width: 640px) {
|
||
#player-container-id {
|
||
width: 100%;
|
||
height: 270px;
|
||
}
|
||
}
|
||
/* 设置logo在高分屏的显示样式 */
|
||
@media only screen and (min-device-pixel-ratio: 2), only screen and (-webkit-min-device-pixel-ratio: 2) {
|
||
.tcp-logo-img {
|
||
width: 50%;
|
||
}
|
||
}
|
||
.button-container{
|
||
text-align: center;
|
||
}
|
||
|
||
#video-container-id{
|
||
display:none;
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<!-- 设置播放器容器 -->
|
||
<div id="video-container"/></div>
|
||
<br>
|
||
<div class="button-container">
|
||
<button onclick="loadVideo()">加载</button>
|
||
<button onclick="showVideo()">隐藏/显示</button>
|
||
<button onclick="disposeVideo()">销毁</button>
|
||
</div>
|
||
<!--
|
||
注意事项:
|
||
* 播放器容器必须为 video 标签
|
||
* player-container-id 为播放器容器的ID,可自行设置
|
||
* 播放器区域的尺寸请按需设置,建议通过 css 进行设置,通过css可实现容器自适应等效果
|
||
* playsinline webkit-playsinline 这几个属性是为了在标准移动端浏览器不劫持视频播放的情况下实现行内播放,此处仅作示例,请按需使用
|
||
* 设置 x5-playsinline 属性会使用 X5 UI 的播放器
|
||
-->
|
||
<script>
|
||
var a,b;
|
||
//动态加载播放器
|
||
function loadVideo() {
|
||
var container = document.getElementById('video-container');
|
||
var getvideo = document.getElementById("player-container-id");
|
||
if(getvideo==null){
|
||
container.innerHTML = '<video id="player-container-id" preload="auto" width="640" height="360" playsinline webkit-playsinline>';
|
||
player = TCPlayer('player-container-id', { // player-container-id 为播放器容器ID,必须与html中一致
|
||
fileID: '7447398157015849771', // 请传入需要播放的视频filID 必须
|
||
appID: '1256993030', // 请传入点播账号的appID 必须
|
||
});
|
||
// 监听play的次数,根据次数判断用户点击显示/隐藏按钮时是否调用play()或pause
|
||
a=0;
|
||
player.on('play', function(){
|
||
a+=1;
|
||
});
|
||
// 监听pause次数,用法同上
|
||
b=0;
|
||
player.on('pause', function(){
|
||
b+=1;
|
||
});
|
||
}else{
|
||
alert('播放器已经初始化过了。');
|
||
}
|
||
};
|
||
|
||
//隐藏/显示播放器
|
||
function showVideo(){
|
||
var container = document.getElementById('video-container');
|
||
var getvideo = document.getElementById("player-container-id");
|
||
var videoheight = container.clientHeight;
|
||
if(getvideo==null){
|
||
alert("播放器还没有初始化。");
|
||
}else if(videoheight>0){
|
||
getvideo.style.display='none';
|
||
// 判断隐藏前是否有点击播放以及暂停
|
||
if(a>0 && a==b){
|
||
a+=1000;
|
||
}else{
|
||
player.pause();
|
||
}
|
||
}else if(videoheight<=0){
|
||
getvideo.style.display='block';
|
||
// 判断显示前是否有点击播放以及是用户主观暂停还是隐藏时调用的暂停
|
||
if(a>0 && a>1000 ){
|
||
a-=1000;
|
||
}else if(a>0 && a==b){
|
||
player.play();
|
||
}
|
||
}
|
||
};
|
||
|
||
//销毁播放器
|
||
function disposeVideo(){
|
||
var getvideo = document.getElementById("player-container-id");
|
||
if(getvideo==null){
|
||
alert("播放器还没有初始化。");
|
||
}else{
|
||
player.dispose();
|
||
}
|
||
};
|
||
</script>
|
||
</body>
|
||
</html>
|