使用PHP在Android上打开实时音频流 [英] Using php to opening live audio stream on android

查看:237
本文介绍了使用PHP在Android上打开实时音频流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有Shoutcast一样/ icecast理解音频流。我希望能够提供一个链接,如mobiledroid.php在我们的网站将使用默认的播放器中打开。我已经看到了这个在其他网站做的,所以我不知道这是可能的。

We have shoutcast/icecast audio streams. I'd like to be able to provide a link such as mobiledroid.php on our web site that will open using default player. I've seen this done on another site so I do know it's possible.

我想它使用PHP的头,并通过PHP文件作为流?

I assume it uses php headers and streams via the php file as a stream?

用布拉德的指令,机器人其实是给予有声播放器中打开的选项。尼斯之一。

Using Brad's instructions, the android actually gives the option to open with sound player. Nice one.

这也可以通过电脑,但没有在Android上扮演着怎样的WMP上面的链接剧

It also plays on WMP through PC but not on the android how the above link plays

header("Content-type: audio/mpeg"); 
header("Transfer-Encoding: chunked"); 
header("Connection: close");

$sock = fsockopen($streamname,$port); //$streamname is the IP

fputs($sock, "GET $path HTTP/1.0\r\n"); //path in my case is /;stream.mp3
fputs($sock, "Host: $ip\r\n"); 
fputs($sock, "User-Agent: WinampMPEG/2.8\r\n"); 
fputs($sock, "Accept: */*\r\n"); 
fputs($sock, "Icy-MetaData:1\r\n"); 
fputs($sock, "Connection: close\r\n\r\n"); 


fpassthru($sock); 

fclose($sock);

在Android,它说:对不起,这个播放器不支持此类型的音频文件的

On the android, it says "Sorry, this player does not support this type of audio file"

更新2:

删除传输编码将在Android,但像往常一样发挥将需要很长的时间,开始流以preparing状态,由于实时流没有内容长度

Removing "Transfer-Encoding" will play on android but as usual will take a long time to begin stream with "Preparing" status due to a live stream not having "Content-Length"

标题(传输编码:无); 也从上面code删除:

header("Transfer-Encoding: none"); also removed from above code:

引述布拉德:

的Andr​​oid 2.3及更高版本有一个问题,传输编码设置为无。去除硬设置头放流回到分块传输编码。这对于Android 2.3+的伟大工程。本来我已经禁用分块编码为VLC不支持它。这也似乎是安卓2.2及以上不支持分块编码。

Android 2.3 and later has an issue with Transfer-Encoding set to "none". Removing that hard-set header puts the stream back to chunked Transfer-Encoding. This works great for Android 2.3+. Originally I had disabled chunked encoding as VLC doesn't support it. It also seems that Android 2.2 and older does not support chunked encoding.

在这里指出,虽然它的作品在Android上,最实时流需要时间来开始一个可怕的金额。

Noting here that although it works on android, most live streams will take an awful amount of time to begin.

推荐答案

传统上,你将成为这将您的浏览器进行保存,然后在任何播放软件配置为开这样的播放列表打开播放列表文件。该播放器将读取流的URL,连接到它,并开始流。

Problem

Traditionally, you would serve a playlist file which would be saved by your browser, and then opened in whatever player software was configured to open such playlists. The player would then read the URL of the stream, connect to it, and begin streaming.

在Android上,他们(对于一些疯狂的原因)决定不支持播放列表文件,所支持的媒体。正因为如此,也没有直接的方法采用传统的方法来启动播放机。

On Android, they (for some crazy reason) chose not to support playlist files as supported media. Because of this, there is no direct way to launch the player using the traditional method.

在Android上看来,如果直接连接,而不是将文件下载到MP3文件/流,默认操作是打开了一个媒体播放器,并尝试以流式处理。这意味着你可以很容易地直接链接到您的流的URL(绕过播放列表的步骤完全)。

On Android it seems that if you link directly to an MP3 file/stream, instead of downloading the file the default action is to open up a media player and try to stream it. This means that you can easily link directly to the URL of your stream (bypassing the playlist step entirely).

有一个陷阱......流送达Shoutcast一样是不完全的HTTP兼容。他们是亲密,但并不完美。真正挂断这里似乎是内容长度之一。通常情况下,HTTP服务器发送一个内容长度响应头。与流媒体,长度未知。有解决这个问题有两种方式:

There is a catch... streams served with SHOUTcast aren't exactly HTTP compliant. They are close, but not perfect. The real hangup here seems to be one of content length. Typically, HTTP servers send a Content-Length response header. With streaming media, the length is unknown. There are two ways around the problem:

  1. 使用块传输编码。
    传输编码:分块
    然后,使用分块编码的流,你很棒,你可以通过设置下面的标题做去了。
  2. 使用任何传输编码,并指定当资源被传输完的连接将被关闭,这些标题:
    传输编码:无
    连接:关闭
  1. Use chunked transfer encoding. You can do this by setting the following headers:
    Transfer-Encoding: chunked
    Then, use chunked encoding for the stream, and you're good to go.
  2. Use no transfer encoding, and specify that the connection will be closed when the resource is finished transferring, with these headers:
    Transfer-Encoding: none
    Connection: close

您例如链接使用的方法之一。我用两个方法成功地为好。

Your example link uses method one. I'm using method two successfully as well.

在你的服务器在说有效的HTTP,播放在Android上似乎是一件轻而易举的事。

Once your server is talking valid HTTP, playback on Android seems to be a breeze.

有极少数的方法去实施此修复程序。你提到一个PHP的解决方案。如果卷曲将无法正常工作,可以随时 的fsockopen() 或<一href="http://www.php.net/manual/en/function.stream-socket-client.php"><$c$c>stream_socket_client()做一个直接的TCP连接,并自己处理传入的数据流。从那里,你只需要发送正确的响应头和中继数据,因为它的用武之地。请记住,一个PHP实例将运行为每个连接的客户端,并且每个实例都将为您的服务器的连接,以及。这是不理想的,但取决于你有什么用,可能是你的唯一方法。

There are a handful of ways to go about implementing this fix. You have mentioned a PHP solution. If cURL won't work, you can always fsockopen() or stream_socket_client() to make a direct TCP connection, and handle the incoming stream data yourself. From there, you simply need to send the correct response headers and relay the data as it comes in. Keep in mind that a PHP instance will be running for each connected client, and each instance will make a connection to your server as well. This isn't ideal, but depending on what you have available, might be the only method for you.

这篇关于使用PHP在Android上打开实时音频流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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