DPlayer 是一个支持弹幕的 HTML5 视频播放器。支持 Bilibili 视频和 danmaku,支持HLS,FLV,MPEG DASH,WebTorrent以及其他视频格式,支持截屏、热键、切换清晰度以及字幕等。在使用Dplayer播放视频时有时候,我们可能会用到视频的前置广告视频啥的,或是要先放个自个的品牌宣传视频,然后再进行播放正片。
今天就介绍下吧。
1,引入JQ,这个很重要
2,暂停贴片广告
3,前置视频贴片广告
4,前置HTML贴片广告
5,整合
CSS部分:
- <style type="text/css">
- html,body{height:100%;margin:0;padding:0;overflow:hidden;text-align:center;background:#181818;}
- *{margin:0;border:0;padding:0;text-decoration:none}
- #dplayer{height:100%;}
- .dplayer-logo{max-width: 150px;max-height: 50px;left: auto;right: 5%;top: 5%;}
- #ad{position: absolute;z-index: 99999;width:100%;height:100%}
- .closeclick{display: inline-block;width: 100px;height: 35px;line-height: 35px;text-align: center;font-size: 14px;border-radius: 22px;margin: 0px 10px;color: #2082B3;overflow: hidden;box-shadow: 0px 2px 3px rgba(0,0,0,.3);background: #fff;position: absolute; z-index: 9999;top:20px;right: 35px;}
- .dplayer_nobuy_tips{position: absolute;z-index: 9;color: #fff;font-size: 14px;height: 30px;line-height: 30px;background-color: rgba(0, 0, 0, 0.5);width: 100%;text-align: center;margin: 0 20px;}
- .dplayer_buybox{position: absolute;top: 0;align-items: center;width: 100%;height: 100%;background: rgba(0, 0, 0, 0.8);z-index: 998;color: #fff;text-align: center;display: flex;font-size: 14px;}
- .dplayer_buybox-content{width:100%;height: 100%;background: rgba(0, 0, 0, 0.3);}
- .dplayer_buybox-tipsbtn {background: #f60;padding: 5px 15px;border-radius: 2px;cursor: pointer;color: #fff;margin-top: 10px;display: inline-block;}
- .dplayer_buybox-content>span {margin: 0 5px;color: #f60;}
- .dplayer_buybox-tips {position: absolute;top:50%;left:50%;width:100%;transform:translate(-50%,-100%);text-align: center;color: #fff;font-size: 14px;line-height: 30px;text-align: center;}
- </style>
HTML部分,放在播放 <div id="dplayer"></div> 上方
var dp = new DPlayer({ 上方放入
- <div id="dplayer_nobuybox"><div class="dplayer_nobuy_tips">【付费视频】您可以免费试看20秒,购买后可观看全部。</div></div>
- <div id="ad" >
- <iframe id="player_pread" class="embed-responsive-item" src="https://www.meiweny.cn/d/file/sanwen/tianyuan/2021-08-25/72cb5c72cbdc88fd0c5bebe92badc707.jpg" frameborder="0" scrolling="no" allowtransparency="true" style="position:absolute;z-index:2;top:0px;left:0px;width:100%;height:100%"></iframe>
- <a class="closeclick" href="javascript:;" onclick="closeclick()">跳过<em id="adindextime"></em></a>
- </div>
- <div id="dplayer_buybox" class="dplayer_buybox" style="display:none;">
- <div class="dplayer_buybox-content">
- <div class="dplayer_buybox-tips">该视频需要<span>付费购买</span>才可以观看完整版<br>
- <m class="dplayer_buybox-tipsbtn">购买</m>
- </div>
- </div>
- </div>
var dp = new DPlayer({//省略播放器配置参数});下方放入
- var adpreh_time = "20";//前置广告显示时间
- var shikan_time = "20";//试看时间
- var adpau = 'https://www.meiweny.cn/d/file/sanwen/tianyuan/2021-08-25/72cb5c72cbdc88fd0c5bebe92badc707.jpg';//暂停广告
- function remove(){
- $('#player_pause').remove();
- }
- function pause(){
- $width = $('#dplayer').width()/2;
- $height = $('#dplayer').height()/2;
- $top = $('#dplayer').height()/4;
- $left = $('#dplayer').width()/4;
- $('#dplayer').before('<iframe id="player_pause" class="embed-responsive-item" src="'+ adpau +'" frameborder="0" scrolling="no" allowtransparency="true" style="position:absolute;z-index:2;top:'+$top+'px;left:'+$left+'px;width:'+$width+'px;height:'+$height+'px"></iframe>');
- }
最后附上整合版
- dp.on('timeupdate', function () {
- if (dp.dplayer.currentTime > shikan_time) {//视频时间,单位’秒‘,建议减1秒
- dp.pause();
- dp.dplayer.currentTime = 0;
- $("#dplayer_buybox").show();
- $('#player_pause').remove();
- $('#dplayer_nobuybox').remove();
- }
- });
- dp.on('pause', function () {
- pause();
- })
- dp.on('play', function () {
- remove();
- });
- function closeclick() {
- $('#ad').remove();
- $("#player_predplayer").show();
- dp.play();
- }
- function clickclose() {
- $('#ad').remove()
- $("#player_predplayer").show();
- dp.play();
- }
- $(function() {
- $("#player_pread").css('width',$('#ad').width()/1.3);
- $("#player_pread").css('height',$('#ad').height()/1.3);
- $("#player_pread").css('top',$('#ad').height()/9);
- $("#player_pread").css('left',$('#ad').width()/9);
- var i = adpreh_time;//前置HTML广告时间
- setTimeout(function() {
- closeclick();
- }, i * 1000);
- after();
- function after() {
- if (i == 0) {
- $("#adindextime").empty().append(i);
- setTimeout(function() {
- after();
- }, 1000);
- }
- else {
- $("#adindextime").empty().append(i);
- i = i - 1;
- setTimeout(function() {
- after();
- }, 1000);
- }
- }
- });
DPlayer 是一个支持弹幕的 HTML5 视频播放器。支持 Bilibili 视频和 danmaku,支持HLS,FLV,MPEG DASH,WebTorrent以及其他视频格式,支持截屏、热键、切换清晰度以及字幕等。更多相关二次开发敬请期待!
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta name="referrer" content="no-referrer" />
- <meta http-equiv="Access-Control-Allow-Origin" content="*" />
- <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
- <title>播放器</title>
- <link href="https://www.meiweny.cn/e/m/DPlayer/DPlayer.min.css" rel="stylesheet">
- <script type="text/javascript" src="https://www.meiweny.cn/e/m/DPlayer/flv.js"></script>
- <script type="text/javascript" src="https://www.meiweny.cn/e/m/DPlayer/hls.js"></script>
- <script src="https://cdn.jsdelivr.net/npm/cdnbye@latest"></script>
- <script src="https://www.meiweny.cn/e/m/DPlayer/DPlayer.min.js"></script>
- <script type="text/javascript" src="https://www.meiweny.cn/e/m/DPlayer/md5.min.js"></script>
- <script type="text/javascript" src="https://www.meiweny.cn/e/m//dasang/js/jquery.js"></script>
- <style type="text/css">
- html,body{height:100%;margin:0;padding:0;overflow:hidden;text-align:center;background:#181818;}
- *{margin:0;border:0;padding:0;text-decoration:none}
- #dplayer{height:100%;}
- .dplayer-logo{max-width: 150px;max-height: 50px;left: auto;right: 5%;top: 5%;}
- #ad{position: absolute;z-index: 99999;width:100%;height:100%}
- .closeclick{display: inline-block;width: 100px;height: 35px;line-height: 35px;text-align: center;font-size: 14px;border-radius: 22px;margin: 0px 10px;color: #2082B3;overflow: hidden;box-shadow: 0px 2px 3px rgba(0,0,0,.3);background: #fff;position: absolute; z-index: 9999;top:20px;right: 35px;}
- .dplayer_nobuy_tips{position: absolute;z-index: 9;color: #fff;font-size: 14px;height: 30px;line-height: 30px;background-color: rgba(0, 0, 0, 0.5);width: 100%;text-align: center;margin: 0 20px;}
- .dplayer_buybox{position: absolute;top: 0;align-items: center;width: 100%;height: 100%;background: rgba(0, 0, 0, 0.8);z-index: 998;color: #fff;text-align: center;display: flex;font-size: 14px;}
- .dplayer_buybox-content{width:100%;height: 100%;background: rgba(0, 0, 0, 0.3);}
- .dplayer_buybox-tipsbtn {background: #f60;padding: 5px 15px;border-radius: 2px;cursor: pointer;color: #fff;margin-top: 10px;display: inline-block;}
- .dplayer_buybox-content>span {margin: 0 5px;color: #f60;}
- .dplayer_buybox-tips {position: absolute;top:50%;left:50%;width:100%;transform:translate(-50%,-100%);text-align: center;color: #fff;font-size: 14px;line-height: 30px;text-align: center;}
- </style>
- <div id="dplayer_nobuybox"><div class="dplayer_nobuy_tips">【付费视频】您可以免费试看20秒,购买后可观看全部。</div></div>
- <div id="ad" >
- <iframe id="player_pread" class="embed-responsive-item" src="https://www.meiweny.cn/d/file/sanwen/tianyuan/2021-08-25/72cb5c72cbdc88fd0c5bebe92badc707.jpg" frameborder="0" scrolling="no" allowtransparency="true" style="position:absolute;z-index:2;top:0px;left:0px;width:100%;height:100%"></iframe>
- <a class="closeclick" href="javascript:;" onclick="closeclick()">跳过<em id="adindextime"></em></a>
- </div>
- <div id="dplayer_buybox" class="dplayer_buybox" style="display:none;">
- <div class="dplayer_buybox-content">
- <div class="dplayer_buybox-tips">该视频需要<span>付费购买</span>才可以观看完整版<br>
- <m class="dplayer_buybox-tipsbtn">购买</m>
- </div>
- </div>
- </div>
- <div id="dplayer"></div>
- <script type="text/javascript">
- var adpreh_time = "20";//前置广告显示时间
- var shikan_time = "20";//试看时间
- var adpau = 'https://www.meiweny.cn/d/file/sanwen/tianyuan/2021-08-25/72cb5c72cbdc88fd0c5bebe92badc707.jpg';//暂停广告
- function remove(){
- $('#player_pause').remove();
- }
- function pause(){
- $width = $('#dplayer').width()/2;
- $height = $('#dplayer').height()/2;
- $top = $('#dplayer').height()/4;
- $left = $('#dplayer').width()/4;
- $('#dplayer').before('<iframe id="player_pause" class="embed-responsive-item" src="'+ adpau +'" frameborder="0" scrolling="no" allowtransparency="true" style="position:absolute;z-index:2;top:'+$top+'px;left:'+$left+'px;width:'+$width+'px;height:'+$height+'px"></iframe>');
- }
- var dp = new DPlayer({
- container: document.getElementById('dplayer'),
- //screenshot: true,//截屏
- theme: '#4C8FE8',//进度条颜色
- volume: 1.0,//音量
- loop: true,//循环
- logo: '',//LOGO
- //autoplay: true,//自动播放
- video: {
- quality: [{
- name: '高清',
- url:'https://vod.cdbaiyou.com/20210721/km18yXmO/1059kb/hls/index.m3u8',
- //type: 'normal'
- }],
- pic: 'https://ww3.sinaimg.cn/large/0060lm7Tly1fke6b991zzg30jq0dwaar.gif',
- defaultQuality: 0
- },
- });
- dp.on('timeupdate', function () {
- if (dp.dplayer.currentTime > shikan_time) {//视频时间,单位’秒‘,建议减1秒
- dp.pause();
- dp.dplayer.currentTime = 0;
- $("#dplayer_buybox").show();
- $('#player_pause').remove();
- $('#dplayer_nobuybox').remove();
- }
- });
- dp.on('pause', function () {
- pause();
- })
- dp.on('play', function () {
- remove();
- });
- function closeclick() {
- $('#ad').remove();
- $("#player_predplayer").show();
- dp.play();
- }
- function clickclose() {
- $('#ad').remove()
- $("#player_predplayer").show();
- dp.play();
- }
- $(function() {
- $("#player_pread").css('width',$('#ad').width()/1.3);
- $("#player_pread").css('height',$('#ad').height()/1.3);
- $("#player_pread").css('top',$('#ad').height()/9);
- $("#player_pread").css('left',$('#ad').width()/9);
- var i = adpreh_time;//前置HTML广告时间
- setTimeout(function() {
- closeclick();
- }, i * 1000);
- after();
- function after() {
- if (i == 0) {
- $("#adindextime").empty().append(i);
- setTimeout(function() {
- after();
- }, 1000);
- }
- else {
- $("#adindextime").empty().append(i);
- i = i - 1;
- setTimeout(function() {
- after();
- }, 1000);
- }
- }
- });
- </script>
- </body>
- </html>