Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
播放php mp3生成的文件,无需保存或下载_Php_Html - Fatal编程技术网

播放php mp3生成的文件,无需保存或下载

播放php mp3生成的文件,无需保存或下载,php,html,Php,Html,因此,我正在生成mp3文件,它工作正常,因为当我下载它时,我可以很好地播放它,但现在我想做的是将该文件输出到我的浏览器并在浏览器中播放,所以我尝试的是: header("Pragma: no-cache"); header('Cache-Control: no-store, no-cache, must-revalidate'); header('Content-Type: audio/mepeg'); header("Content-Disposition: attachment; filen

因此,我正在生成mp3文件,它工作正常,因为当我下载它时,我可以很好地播放它,但现在我想做的是将该文件输出到我的浏览器并在浏览器中播放,所以我尝试的是:

header("Pragma: no-cache");
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Content-Type: audio/mepeg');
header("Content-Disposition: attachment; filename=\"validate.mp3\"");
header("Content-Transfer-Encoding: binary");
header("Content-length: $size");

<embed autoplay="true" height="0" width="0" src="actions/play_file"  />

当然id不起作用,它只是强制下载那个文件,因为我使用了

“内容处置:附件;文件名=\”验证.mp3\”)

我很确定我是否使用了正确的html标记?
但如果我是,我所需要的只是正确的标题,以使此工作正常。

以下是一种向上提供文件的方法:

header("Content-type: audio/mpeg");
header("Content-length: " . filesize($file));
header("Cache-Control: no-cache");
header("Content-Transfer-Encoding: binary"); 
readfile($file);
或者成块

$total     = filesize($filepath);
$blocksize = (2 << 20); //2M chunks
$sent      = 0;
$handle    = fopen($filepath, "r");

// Push headers that tell what kind of file is coming down the pike
header('Content-type: '.$content_type);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-length: '.$filesize * 1024);

// Now we need to loop through the file and echo out chunks of file data
// Dumping the whole file fails at > 30M!
while($sent < $total){
    echo fread($handle, $blocksize);
    $sent += $blocksize;
}

exit(0);
$total=filesize($filepath);
$blocksize=(2300万美元!
而($sent<$total){
echo-fread($handle,$blocksize);
$sent+=$blocksize;
}
出口(0);

您要做的重要事情是指定
内容类型
标题。浏览器(或其他用户代理)如何处理标题取决于他们,而不是您。您现在使用的内容类型不正确。请使用
音频/mpeg


让它始终播放的唯一方法是在网页上包含一个播放器。为此,您可以使用HTML5音频标签、Flash、embed等。

有一个HTML5
audio
标签,不是吗?请记住,尊重该标题取决于浏览器。一些用户(比如我自己)覆盖浏览器的默认功能,以始终下载音频/视频类型的内容。如果您有某种播放器(如flash)您可以更好地保证该文件将按预期播放。@kurtzbot如果我保存该mp3文件,然后尝试从服务器播放,它会一直工作吗?@Linas我不太理解您的问题。您不能选择在用户下载音频文件后为其播放。您最好使用嵌入服务器的播放器自动从您的服务器下载音频文件并播放的浏览器。@kurtzbot nevermind我自己找到了解决方案,谢谢您的帮助