MP3 Flash Player PHP readfile() 缓冲区

MP3 Flash Player PHP readfile() buffer

我有一个 mp3 flash 播放器,即使显示歌曲的加载部分,它也能完美运行。

<embed src="player.swf" width="70" height="15" 
FlashVars="song=songs/song.mp3"/>

但现在我需要从.php文件中加载歌曲,所以它会像这样

<embed src="player.swf" width="70" height="15" FlashVars="song=song.php"/>

这是我的 song.php

$filename="songs/song.mp3";
header("Expires: -1");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Content-type: audio/mpeg");
header("Content-Transfer-Encoding: binary");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0");
$len = filesize($filename);
header("Content-Length: $len;\n");
$outname=$archivo;
header("Content-Disposition: application/octet-stream");

readfile($filename); 

它与 play/stop 按钮完美配合,但问题是这样它不会显示歌曲的加载部分。

在搜索 tips/codes/answers 几个小时后,我找到了来自 Anindya 并由 Mihai Iorga 编辑的代码,最终对我有用,检查问题 here。代码:

set_time_limit(0);
$dirPath = "path_of_the_directory";
$songCode = $_REQUEST['c'];
$filePath = $dirPath . "/" . $songCode . ".mp3";
$strContext=stream_context_create(
    array(
        'http'=>array(
        'method'=>'GET',
        'header'=>"Accept-language: en\r\n"
        )
    )
);
$fpOrigin=fopen($filePath, 'rb', false, $strContext);
header('Content-Disposition: inline; filename="song.mp3"');
header('Pragma: no-cache');
header('Content-type: audio/mpeg');
header('Content-Length: '.filesize($filePath));
while(!feof($fpOrigin)){
  $buffer=fread($fpOrigin, 4096);
  echo $buffer;
  flush();
}
fclose($fpOrigin);

所以我只是在 song.php 中替换了这段代码,现在它完美无缺了!