在某些浏览器中生成音频的时序问题 [英] Timing problem for generated audio in some browsers
问题描述
我需要从mp3文件生成音频.因此,我使用curl库获取文件,然后设置所需的标头和echo
音频内容.
I need to generate audio from an mp3 file. So I use curl library to get the file, then set required headers, and echo
the audio content.
问题是它无法在Chrome和Safari浏览器中正常运行.正在加载音频文件并开始播放,但是您无法更改时间(无法在javascript中设置.currentTime
,在浏览器中,定时滑块也不起作用). (在Firefox中工作正常).
The problem is that it does not work correctly in Chrome and Safari browsers. The audio files being loaded, and starts playing, but you can't change the time(can't set .currentTime
in javascript, also in browser the timing slider does not work). (In Firefox works fine).
代码:php
$agent = 'stagefright/1.2 (Linux;Android 5.0)';
$url = 'http://www.jplayer.org/audio/mp3/Miaow-07-Bubble.mp3';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) ;
curl_setopt($ch, CURLOPT_REFERER, 'http://www.jplayer.org/');
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
$media = curl_exec($ch);
curl_close($ch);
$content_length = strlen($media);
$type = "audio/mpeg";
header("Content-type: ".$type);
header("Content-length: ".$content_length);
echo $media;
exit;
有什么想法吗?
也许我想念一些php标头?
Maybe I miss some php headers?
谢谢.
推荐答案
我猜想,我缺少一个php标头.
As I guess, I was missing a php header.
需要添加以下标题:
header('Accept-Ranges: bytes');
希望对别人有帮助.
更新:
我在这里发现了一个非常类似的问题: HTML5< audio> Safari直播与否
I found a very similar question here: HTML5 <audio> Safari live broadcast vs not
添加Accept-Ranges
标头可解决chrome的问题.但是对于野生动物园,您需要检查HTTP_RANGE
并添加Content-Range
标头.
Adding the Accept-Ranges
header fix the issue for chrome. But for safari you need to check for HTTP_RANGE
and add Content-Range
header.
这是我的实现,在所有主流浏览器上都可以正常运行.
Here is my implementation, which works fine in all major browsers.
$content_length = strlen($media_total);
$total_bytes = $content_length;
$content_length_1 = $content_length - 1;
if (isset($_SERVER['HTTP_RANGE'])) {
$byte_range = explode('-',trim(str_ireplace('bytes=','',$_SERVER['HTTP_RANGE'])));
$byte_from = $byte_range[0];
$byte_to = intval($byte_range[1]);
$byte_to = $byte_to == 0 ? $content_length_1 : $byte_to;
$media_total = substr($media_total,$byte_from,$byte_to);
$content_length = strlen($media_total);
header('HTTP/1.1 206 Partial Content');
}
else {
$byte_from = 0;
$byte_to = $content_length_1;
}
$content_range = 'bytes '.$byte_from.'-' . $byte_to . '/' . $total_bytes;
header('Accept-Ranges: bytes');
header("Content-Range: ".$content_range);
header("Content-type: ".$type);
header("Content-length: ".$content_length);
header('Content-Transfer-Encoding: binary');
echo $media_total;
exit;
这篇关于在某些浏览器中生成音频的时序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!