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 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目