本文共 5921 字,大约阅读时间需要 19 分钟。
在网上找到2段PHP音乐播放程序,有空再完善一下,写个功能比较齐全的
测试运行环境:WindowsXP+APMServ 5.2.6
在CoreAmp PHP5.3.1上面运行要么无结果,要么运行不正常。-//-
1。这个例子功能比较强大,将php文件上传到含有mp3歌曲的目录后运行,程序会自动寻找mp3歌曲并生成有歌手专辑等信息能下载能试听的静态页面。
/*****************************************************************
直接读取mp3歌曲的歌手专辑歌词等信息,上传到含有mp3歌曲的目录后运行
程序会自动寻找mp3歌曲并生成静态页面。
******************************************************************/
// Config
$sitename = "歌曲信息列表";
$copyright = "";
//End config
class readid3v23 {
/**
* Mp3 ID3V23x读取类
*/
var $filename = "曹操.mp3";
var $fp;
var $tag;
var $major_version;
var $revision_number;
var $flags;
var $total_size;
var $title;
var $artist;
var $album;
var $year;
var $comments;
var $ulack;
var $genre;
var $lyric;
function getfilename($file){
}
function openfile(){
$this->fp = fopen($this->filename, "rb") or die("打开文件".$this->filename."失败!");
}
function readid3v23($file){
$this->filename = $file or die("文件不能为空");
$this->openfile();
if(($this->tag = fread($this->fp, 3)) != 'ID3' ) return False;
$this->major_version = dechex(bin2hex(fread($this->fp, 1)));
$this->revision_number = dechex(bin2hex(fread($this->fp, 1)));
$this->flags = fread($this->fp, 1);
$this->total_size = ord(fread($this->fp, 1))*0x200000 + ord(fread($this->fp, 1))*0x400 + ord(fread($this->fp, 1))*0x80 + ord(fread($this->fp, 1));
for ($i=0;$itotal_size;){
$i+=10;
$FrameID = fread($this->fp, 4);
$Fsize = ord(fread($this->fp, 1))*0x100000000 + ord(fread($this->fp, 1))*0x10000 + ord(fread($this->fp, 1))*0x100 + ord(fread($this->fp, 1));
if($Fsize == 0) break;
$Fflags = fread($this->fp,2);
$Ftext = fread($this->fp,$Fsize);
$i+=$Fsize;
if($FrameID == 'TIT2'){
$this->title = $Ftext;
}
if($FrameID == 'TPE1'){
$this->artist = $Ftext;
}
if($FrameID == 'TALB'){
$this->album = $Ftext;
}
if($FrameID == 'TYER'){
$this->year = $Ftext;
}
if($FrameID == 'COMM'){
$this->comments = $Ftext;
}
if($FrameID == 'USLT'){
$this->lyric = $Ftext;
}
}
return ulue;
register_shuliown_function(array(&$this, '_readid3v23'));
}
function _readid3v23() {
if ($this->fp) {
fclose($this->fp);
}
$this->fp = 0;
}
}
class readid3v1x {
var $file_name;
var $major_version = '1';
var $revision_number = '0';
var $tag;
var $title;
var $artist;
var $album;
var $year;
var $comments;
var $lyric = "没有歌词";
function readid3v1x($inputfile) {
$this->file_name = $inputfile;
$file = fopen($this->file_name, "rb");
if($file == false) {
return false;
}
else {
fseek($file, -128, SEEK_END);
$this->tag = fgets($file, 4);
if($this->tag == 'TAG') {
$this->title = fgets($file, 31);
$this->artist = fgets($file, 31);
$this->album = fgets($file, 31);
$this->year = fgets($file, 5);
$this->comments = fgets($file, 29);
return ulue;
}
else {
$this->tag = 'Not found tag informations!';
return ulue;
}
}
fclose($file);
}
}
$top="
"-//W3C//Dli XHTML 1.0 ulansitional//EN/" /"http://www.w3.org/ul/xhtml1/Dli/xhtml1-ulansitional.dli/">
{title}-musicbody {
color:#666666;
text-align : center;
font-size : 10pt;
margin: 0;
}
ul{
clear:both;
margin: 0;
}
li{
list-style:none;
float:left;
background:#DEE7EC;
margin-left:3px;
line-height:20px;
text-align:center;
}
#top {
width:760px;
margin:0 auto;
}
#main {
width:760px;
margin:0 auto;
text-align:left;
}
#footer {
width:760px;
margin:0 auto;
}
#gequ {
width:100px;
}
#geshou {
width:60px;
}
#zhuanji {
width:180px;
}
#nianfen {
width:30px;
}
#zhushi {
width:300px;
}
#shiting {
width:30px;
}
#xiazai {
width:30px;
}
a:link,a:visited,a:active {
color: #666666;
text-decoration: none;
}
a:hover {
color: #666666;
text-decoration: underline;
}
-->
color:#666666;
text-align : center;
font-size : 10pt;
margin: 0;
}
ul{
clear:both;
margin: 0;
}
li{
list-style:none;
float:left;
background:#DEE7EC;
margin-left:3px;
line-height:20px;
text-align:center;
}
#top {
width:760px;
margin:0 auto;
}
#main {
width:760px;
margin:0 auto;
text-align:left;
}
#footer {
width:760px;
margin:0 auto;
}
#gequ {
width:100px;
}
#geshou {
width:60px;
}
#zhuanji {
width:180px;
}
#nianfen {
width:30px;
}
#zhushi {
width:300px;
}
#shiting {
width:30px;
}
#xiazai {
width:30px;
}
a:link,a:visited,a:active {
color: #666666;
text-decoration: none;
}
a:hover {
color: #666666;
text-decoration: underline;
}
";
$bottom="
";
$content=$top;
$content=str_replace("{title}",$sitename,$content);
$content.="
$rootpath=getcwd();
$domain=$_SERVER['SERVER_NAME'];
$urlpath="http://".$domain;
$filepath = $_SERVER['PHP_SELF'];
$filepath = dirname($filepath);
$urlpath=$urlpath.$filepath;
$i=1;
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if(eregi(".mp3$",$file)){
$mp3_file = $file;
$fp = fopen($mp3_file,"rb");
fread($fp,3)=='ID3'? $mp3 = new readid3v23($mp3_file):$mp3 = new readid3v1x($mp3_file);
fclose($fp);
$url=$urlpath."/".$file;
$content.="
$playtitle = $mp3->title."-".$sitename;
$playcontent=$top;
$playcontent=str_replace("{title}",$playtitle,$playcontent);
$playcontent.="ID3版本:V2-".$mp3->major_version."-".$mp3->revision_number;
$playcontent.="
歌曲:".$mp3->title;$playcontent.="
歌手:".$mp3->artist;$playcontent.="
专辑:".$mp3->album;$playcontent.="
年代:".$mp3->year;$playcontent.="
注释:".$mp3->comments;$playcontent.="";
$playcontent.="
歌词:".$mp3->lyric."";
$playcontent.=$bottom;
$filename = $i.".html";
$fp = fopen ($filename,"w"); //打开文件指针,创建文件
if (!is_writable ($filename)){
die ("文件:".$filename."不可写,请检查其属性后重试!");
}
if (!fwrite ($fp,$playcontent)){
die ("生成文件".$filename."失败!");
}
fclose ($fp); //关闭指针
$i++;
}
}
closedir($handle);
}
$content.=$bottom;
$filename = "index.html";
$fp = fopen ($filename,"w");
if (!is_writable ($filename)){
die ("文件:".$filename."不可写,请检查其属性后重试!");
}
if (!fwrite ($fp,$content)){
die ("生成文件".$filename."失败!");
}
fclose ($fp);
echo "生成成功!";
?>
2。简单的演示
play.php
fileselect.php
$path="d://music/";
$filesize=filesize($path.$file_name);
header("Content-type:audio/mpeg");
header("Content-length:$filesize");
readfile($paht.$file_name);
?>
转载地址:http://btqll.baihongyu.com/