Nginx SoundCloud是如何播放mp3文件的?
我正试图实现和SoundCloud一样的方法,在我的webiste上用JwPlayer播放.mp3文件。 我有一个很大的mp3文件,有时长达4小时。所以我不想在JwPlayer中将其作为一个源文件加载。 我看到soundcloud在部分(160k图片)中播放mp3文件。他们是怎么做到的 我尝试将我的nginx配置为Nginx SoundCloud是如何播放mp3文件的?,nginx,audio,stream,mp3,soundcloud,Nginx,Audio,Stream,Mp3,Soundcloud,我正试图实现和SoundCloud一样的方法,在我的webiste上用JwPlayer播放.mp3文件。 我有一个很大的mp3文件,有时长达4小时。所以我不想在JwPlayer中将其作为一个源文件加载。 我看到soundcloud在部分(160k图片)中播放mp3文件。他们是怎么做到的 我尝试将我的nginx配置为 location /uploads/ { mp4; mp4_buffer_size 160k; mp4_max_buffer_size 20
location /uploads/ {
mp4;
mp4_buffer_size 160k;
mp4_max_buffer_size 200k;
}
但它不起作用。Mp3仍然加载在一个大请求中
我尝试使用php脚本手动读取选项2文件,如
if (isset($_SERVER['HTTP_RANGE'])) {
$c_start = $start;
$c_end = $end;
list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
if (strpos($range, ',') !== false) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: bytes $start-$end/$size");
exit;
}
if ($range == '0-') {
$c_start = 0;
}
else {
$range = explode('-', $range);
$c_start = (int)$range[0];
if($c_start > 2407200) {
exit;
}
$c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $c_start + $end;
}
$c_end = ($c_end > $size) ? $size : $c_end;
if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: bytes $start-$end/$size");
exit;
}
$start = $c_start;
$end = $c_end;
$length = $end - $start + 1; // Calculate new content length
fseek($fp, $start);
header('HTTP/1.1 206 Partial Content');
}
header("Content-Range: bytes $start-$end/$size");
header("Content-Length: $length");
$buffer = 1024 * 8;
while(!feof($fp) && ($p = ftell($fp)) <= $end) {
if ($p + $buffer > $end) {
$buffer = $end - $p + 1;
}
set_time_limit(0); // Reset time limit for big files
echo fread($fp, $buffer);
flush(); // Free up memory. Otherwise large files will trigger PHP's memory limit.
}
fclose($fp);
if(isset($\u服务器['HTTP\u范围]])){
$c_start=$start;
$c_end=$end;
列表(,$range)=分解('=',$服务器['HTTP\u range'],2);
if(strpos($range,,')!==false){
标头(“HTTP/1.1 416请求的范围不可满足”);
标题(“内容范围:字节$start-$end/$size”);
出口
}
如果($range==“0-”){
$c_start=0;
}
否则{
$range=分解('-',$range);
$c_start=(int)$range[0];
如果($c_开始>2407200){
出口
}
$c_end=(isset($range[1])&&is_numeric($range[1])?$range[1]:$c_start+$end;
}
$c_-end=($c_-end>$size)?$size:$c_-end;
如果($c|u开始>$c|u结束|$c|u开始>$size-1 |$c|u结束>=$size){
标头(“HTTP/1.1 416请求的范围不可满足”);
标题(“内容范围:字节$start-$end/$size”);
出口
}
$start=$c_start;
$end=$c_end;
$length=$end-$start+1;//计算新内容长度
fseek($fp,$start);
标题(“HTTP/1.1 206部分内容”);
}
标题(“内容范围:字节$start-$end/$size”);
标题(“内容长度:$Length”);
$buffer=1024*8;
而(!feof($fp)&($p=ftell($fp))$end){
$buffer=$end-$p+1;
}
设置时间限制(0);//重置大文件的时间限制
echo-fread($fp,$buffer);
flush();//释放内存。否则,大文件将触发PHP的内存限制。
}
fclose($fp);
这还不错,但当我停止播放mp3时,脚本会加载其他文件
那么SoundCloud是如何做到这一点的呢?当播放音乐时,他们将音乐装入小段,当播放器停止播放时,请求也停止