2012-02-08 66 views
0

我需要通过此php文件从另一台服务器流式传输媒体文件。如何解决这个标题为流?

<?php 
$out = array(
    'http'=>array(
    'method'=>"GET", 
    'header'=>"Content-type: audio/mpeg\r\n", 
) 
); 

$stream = stream_context_create($out); 

$end = fopen('http://example.com/audio.mp3', 'r', false, $stream); 
fpassthru($end); 
readfile($end); 
?> 

但是标题不起作用。 我该如何解决这个问题?

+0

? – djot 2012-02-08 10:10:38

回答

0

您正在以错误的方向发送标题。你所做的是通知源服务器,你将在GET请求中发送一些audio/mpeg - 无论如何这是无效的,GET请求没有内容。您实际需要做的是将其发送给将要接收内容的客户端。

你应该不需要流上下文完成这个任务 - 试试这个代码:

<?php 

    // Try and open the remote stream 
    if (!$stream = fopen('http://example.com/audio.mp3', 'r')) { 
    // If opening failed, inform the client we have no content 
    header('HTTP/1.1 500 Internal Server Error'); 
    exit('Unable to open remote stream'); 
    } 

    // It's probably an idea to remove the execution time limit - on Windows hosts 
    // this could result in the audio stream cutting off mid-flow 
    set_time_limit(0); 

    // Inform the client we will be sending it some MPEG audio 
    header('Content-Type: audio/mpeg'); 

    // Send the data 
    fpassthru($stream); 
+0

不错的解决方案!但在iOS(5.0.1)中,即使使用mp3 128kbps,流式传输也有点慢。任何想法为什么? PD:我从更快的VPS流式传输。 – ignaces 2012-02-08 12:05:17

+0

你如何测试?通过移动网络或WiFi?请记住,您受限于客户端和远程服务器之间传输链中最慢的链接。因此,如果客户端的互联网连接不能支持128kbps +数据包开销,它将无法工作。同样,如果您的服务器遇到带宽问题,它也将无法工作,原始源服务器也是如此。由于您同时从源服务器下载数据并将其上载到客户端,因此您的服务器将需要每个并发流对称的128k +数据包开销。 – DaveRandom 2012-02-08 12:50:48

+0

这是实时媒体流协议通常使用UDP的一个主要原因。由于其固有的有损性质,这在无线连接上不太可行,但它大大降低了开销,应尽可能地实施。由于丢包乱序到达,UDP流更容易出现小错误,但是TCP流可能会导致重传,这将增加所需的带宽以及延续数据流的等待时间。 – DaveRandom 2012-02-08 12:54:21

0

的fopen后,加入

是内容长度也需要
header('content-type: audio/mpeg'); 
or 
header('content-type: application/octet-stream'); 
+0

第二个示例通常会强制浏览器下载。 – Narf 2012-11-13 14:04:25