duanchao4445 2015-01-24 09:49
浏览 61
已采纳

MP3 Flash Player PHP readfile()缓冲区

I have a mp3 flash player and it works perfectly even showing the loaded part of the song.

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

But now I need to load the song from a .php file, so it would be like this way

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

and this is my 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;
");
$outname=$archivo;
header("Content-Disposition: application/octet-stream");

readfile($filename); 

It works perfect with play/stop button, but the problem is that this way it doesn't show me the loaded part of the song.

  • 写回答

1条回答 默认 最新

  • duanpanbo9476 2015-01-29 02:33
    关注

    After several hours searching for tips/codes/answers, I found a code from Anindya and edited by Mihai Iorga that finally worked for me, check the question here. The code:

    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
    "
            )
        )
    );
    $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);
    

    So I just replaced this code in my song.php and now it works perfect!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算