With the rapid development of electronic products, radio stations have almost moved away from people's entertainment lives. However, in recent years, due to the rise of Internet radio stations, Radio stations have gradually become available in the market, if there is a network radio software on the mobile phone, the usage should be relatively high. Currently, iPhone and Symbian already have this type of software in mainstream smartphone platforms. Although the Android platform has not been launched for a long time, it already has such software, recently, I am also working on Android. I will introduce some of my experiences below.
Network radio station type
Currently, Internet radio websites are generally based on the following three protocols:
MMS, RTSP, HTTP
Among them, MMS is the network streaming media protocol proposed by Microsoft, which usually uses WMA format files. Android does not support this Protocol yet, nor does it support WMA format. The reason is obvious, competitors.
RTSP is proposed by RealNetworks. Android supports this protocol, but this Protocol usually uses files in RM format, which is not supported by Android.
HTTP Android is certainly supported, and this Protocol generally uses MP3 file format, which is also supported by Android.
To sum up, if we want to develop network radio software on Android, we can only select an HTTP MP3 format network radio website. Such a website mainly includes Shoutcast and Icecast. These two categories have minor differences, basically the same. The following mainly describes ShoutCast.
Mediaplayer
Anyone familiar with Android may know that mediaplayer is a class used for media playback, and this class supports streaming media playback. This class supports protocols such as RTSP and HTTP, however, we cannot directly use this class to play network radio stations. There are two main reasons:
1. The ShoutCast station mentioned above does not directly adopt the HTTP protocol, and the mediaplayer cannot connect to the ShoutCast station normally.
2. the URL that mediaplayer can parse must be http ://............ /*. * format, that is, the URL must contain the file name. the URL of the ShoutCast station does not conform to this format.
Therefore, some conversions are required.
ShoutCast Protocol
As mentioned above, the ShoutCast network station uses the HTTP protocol. In fact, this statement is not accurate. ShoutCast uses the ShoutCast protocol, which is slightly different from HTTP, the communication process between the client and the server is as follows:
Send a GET request to the server with the following content:
HTTP/1.0/R/n
User-Agent: androidinternetradio/R/n
Accept: Audio/MPEG/R/n
The server returns the following:
Icy 200 OK/R/N (signifying that the server was successful)
Icy-notice1: <br> This stream requires <ahref = "http://www.winamp.com/"> Winamp </a> <br> (redundant notice)
Icy-notice2: SHOUTcast Distributed Network Audio Server/POSIX v1.x. x <br> (tells the client what server it is and version) ShoutCast specific
Icy-Name: Unnamed server/R/N (name of the server)
Icy-Genre: Unknown genre/R/N (what genre the server falls under)
Icy-URL: http://www.shoutcast.com/r/n (homepage for the server)
Content-Type: Audio/MPEG/R/N (content type of the stream to follow)
Icy-Pub: 1/R/N (whether the server is public or not)
Icy-Br: 56/R/N (bitrate of the server)
Icy-metaint: 8192/R/N (if icy-Metadata: 1 was signified this was shown I will discuss this further later)
/R/N (end of header)
At this point the server begins sending the audio data (the audio data is sent from here ).
It can be seen that the reponse of the ShoutCast server is different from the common HTTP protocol. Therefore, the connection cannot be successful directly using the HTTP protocol. You need to write a set of corresponding ShoutCast protocol for processing.
The above introduces some related knowledge. The following describes the software structure, such:
Part of activity app screen code
The SC client connects to a network station through the ShoutCast protocol to obtain the audio data stream of the station.
The HTTP server provides an HTTP connection to forward the audio data received by the scclient to the mediaplayer class.
Mediaplayer audio playback class. You can access http: // 127.0.0.1/a.mp3: port to establish a connection with httpserver to obtain data streams.
Radioserver network radio Server
The core part of developing a network radio on Android is to convert the ShoutCast protocol data stream into a mediaplayer playing format, that is, scclient and httpserver. This article only introduces the general implementation scheme, there are also some details in the specific encoding (for example, when the mediaplayer is connected to httpserver, A contentlenth value must be specified as Infinity.