从PHP提供mp3时,audio.duration在Safari上返回Infinity [英] audio.duration returns Infinity on Safari when mp3 is served from PHP

查看:376
本文介绍了从PHP提供mp3时,audio.duration在Safari上返回Infinity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个php脚本,可将我的mp3文件发送到html5的音频标签.问题在于,在Safari中,audio.duration标记不起作用并返回无穷大.如果我直接将音频的src设置为文件,则一切正常.但是我不希望我的用户看到文件的路径.

So, I have a php script that sends my mp3 file to the audio tag of html5. The problem is that in Safari, the audio.duration tag does not work and returns infinity. If I set the src of the audio directly to the file everything works fine. But I don't want my users to see the path to the file.

无论如何,这就是我从PHP发送标头的方式.

Anyways, this is how I'm sending my headers from the PHP.

我已经尝试了内容范围.那没有帮助.

I have already tried having the content-ranges. That did not help.

if (file_exists($filename)) {
  $fp = fopen($filename, 'r');
  $etag = md5(serialize(fstat($fp)));
  fclose($fp);
  header("Content-Transfer-Encoding: binary"); 
  header("Content-Type: audio/mpeg");
  header('Content-Length: ' . (string)(filesize($filename)));
  header('Content-Disposition: inline; filename="' . $filename . '"');
  header('X-Pad: avoid browser bug');
  header('Cache-Control: no-cache');
  header('Etag: ' . $etag);

  //GetContentRange($filelength);

  readfile($filename);
  exit;
}
else {
  header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
  echo "no file";
}

推荐答案

我遇到了同样的问题,并添加了几个标题.似乎为我工作-

I was having the same issue and added a couple headers. Seems to work for me -

 header("Pragma: public");
 header("Expires: 0"); 
 header("Content-Type: audio/mpeg");
 header('Content-Length: ' . $fsize);
 header('Content-Disposition: inline; filename="' . $track2play . '"');
 header( 'Content-Range: bytes 0-'.$shortlen.'/'.$fsize); 
 header( 'Accept-Ranges: bytes');
 header('X-Pad: avoid browser bug');
 header('Cache-Control: no-cache');
 header('Etag: ' . $etag);

$ fsize是文件大小,$ shortlen = $ filesize-1 ...已经在Safari 5.1.4和新iPad上进行了测试...

$fsize is file size and $shortlen =$filesize-1...tested on Safari 5.1.4 and on new iPad...

这篇关于从PHP提供mp3时,audio.duration在Safari上返回Infinity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