首页 > 代码库 > 基于jPlayer的三分屏制作

基于jPlayer的三分屏制作

三分屏,这里的三分屏只是在一个播放器里同时播放三个视频,但是要求只有一个控制面板同时控制它们,要求它们共享一个时间轨道。这次只是简单的模拟了一下功能,并没有深入的研究。

首先,需要下载jPlayer,jPlayer是一个JavaScript写的完全免费和开源的jQuery多媒体库插件,我觉得他最大的好处就是兼容性,并且页面也简洁大方,个人比较喜欢。jPlayer可到其官网下载最新版本(http://www.jplayer.cn)。并且官网有开发文档和Demo,所以还是比较容易上手的。

关于简单的视频播放器和音频播放器这里不再赘述了,官网的Demo挺好的,这里直奔主题。

首先当然是引入jPlayer相关文件。

首先是css文件夹,这里为了方便,某些文件也放到css文件夹里了:

技术分享

然后是js文件夹:

技术分享

因为jPlayer是在jQuery的插件,所以jquery的包自然也就少不了了。

新建一个test.jsp页面,下面是代码:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 
 2     pageEncoding="UTF-8"%>
 3     
 4 <!DOCTYPE html>
 5 <html>
 6     <head>
 7         <meta charset="utf-8" />
 8         <title>JPlayer Test</title>
 9         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
10         <!-- 引入相关文件 -->
11         <link href="http://www.mamicode.com/css/jplayer.blue.monday.css" rel="stylesheet" type="text/css" />
12         <script type="text/javascript" src="http://www.mamicode.com/js/jquery.min.js"></script>
13         <script type="text/javascript" src="http://www.mamicode.com/css/jquery.jplayer.min.js"></script>
14         <script type="text/javascript" src="http://www.mamicode.com/js/jplayer.playlist.min.js"></script>
15         <script type="text/javascript" src="http://www.mamicode.com/test.js"></script>
16     </head>
17 <body>
18 <div class="wrapper">
19     <div class="left-wrapper" style="float: left; width: 320px">
20         <div id="jp_container_2" class="jp-video jp-video-360p">
21             <div class="jp-type-single">
22                 <!-- 左上的屏幕 -->
23                 <div ondblclick="fullScreen()" id="jquery_jplayer_2" class="jp-jplayer"></div> 
24                 <!-- 左下的屏幕 -->
25                 <div ondblclick="fullScreen()" id="jquery_jplayer_3" class="jp-jplayer"></div>
26             </div>
27         </div>   
28     </div>
29     <div class="right-wrapper" style="float: left;">
30         <div id="jp_container_1" class="jp-video jp-video-360p" style="border-bottom: none; border-left: none;">
31             <div class="jp-type-single">
32                 <div ondblclick="fullScreen()" id="jquery_jplayer_1" class="jp-jplayer"></div>
33                 <div class="jp-gui" align="left">
34                     <div class="jp-video-play" style="margin-left: -160px">
35                         <button class="jp-video-play-icon">play</button>
36                     </div>
37                     
38                     <!-- 控制面板 -->
39                     <!-- width: 959px; margin-left: -320px;  -->
40                     <div id="controller" class="jp-interface" style="border: 1px solid #009be3;">
41                         <div class="jp-progress">
42                             <div class="jp-seek-bar">
43                                 <div class="jp-play-bar"></div>
44                             </div>
45                         </div>
46                         <div class="jp-current-time">&nbsp;</div>
47                         <div class="jp-duration">&nbsp;</div>
48                         <div class="jp-controls-holder">
49                             <div class="jp-controls">
50                                 <button class="jp-play">play</button>
51                                 <button class="jp-stop">stop</button>
52                             </div>
53                             <div class="jp-volume-controls">
54                                 <button class="jp-mute">mute</button>
55                                 <button class="jp-volume-max">max volume</button>
56                                 <div class="jp-volume-bar">
57                                     <div class="jp-volume-bar-value"></div>
58                                 </div>
59                             </div>
60                             <div class="jp-toggles">
61                                 <button class="jp-full-screen">full screen</button>
62                             </div>
63                         </div>
64                         <div class="jp-details">
65                             <div class="jp-title" aria-label="title">&nbsp;</div>
66                         </div>
67                     </div>
68                 </div>
69             </div>
70         </div>
71     </div>
72 </div>
73 </body>
74 
75 </html>

 新建一个test.js文件,下面是代码:

  1 var flag = false;
  2 var isFull = false;
  3 
  4 $(document).ready(function() {
  5     // 实例化第一个视频
  6     $("#jquery_jplayer_1").jPlayer({
  7         ready: function() {
  8             $(this).jPlayer("setMedia", {
  9                 title: "This is video Title",
 10                 m4v: "http://www.jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v",
 11                 /* m4v: "video/003.webm", */
 12                 poster: "http://www.jplayer.org/video/poster/Big_Buck_Bunny_Trailer_480x270.png"
 13             });
 14         },
 15         swfPath: "../../dist/jplayer",
 16         supplied: "m4v",
 17         size: {
 18             width: "640px",
 19             height: "360px",
 20             cssClass: "jp-video-360p"
 21         },
 22         useStateClassSkin: true,
 23         autoBlur: false,
 24         smoothPlayBar: true,
 25         keyEnabled: true,
 26         remainingDuration: true,
 27         toggleDuration: true
 28     });
 29     
 30     // 实例化第二个视频
 31     $("#jquery_jplayer_2").jPlayer({
 32         ready: function() {
 33             $(this).jPlayer("setMedia", {
 34                 title: "This is video Title",
 35                 m4v: "http://www.jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v",
 36                 /* m4v: "video/003.webm", */
 37                 poster: "http://www.jplayer.org/video/poster/Big_Buck_Bunny_Trailer_480x270.png"
 38             });
 39         },
 40         swfPath: "../../dist/jplayer",
 41         supplied: "m4v",
 42         size: {
 43             width: "320px",
 44             height: "180px",
 45             cssClass: "jp-video-360p"
 46         },
 47         useStateClassSkin: true,
 48         autoBlur: false,
 49         smoothPlayBar: true,
 50         keyEnabled: true,
 51         remainingDuration: true,
 52         toggleDuration: true
 53     });
 54     
 55     // 实例化第三个视频
 56     $("#jquery_jplayer_3").jPlayer({
 57         ready: function() {
 58             $(this).jPlayer("setMedia", {
 59                 title: "This is video Title",
 60                 m4v: "http://www.jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v",
 61                 /* m4v: "video/003.webm", */
 62                 poster: "http://www.jplayer.org/video/poster/Big_Buck_Bunny_Trailer_480x270.png"
 63             });
 64         },
 65         swfPath: "../../dist/jplayer",
 66         supplied: "m4v",
 67         size: {
 68             width: "320px",
 69             height: "180px",
 70             cssClass: "jp-video-360p"
 71         },
 72         useStateClassSkin: true,
 73         autoBlur: false,
 74         smoothPlayBar: true,
 75         keyEnabled: true,
 76         remainingDuration: true,
 77         toggleDuration: true
 78     });
 79 
 80     // 绑定初始化事件,用于在页面加载的时候初始化控制条的位置
 81     $("#jquery_jplayer_1").bind($.jPlayer.event.ready, function(event) {
 82         $("#controller").css({
 83             width: "959px", 
 84             margin: "0px 0px 0px -320px"
 85         });
 86     });
 87     
 88     // 绑定页面大小变更事件,用于页面大小变化的时候,设置控制条的位置
 89     $("#jquery_jplayer_1").bind($.jPlayer.event.resize, function(event) {
 90         if (!isFull) {
 91             $("#controller").css({
 92                 width: "100%", 
 93                 margin: "0"
 94             });
 95             isFull = true;
 96         } else {
 97             $("#controller").css({
 98                 width: "959px", 
 99                 margin: "0px 0px 0px -320px"
100             });
101             isFull = false;
102         }
103     });
104     
105     // 绑定单击事件,用于单击屏幕控制视频的播放与暂停
106     $(‘#jquery_jplayer_1, #jquery_jplayer_2, #jquery_jplayer_3‘).bind("click", function(){
107         var $jplayer1 = $(‘#jquery_jplayer_1‘);
108         var $jplayer2 = $(‘#jquery_jplayer_2‘);
109         var $jplayer3 = $(‘#jquery_jplayer_3‘);
110         var status = $jplayer1.data("jPlayer").status;
111         if (status.paused) {
112             $jplayer1.jPlayer("play");
113             $jplayer2.jPlayer("play");
114             $jplayer3.jPlayer("play");
115         } else {
116             $jplayer1.jPlayer("pause");
117             $jplayer2.jPlayer("pause");
118             $jplayer3.jPlayer("pause");
119         }
120     });
121 });
122 
123 // 双击屏幕时,进入全屏状态
124 function fullScreen() {
125     $("#jquery_jplayer_1").jPlayer("option", "fullScreen", true); 
126 }

以上就是全部信息,需要注意的是,进入全屏状态的时候,只有主屏幕进入全屏,其它两个小的屏幕并不会进入全屏状态。

基于jPlayer的三分屏制作