JPlayer-MP3播放器(带播放列表)

第一篇随笔,写的不好的地方,各位不要见笑。其他的也不多说了,下面是我在工作中用的一个基于JQuery开源的插件,官方地址:http://www.jplayer.org/。先看下要实现的效果图:

首先引用需要的css和js文件:

1 <link href="Styles/jplayer.blue.monday.css" rel="stylesheet" type="text/css" />
2 <script src="Scripts/jquery-1.8.2.js" type="text/javascript"></script>
3 <script src="Scripts/jquery.jplayer.min.js" type="text/javascript"></script>
4 <script src="Scripts/jplayer.playlist.min.js" type="text/javascript"></script>
5 <script src="Scripts/jquery.blockUI.js" type="text/javascript"></script><!--只是模拟加载等待的一个插件-->
View Code

 接下来就是播放器的布局了,原本是英文的,在这里我只翻译了常用的:

 1     <!--播放器设计开始-->
 2     <div style="width: 482px; margin: 0px auto 0px auto;">
 3         <div id="jp_container_N" class="jp-video jp-video-270p">
 4             <div class="jp-type-playlist">
 5                 <div id="jquery_jplayer_N" class="jp-jplayer">
 6                 </div>
 7                 <div class="jp-gui">
 8                     <div class="jp-interface">
 9                         <div class="jp-progress">
10                             <div class="jp-seek-bar">
11                                 <div class="jp-play-bar">
12                                 </div>
13                             </div>
14                         </div>
15                         <div class="jp-current-time">
16                         </div>
17                         <div class="jp-duration">
18                         </div>
19                         <div class="jp-controls-holder">
20                             <ul class="jp-controls">
21                                 <li><a href="javascript:;" class="jp-previous" tabindex="1" title="上一首">上一首</a></li>
22                                 <li><a href="javascript:;" class="jp-play" tabindex="1" title="播放">播放</a></li>
23                                 <li><a href="javascript:;" class="jp-pause" tabindex="1" title="暂停">暂停</a></li>
24                                 <li><a href="javascript:;" class="jp-next" tabindex="1" title="下一首">下一首</a></li>
25                                 <li><a href="javascript:;" class="jp-stop" tabindex="1" title="停止">停止</a></li>
26                                 <li><a href="javascript:;" class="jp-mute" tabindex="1" title="静音">静音</a></li>
27                                 <li><a href="javascript:;" class="jp-unmute" tabindex="1" title="调节音量">调节音量</a></li>
28                                 <li><a href="javascript:;" class="jp-volume-max" tabindex="1" title="最大声音">最大声音</a></li>
29                             </ul>
30                             <div class="jp-volume-bar">
31                                 <div class="jp-volume-bar-value">
32                                 </div>
33                             </div>
34                             <ul class="jp-toggles">
35                                 <li><a href="javascript:;" class="jp-shuffle" tabindex="1" title="shuffle">shuffle</a></li>
36                                 <li><a href="javascript:;" class="jp-shuffle-off" tabindex="1" title="shuffle off">shuffle
37                                     off</a></li>
38                                 <li><a href="javascript:;" class="jp-repeat" tabindex="1" title="单曲循环">单曲循环</a></li>
39                                 <li><a href="javascript:;" class="jp-repeat-off" tabindex="1" title="关闭单曲循环">关闭单曲循环</a></li>
40                             </ul>
41                         </div>
42                         <div class="jp-title">
43                             <ul>
44                                 <li></li>
45                             </ul>
46                         </div>
47                     </div>
48                 </div>
49                 <div class="jp-playlist" style="height: 500px; overflow-y: auto;">
50                     <ul>
51                         <!-- The method Playlist.displayPlaylist() uses this unordered list -->
52                         <li></li>
53                     </ul>
54                     <div id='divButton' style='width: 103px; height: 30px; margin: 230px auto 200px auto;
55                         display: none;'>
56                     </div>
57                 </div>
58                 <div class="jp-no-solution">
59                     <span>Update Required</span> To play the media you will need to either update your
60                     browser to a recent version or update your <a href="http://get.adobe.com/flashplayer/"
61                         target="_blank">Flash plugin</a>.
62                 </div>
63             </div>
64         </div>
65     </div>
66     <!--播放器设计结束-->
View Code

再来看一下运行的效果图:

