网络电台种类
目前的网络电台网站一般是基于以下三种协议的:
mms、rtsp、http
其中mms是微软公司提出的网络流媒体协议,通常采用wma格式的文件,Android现在还不支持这种协议,也不支持wma格式,原因比较明显,竞争对手嘛。
rtsp是RealNetworks公司提出的,Android是支持这种协议的,但是这种协议通常采用rm格式的文件,Android是不支持的。
http协议Android是肯定支持的,而且这种协议一般采用mp3的文件格式,Android也支持。
综上,我们要在Android上开发网络电台软件,那么只能选择http协议 mp3格式的网络电台网站,这样的网站主要包括shoutcast和icecast两大类。这两类有微小差别,基本上是相同的,下面主要以 shoutcast为主进行说明。
MediaPlayer
熟悉Android的人可能都知道有MediaPlayer这样一个类,用于媒体播放,而且这个类是支持流媒体播放的,这个类支持的协议包括rtsp、http等,但是我们还不能直接用这个类来播放网络电台,主要有两个原因:
1.上面提到的shoutcast电台并不是直接采用http协议,MediaPlayer无法正常连接到shoutcast电台
2.MediaPlayer 能够正常解析的URL必须是http://............/*.*的形式,也就是说url中必须包括文件名,shoutcast电台的url不符合这样的形式.
因此需要做一些转换的工作。
shoutcast协议
上面已经提到,shoutcast网络电台是采用http协议的,其实这种说法并不准确,shoutcast电台采用的是shoutcast协议,这种协议与http略有不同,客户端与服务器间通讯过程如下:
向服务器发送GET请求,内容如下:
HTTP/1.0
User-Agent:AndroidInternetRadio
Accept:audio/mpeg
服务器端返回如下:
ICY 200 OK (signifying that the server was successful)
icy-notice1:
This stream requires Winamp
(redundant notice)
icy-notice2:SHOUTcast Distributed Network Audio Server/posix
v1.x.x
(tells the client what server it is and version)SHOUTcast
Specific
icy-name:Unnamed Server (Name of the server)
icy-genre:Unknown Genre (what genre the server falls under)
icy-url:http://www.shoutcast.com (homepage for the server)
Content-Type:audio/mpeg (Content type of the stream to follow)
icy-pub:1 (whether the server is public or not)
icy-br:56 (bitrate of the server)
icy-metaint:8192 (if icy-metadata:1 was signified this was shown I will discuss this further later)
(end of header)
At this point the server begins sending the audio data(从这里开始发送音频数据).
可以看出ShoutCast服务器的Reponse与通常的HTTP协议不同,因此直接以HTTP协议方式进行连接不能够成功,需要自己写一套对应ShoutCast协议的处理。
原文:http://www.cnblogs.com/jingzhishen/p/3659165.html