Internet Radio in C#
Internet radio, a phenomena that appeared in the early 90's that soon made it's way to it's grand stature today. Sure politics are trying to destroy it, but it's not going away any time soon. There is a great deal of history in Internet Radio from war protests to social networking. The latest advent in internet radio, Pandora, from the Music Genome Project, combines the analysis of each song to provide users the ability to listen to similar music; thus creating their own stations based on style, rhythms, lyrics and more. Nevertheless, I suggest you review the history on Wikipedia. I believe the next step in Internet Radio is playing from your mobile phone which doesn't seem that far off. [note project]
At any rate, our little music player doesn't seem to support internet radio at all. Well, I simply won't have that! Especially because supporting internet streams is so very simple with the Bass.NET api. In this tutorial you will learn to accomplish the following:
- Play streaming audio from an internet radio station.
- Visual C# Express (sorry, Visual C# Express 2005 is no longer available)
- Bass.NET 2.3 (scroll down until you find the .NET 2.0 link)
- Un4seen Bass api (top download link for Windows)
I am going to create a new Windows Form project called MyRadio. To start, you will need to add the reference to the Bass.NET api. Then copy the bass.dll to your output directory by including it in your project followed by setting the Copy to Output Directory property to Copy if newer. Your solution explorer should look like the following:
Once you have that setup look for the Player code from the previous tutorial. If you can't find it, you will find the code layout here.
We are going to modify this class to support internet radio streams. Open up the Player class and add the following code snippet.
public void LoadURL(string url)
{
stream = Bass.BASS_StreamCreateURL(url, 0,
BASSStream.BASS_SAMPLE_FLOAT, null, 0);
}
Bass provides this function for loading a stream from a url. In most cases, it won't matter if the stream is a file or a broadcasting stream; bass handles this for you.
You'll notice that this function looks a lot like our original. Except, instead of StreamCreateFile we are using StreamCreateURL. With the fourth parameter, specified as null, is a callback to the function to receive the stream as it's being downloaded. Also, the BASSStream contains a few flags that may allow us to obtain tags about the music including the title, author, genre and more. There are many flags that have a very specific purpose, but for now we are just going to use the default like in our previous case.
The only thing left for us to do is to provide access in our user interface. I have simply copied our previous UI and added a textbox to allow the url to be entered. When you're done, your user interface may look like the following.
There you go. Hit F5 type in an address to an internet radio station or an hosted song online and you should be good to go. In this tutorial you learned how to use Bass to stream internet radio. In my next tutorial I will be teaching you how to create your own playlist allowing for even more support for internet radio stations.
Comments
http://www.finactu.fi/Juuxy/publish.htm (install from IE)
For one of my projects I have to build a desktop windows app which plays internet radio station. I am trying to follow your tutorial. But I am running into many problems. I am a new c# programmer. Can you make a step by step/ screenshot based tutorial for this app of yours. I am even having problem locating the files you are asking to download, the bass.net 2.3 and bass net api???
It will be really helpful if you can guide me in any way possible.
Thanks,
C
public void LoadURL(string url)
{