Getting the groove on - using the FM radio on Windows Phone 7
Join the DZone community and get the full member experience.Join For Free
If you used any of the Microsoft Zune players before, you probably know that those have a built-in FM Radio. Zune that is installed on every Windows Phone 7 device also has the FM radio capability, and there is a public API endpoint that lets developers who use Windows Phone 7 development tools access the radio from their own applications.
IMPORTANT NOTE: You need an actual unlocked Windows Phone 7 developer device in order to follow the steps described in this article. You cannot test radio capabilities inside the emulator.
First of all, I am assuming you already have a simple Windows Phone 7 application "skeleton" ready. If not, make sure you create one - it doesn't need anything complex in it, just the basic page.
The FM radio functionality is exposed via the FMRadio class, that is in fact a singleton. And it's not surprising - it probably wouldn't be a good idea to be able to run multiple radio instances on the same device. This class is a member of Microsoft.Devices.Radio, so make sure you are adding a reference to it in the class header.
The radio functionality can be accessed via:
Before you start with the radio itself, you need to turn it on:
FMRadio.Instance.PowerMode = RadioPowerMode.On;
Setting the PowerMode property is the standard way to do it. This pretty much triggers the Play/Pause mechanism, in user terms.
Once the radio is on, you can set the frequency. This is a decimal value, therefore you should use culture-specific decimal representation to pass the decimal value (depending on your system, you might need to replace the dot with a comma):
FMRadio.Instance.Frequency = 105.5;
One curious fact - if you pass the frequency directly without changing the PowerMode, the radio will start playing automatically.
IMPORTANT NOTE: You need to connect the headphones to the physical device when testing the radio functionality. Those serve as an antenna.
Once you have this set, you should be able to get radio transmissions, if only you don't get an Access Denied exception:
This happens for a very simple reason. To debug on the device, you need to have the Zune software running and the phone should be registered in a full sync relationship. Fair enough, but at the same time Zune is monopolizing the media on the device, therefore blocking access to it from any external resource. Radio is part of the media set, therefore you cannot run it with Zune running as well.
So what should you do - after all, you cannot simply close Zune? There is the WPConnect tool available as a part of the October Windows Phone 7 SDK update. Once you download and install it, go to (for 64-bit machines):
C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.0\Tools\WPConnect
For 32-bit machines:
C:\Program Files\Microsoft SDKs\Windows Phone\v7.0\Tools\WPConnect
Launch WPConnect.exe, that is located in the above mentioned folder. You should see something similar to this:
Of course, Zune software should be closed while you are performing this operation. If it's not, you will get an error notifying you that it should be done.
Now you can start listening to FM radio right from your application.
One more error you can get is RadioDisabledException - it is caused by the Radio being blocked either on the phone itself or specifically for your application:
The first thing you can and should do is check the declared application capabilities in WMAppManifest.xml. Your application must have ID_CAP_MEDIALIB present:
If this is not working, then there are other enforced policies on the device itself.
As a last note, I wanted to let the reader know that once connected to a radio station, you can track the signal strength:
You can do this by using this code:
Make sure you are running this piece in a background thread. Otherwise, the UI thread will become locked.
Opinions expressed by DZone contributors are their own.