【发布时间】:2016-12-29 01:40:40
【问题描述】:
我正在将 mp3 加载到 html <audio> 标记中。该标签的来源实际上是一个 php 脚本,它返回一首不在公共目录中的歌曲。
省略大部分验证和其他代码,用于输出 mp3 的标头是:
header( 'Content-type: {$mime_type}' );
header( 'Content-length: ' . filesize( $file ));
header( 'Content-Disposition: inline;filename="'.$filename.'"' );
header( 'Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0' );
header( 'Pragma: no-cache' );
header( 'Content-Transfer-Encoding: binary');
header( 'Expires: 0');
readfile( $file );
这适用于 Firefox、Chrome(和移动设备)、Edge(和移动设备)和 Opera。但是我似乎无法让 Safari 使用生成的 url 作为音频源。事实上,页面渲染后,DOM 中不会出现任何音频源:
网址可能类似于www.website.com/song.php?sid=234234234234
我已尝试多次调整此文件,但无法弄清楚为什么会在 Safari 上发生这种情况。我的直觉是浏览器对mp3之类的处理方式,应该在header中处理。
任何指导、提示或帮助将不胜感激。
谢谢。
更新: 添加了网络,显示它加载了 mp3 但第二个请求没有?
响应标题:
Name Value
Server Apache
Content-Type audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3
Date Wed, 28 Dec 2016 17:24:48 GMT
Cache-Control no-cache
X-Powered-By PHP/5.3.29
Content-Disposition inline;filename="148_2793d1c49976a3689147634359577ec1aa5619f1.mp3"
Content-Length 834312
Expires 0
Connection Keep-Alive
Content-Transfer-Encoding binary
Accept-Ranges bytes
Keep-Alive timeout=5, max=100
Pragma no-cache
【问题讨论】:
-
如果您在音频标签中看不到
src属性,则问题出在 HTML,而不是 PHP 脚本。它从一开始就不会调用 PHP 脚本。 -
检查标题响应...因为“header('Content-type: {$mime_type}');”没有替换值,因为即使使用“{}”我也没有错,单引号在 php 中不能用于替换 vars 中的值。尝试将其更改为 header('Content-type: '.$mime_type}');如果不起作用,那么请将网络标头添加到您的问题中(如果我有什么问题,请纠正我:))
-
@Asfo 很好的建议,但没有奏效。以下是网络标头:i.imgur.com/9xsiyqx.png
标签: php audio safari http-headers readfile