How to play Internet streams and radio stations |
|
There are three main types of Internet stream:
• | Broadcasting streams |
• | Sound files available on a web site |
• | Playlists which main contain both broadcasting streams or sound files |
These streams cannot be loaded through the LoadSound method: for this purpose you must use the LoadInternetStream method which gives the possibility to pass a URL in place of a pathname. Let's see how to manage these different streams.
Default settings related to Internet connections, like the buffer length and eventual timeouts, can be retrieved and modified through the InternetSettingsGet and InternetSettingsSet methods.
Broadcasting streams
Internet radio is a broadcasting service which transmits one or more sound streams via the Internet: for this kind of streams, the control will connect to the remote server and the container application will be notified about the situation through the CallbackForPlayersEvents delegate having the following values for the nEvent parameter:
• | EV_STREAM_CONNECTING to notify about the beginning of the connection to the server. |
• | In case the connection should fail, EV_STREAM_LOADED would report the failure inside the nData1 parameter. |
• | In case of successful connection the nEvent parameter would be set to EV_DOWNLOAD_BUFFERING with the nData1 parameter set to STREAMING_BUFFERING to notify that connection with the server was established and that buffering has begun. This would be followed by a number of EV_DOWNLOAD_PERC events to notify about buffering percentage. |
• | Once there will be enough data to play, EV_STREAM_LOADED would report the connection's success, allowing the container application to start the playback session through a call to the PlaySound method. The effective URL of the broadcasting stream can be obtained through the GetLoadedSoundFile method. |
Immediately after starting playback, the CallbackForPlayersEvents delegate would be invoked with the nEvent parameter set to EV_DOWNLOAD_BUFFERING and with the nData1 parameter set to STREAMING_PLAYING; note that, during the playback session, the control will continue buffering data from the Internet: if for any reason, for example for a slow connection, the download should stall, the control will notify the container application through the EV_DOWNLOAD_BUFFERING with the nData1 parameter set to STREAMING_BUFFERING: when there will be again enough data to play, the control will automatically resume playback notifying the container application through EV_DOWNLOAD_BUFFERING and with the nData1 parameter set to STREAMING_PLAYING.
After a stall situation, the Internet stream provider could also close the connection: in this case the CallbackForPlayersEvents delegate would be invoked with the nEvent parameter set to EV_SERVER_CLOSED.
During playback, the server will usually send information about the radio station and about the song in playback: the control has the capability to notify in real-time the availability of this information, usually called "tags", and to retrieve the various strings composing the same tag: for this purpose the the CallbackForPlayersEvents delegate is invoked with nEvent parameter set to EV_META_CHANGED: at this point the container application will have the possibility to perform the following sequence of tasks:
• | instruct the control to retrieve tag contents through a call to the SoundInfoGet method. |
• | verify the effective availability of the tags and the number of strings composing the tag through a call to the IsTagAvailable method. |
• | retrieve each of the various strings composing the tag through a call to the GetTagString method. |
Sound files available on a web site
Loading a sound file from a web site is mostly identical to loading a broadcast stream with the following differences:
• | The remote server will not send metadata so the control will never generate the EV_META_CHANGED event. |
• | Differently from streams, that can be created in real-time, sound files have a limited size: this means that, at a certain extent, the download of the file will be completed: the control will in this case the CallbackForPlayersEvents delegate would be invoked with the nEvent parameter set to EV_DOWNLOAD_COMPLETE allowing the container application to gain access to tags like ID3V1 which, for MP3 files, are stored on the file's bottom. |
Playlists which main contain more broadcasting streams and/or sound files
Many radio stations will play a small intro before starting playback of the real broadcasting streams: some achieve this by loading a playlist (usually in ASX or PLS format) which contains the URL pointing to the intro message (usually a WMA or MP3 file stored inside the server) followed by the real URL of the broadcasting stream.
In some cases the playlist may point to other nested playlists: in this case our component will try to manage the situation by updating the playlist with new contents of the nested playlist.
When dealing with playlists, most of events fired by the component will be identical to the ones described for broadcasting streams and for sound files with the following differences:
• | In case of success EV_STREAM_LOADED event is immediately followed by the EV_STREAM_LOADED_FROM_INET_PLS event which reports the URL of the loaded stream. |
• | In case the connection should fail the EV_STREAM_LOADED event would report the failure only if all of the streams contained inside the playlist should fail while the EV_STREAM_LOADED_FROM_INET_PLS event would be fired for each failing URL. |
• | EV_SERVER_CLOSED event would be fired only when executing the latest entry inside the playlist |
A sample of access to Internet streams in Visual C# and Visual Basic.NET can be found inside the following sample installed with the product's setup package:
- InternetRadio