Programmatic access to Windows Phone FM tuner

Windows Phone 7 has the FMRadio class (Microsoft.Devices.Radio namespace) that is ready to use:

FMRadio.Instance.PowerMode = RadioPowerMode.On; 

but… there are couple of things to know:

The first time you will use it, chances are that you will get an UnauthorizedAccessDenied exception with an obscure "Access is denied" message.

The problem is that the Zune software must be closed….  but if you close Zune, now when you try to build or debug (F5 or Ctrl+F5) Visual Studio complains that it can’t deploy to the device anymore: “Zune software is not launched. Retry after making sure that Zune software is launched.”

So… you need to deploy the app to the device and then close Zune, debugging might be a problem as if you hit F5 then close Zune, the app will stop; but instead you can right-click on your project and select “Deploy”:

alt 
Now you can close Zune and it should be ok, but the FM Radio only works with headset plugged or you’ll get an error notification. You can check this by code:

 

try
{
    FMRadio fmRadio = FMRadio.Instance;
                
    fmRadio.PowerMode = RadioPowerMode.On;
    fmRadio.CurrentRegion = RadioRegion.UnitedStates;
    fmRadio.Frequency = 99.5;
 
    if (FMRadio.Instance.SignalStrength == 0.0)
    {
        MessageBox.Show("Please connect your headset.");
    }
}
catch (Exception ex)
{
    MessageBox.Show(String.Format("Error: {0}\nClose the Zune
software or disconnect the phone from your computer."
, ex.Message));
} 

 

Also check the Marketplace application certification requirements document, as you need to follow some guidelines while your app plays music, like updating the “History” area of the Music hub, or asking the user his permission to play music in background.

 

Note: I was not able to make FM Radio works in the emulator, some people report that it may take long (minutes) to launch…but for me it keeps telling that headset are not connected.

 

Download my code sample

Published Tuesday, November 30, 2010 12:02 AM by pluginbaby

Comments

Tuesday, November 30, 2010 7:07 AM by peSHIr

# re: Programmatic access to Windows Phone FM tuner

Is the connect tool in the recent SDK update for connecting phone to VS without needing Zune running (and thus enabling media related debugging) a possible workaround for using the FM Radio too?

Tuesday, November 30, 2010 12:11 PM by Justin Angel

# re: Programmatic access to Windows Phone FM tuner

Good blog post Laurent.

It might be a good idea to check if Zune is connected prior to using the FMRadio class by checking if NetworkConnectionType is an ethernet connection.

Checkout this blog post that has more details on Zune Connection detection @

gdwp7dev.wordpress.com/.../zune-detection-and-network-awareness

-- Justin