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 more broadcasting streams and/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 will fire the following sequence of events:
• | StreamConnecting to notify about the beginning of the connection to the server. |
• | In case the connection should fail, the StreamLoaded event would report the failure. |
• | In case of successful connection the DownloadBuffering event would be fired with its nState parameter set to STREAMING_BUFFERING to notify that connection with the server was established and that buffering begins. This would be followed by a number of DownloadPercentage events to notify about buffering percentage. |
• | Once there will be enough data to play, the control will fire the StreamLoaded event 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 control will fire again the DownloadBuffering event with its nState 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 firing again the DownloadBuffering event with the nState 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 the DownloadBuffering event with the nState parameter set to STREAMING_PLAYING.
After a stall situation, the Internet stream provider could also close the connection: in this case the control will fire the ServerClosed event.
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 control will fire the MetaTagChanged event: at this point the container application will have the possibility to perform the following sequence of tasks:
• | instruct the control to retrieve the tag contents through a call to the ReadSoundInfo 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 fire the MetaTagChanged 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 fire a DownloadComplete method 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 StreamLoaded event is immediately followed by the StreamLoadedFromInetPlaylist event which reports the URL of the loaded stream. |
• | In case the connection should fail the StreamLoaded event would report the failure only if all of the streams contained inside the playlist should fail while the StreamLoadedFromInetPlaylist event would be fired for each failing URL. |
• | ServerClosed event would be fired only when executing the latest entry inside the playlist. |
A sample of access to Internet streams in Visual Basic 6 and Visual C++ 6 can be found inside the following sample installed with the product's setup package:
- InternetRadio