然后就是将磁盘的歌曲添加到列表中,我是通过ajax请求将本地歌曲库的所有歌曲重新复制一遍到项目的一个存取歌曲的文件夹中。那先看下js代码:

 1     <script type="text/javascript">
 2         var mTitle, mArtist, path = "";
 3         var myPlaylist = new jPlayerPlaylist();
 4         $(function () {
 5             myPlaylist = new jPlayerPlaylist({
 6                 jPlayer: "#jquery_jplayer_N",
 7                 cssSelectorAncestor: "#jp_container_N"
 8             }, [
 9             ], {
10                 playlistOptions: {
11                     enableRemoveControls: true
12                 },
13                 swfPath: "Scripts",
14                 supplied: "mp3",
15                 wmode: "window"
16             });
17             $("#divButton").html("<input type='button' id='btnSearchMusic' value='单击获取歌曲' style='border:1px solid #48C9EF; background:#48C9EF; cursor:pointer; color:#fff; height:30px;' />").show();
18             $("#btnSearchMusic").click(function () {
19                 $.blockUI({ message: '<h2>正在加载歌曲,请稍候。。。</h2>' });
20                 $("#divButton").hide();
21                 myPlaylist.remove();
22                 $.ajax({
23                     url: "play.ashx?action=GetResourceLists",
24                     cache: false,
25                     success: function (data) {
26                         var json = eval("(" + data + ")");
27                         if (json.length > 0) {
28                             for (var i = 0; i < json.length; i++) {
29                                 var fileName = json[i].OriginalPath;
30                                 path = json[i].FullPath;
31                                 var singer = fileName.substring(9, fileName.length);
32                                 mArtist = singer.substring(0, singer.indexOf("-"));
33                                 var song = singer.substring(0, singer.lastIndexOf("."));
34                                 mTitle = song.substring(song.lastIndexOf("-") + 1);
35                                 //console.log(mArtist);
36                                 myPlaylist.add({
37                                     title: mTitle,
38                                     artist: mArtist,
39                                     free: true,
40                                     mp3: "http://localhost:3881/MiniPlay/Resources/" + encodeURI(singer)
41                                 });
42                             }
43                         }
44                     }
45                 });
46                 $.unblockUI();
47             });
48         });
49     </script>
View Code

 下面是ashx(一般处理程序)的关键代码:

 1     //转换json格式
 2     public string FormatJson(int count, List<FileInfo> list)
 3     {
 4         string str = "";
 5         DataContractJsonSerializer json = new DataContractJsonSerializer(list.GetType());
 6         using (MemoryStream ms = new MemoryStream())
 7         {
 8             json.WriteObject(ms, list);
 9             str = Encoding.UTF8.GetString(ms.ToArray());
10         }
11         return str;
12     }
13 
14     //获取所有文件
15     public void GetResourceLists(HttpContext context)
16     {
17         List<FileInfo> files = new List<FileInfo>();
18         if (File.Exists(context.Server.MapPath(@"~/config.ini")))
19         {
20             StreamReader strReader = new StreamReader(context.Server.MapPath(@"~/config.ini"));
21             var path = strReader.ReadToEnd().Substring(5);
22             strReader.Close();
23             var localFileLists = Directory.GetFiles(path);
24             var targetPath = context.Server.MapPath(@"~/Resources");
25             if (!Directory.Exists(targetPath))
26             {
27                 Directory.CreateDirectory(targetPath);
28             }
29             if (localFileLists.Count() > 0)
30             {
31                 foreach (var counts in localFileLists)
32                 {
33                     if (counts.Substring(counts.LastIndexOf(".")) == ".mp3")
34                     {
35                         if (!File.Exists(Path.Combine(targetPath, Path.GetFileName(counts))))   //如果目标存在该文件,不复制
36                         {
37                             File.Copy(counts, Path.Combine(targetPath, Path.GetFileName(counts)));
38                         }
39                         files.Add(new FileInfo(counts));
40                     }
41                 }
42                 context.Response.Write(FormatJson(files.Count(), files));
43             }
44         }
45         else
46         {
47             context.Response.Write("no exist");
48         }
49     }
View Code

 到这里,要实现上面的功能基本已经完成了!

原文地址:http://www.cnblogs.com/JasonLong/p/jplayer-mp3.html

转载于:https://www.cnblogs.com/JasonLong/p/jplayer-mp3.html

要通过 JavaScript 动态更新 jPlayer 播放列表,您需要使用 jPlayer 提供的 API 方法。以下是一个简单的示例: 首先,您需要在 HTML 中设置一个 jPlayer 播放器播放列表。例如: ```html <div id="jp_container_1" class="jp-video"> <div class="jp-type-playlist"> <div class="jp-gui jp-interface"> <div class="jp-controls"> <button class="jp-play"></button> <button class="jp-stop"></button> <button class="jp-previous"></button> <button class="jp-next"></button> </div> <div class="jp-progress"> <div class="jp-seek-bar"> <div class="jp-play-bar"></div> </div> </div> <div class="jp-volume-controls"> <button class="jp-mute"></button> <button class="jp-volume-max"></button> <div class="jp-volume-bar"> <div class="jp-volume-bar-value"></div> </div> </div> </div> <div class="jp-playlist"> <ul> <li></li> </ul> </div> <div class="jp-no-solution"> <span>Update Required</span> To play the media you will need to either update your browser to a recent version or update your <a href="http://get.adobe.com/flashplayer/" target="_blank">Flash plugin</a>. </div> </div> </div> ``` 然后,在 JavaScript 中,您可以使用 jPlayer 提供的方法来添加和删除播放列表项,如下所示: ```js var myPlaylist = new jPlayerPlaylist({ jPlayer: "#jquery_jplayer_1", cssSelectorAncestor: "#jp_container_1" }, [], { playlistOptions: { enableRemoveControls: true }, swfPath: "js", supplied: "webmv, ogv, m4v, oga, mp3", useStateClassSkin: true, autoBlur: false, smoothPlayBar: true, keyEnabled: true, audioFullScreen: true }); // 添加列表项 myPlaylist.add({ title:"Song Title", artist:"Artist Name", mp3:"path/to/song.mp3", ogg:"path/to/song.ogg", poster: "path/to/poster.jpg" }); // 删除列表项 myPlaylist.remove(0); ``` 在上面的代码中,我们首先使用 `new jPlayerPlaylist` 方法创建一个播放列表,然后使用 `add` 方法向列表中添加一个新的条目。我们可以指定标题、艺术家、音频文件路径、海报图像等属性。如果需要删除某个列表项,可以使用 `remove` 方法并指定要删除的索引。 您可以根据需要使用其他 jPlayer API 方法来操作播放器播放列表。更多信息请参见 jPlayer 官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值