IT168 有一篇关于Jquery制作网页播放器的文章(),这里引用了他的Div+css表现形式,再此表示感谢。不过他那个是用ezSQL连接数据库,并且播放音乐是随机从数据库中选择一首。这对于网站开发来说是不够的,在下不才,稍做更改,以实用于程序系统。
相对于原系统主要做了以下更改:
1、数据库访问采用经典的php+mysql连接方式;
2、音乐播放采用按需播放,通过网页地址动态获取音乐id。
演示地址:
主要代码如下:
play.php:
此代码中加入了js动态获取url中数据的函数和jquery.get方法,也是更改关键所在。
- <title>Demo : jPlayer as an audio player</title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <link href="skin/jplayer.blue.monday.css" rel="stylesheet" type="text/css" />
- <script type="text/javascript" src=""></script>
- <script type="text/javascript" src="js/jquery.jplayer.min.js"></script>
- <script type="text/javascript">
- //<![CDATA[
- $(document).ready(function(){
- //js获取url中id
- function GetRequest() {
- var url = location.search; //获取url中"?"符后的字串
- var theRequest = new Object();
- if (url.indexOf("?") != -1) {
- var str = url.substr(1);
- strs = str.split("&");
- for(var i = 0; i < strs.length; i ++) {
- theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
- }
- }
- return theRequest;
- }
- var Request = new Object();
- Request = GetRequest();
- var id;
- id = Request['id'];
- //end
- $.get("getsong.php?id="+id);
- $("#jquery_jplayer_1").jPlayer({
- ready: function () {
- var data = $.ajax({
- url: "getsong.php?id="+id,
- async: false
- }).responseText;
- var string = data.split('|');
- $(this).jPlayer("setMedia", {
- mp3: string[0]
- }).jPlayer("play");
- $('#singer').html(string[1]);
- $('#songname').html(string[2]);
- },
- ended: function (event) {
- var data = $.ajax({
- url: "getsong.php?id="+id,
- async: false
- }).responseText;
- var string = data.split('|');
- $(this).jPlayer("setMedia", {
- mp3: string[0]
- }).jPlayer("play");
- $('#singer').html(string[1]);
- $('#songname').html(string[2]);
- },
- swfPath: "js",
- supplied: "mp3"
- });
- });
- //]]>
- </script>
- <div id="jquery_jplayer_1" class="jp-jplayer"></div>
- <div class="jp-audio">
- <div class="jp-type-single">
- <div id="jp_interface_1" class="jp-interface">
- <ul class="jp-controls">
- <li><a href="#" class="jp-play" tabindex="1">play</a></li>
- <li><a href="#" class="jp-pause" tabindex="1">pause</a></li>
- <li><a href="#" class="jp-stop" tabindex="1">stop</a></li>
- <li><a href="#" class="jp-mute" tabindex="1">mute</a></li>
- <li><a href="#" class="jp-unmute" tabindex="1">unmute</a></li>
- </ul>
- <div class="jp-progress">
- <div class="jp-seek-bar">
- <div class="jp-play-bar"></div>
- </div>
- </div>
- <div class="jp-volume-bar">
- <div class="jp-volume-bar-value"></div>
- </div>
- <div class="jp-current-time"></div>
- <div class="jp-duration"></div>
- </div>
- <div id="jp_playlist_1" class="jp-playlist">
- <ul>
- <li><strong id="singer">Singer</strong> - <span id="songname">Song name</span></li>
- </ul>
- </div>
- </div>
- </div>
getsong.php:
- <?php
- include_once("config/conn.php");
- $id=$_GET['id'];
- $sql_song = "SELECT * FROM h_song WHERE id='".$id."'";
- //$sql_song = "SELECT * FROM h_song ORDER BY RAND() LIMIT 1";
- $result_song = mysql_query($sql_song);
- $row_song = @mysql_fetch_array($result_song);
- $singer = $row_song[singer];
- $songname = $row_song[songname];
- $url = $row_song[downloadurl];
- $separator = '|';
- echo $url.$separator.$singer.$separator.$songname;
- ?>
数据库:
- create table h_song(
- id int not null auto_increment primary key,
- classid int not null,
- songname varchar(100) not null,
- singer varchar(50) not null,
- downloadurl varchar(100) null,
- downamound int null,
- recommend int null,
- uploadid int not null,
- uptime date not null,
- hit int null,
- remark text null
- );