Appendix B. Using the Windows Phone FMRadio

All Windows Phone 7 devices will have a built in FM radio tuner. Unlike some other portable music devices users of Windows Phone 7 devices will be able to tune into available FM radio stations. This is very convenient when a user wants to listen to a local station for sporting events or tune to emergency broadcast stations.

The Windows Phone 7 developer APIs provide the ability for developers to utilize the built in FM Radio tuner.

In this appendix you will learn:

• How to turn the FM radio turner on

• How to set the radio region

• How to set the station frequency for the radio

Using the FMRadio in Your Windows Phone 7 Game

The Windows Phone 7 developer APIs provide an easy-to-use API that enables developers to control the FM radio tuner on the device.

The FMRadio type is provided in the Microsoft.Phone.dll assembly. To utilize the FMRadio in the sample, you first need to add the assembly to the project. Right click on the References section of your XNA Windows Phone project and select Add Reference. Click the .NET tab, select the Microsoft.Phone assembly, and then click the OK button.

FMRadio is defined in the Microsoft.Devices.Radio namespace, so you need to add the following using statement at the top of the game:

using Microsoft.Devices.Radio;

Now you need to store the instance of the FMRadio so you can add the following member variable to the Game class:

FMRadio radio;

There is only a single instance of the FMRadio. Because it represents the physical FM tuner built into the hardware, it does not make sense to have multiple instances that developers can set to different kinds of values. To ensure there is one and only one instance of the FMRadio, the FMRadio exposes a singleton instance through a property called Instance.

radio = FMRadio.Instance;

After you have a reference to this instance of the FMRadio, you can set the other properties exposed by FMRadio.

Because the FM radio band has different rules and regulations around the world, the FMRadio type exposes a property call CurrentRegion which is of the type RadioRegion which has values for UnitedStates, Europe, and Japan.

radio.CurrentRegion = RadioRegion.UnitedStates;

As you most likely know, FM radio stations utilize different frequencies. By setting the FM tuner to a specific frequency, you tune your radio into that station and can hear what broadcasts. FMRadio provides a Frequency property that enables you to read and set the current tuner frequency.

radio.Frequency = 101.5;

Note

You might have asked how you can test your FMRadio code on the Windows Phone emulator. The good news is that some fine engineers thought of this problem and decided to play some simple music on the emulator if the FMRadio is set to the 101.5 frequency.

The radio turner might not be turned on. To turn on the radio, you need to set the PowerMode property to RadioPowerMode.On, as shown in the following:

radio.PowerMode = RadioPowerMode.On;

With the radio turned on, you can hear audio play. If you run the sample in the emulator, you should be able to hear some simple music on a loop. If you stop the application, you might notice that the music continues to play. This is because the FMRadio is available throughout the Windows Phone and works like background music.

Some users might choose to disable application access to the FM radio. If the user disables this access, the FMRadio properties will throw a RadioDisabledException. Your application should handle this exception and continue to function.

Summary

The FM radio is a unique feature for Windows Phone 7 devices and provides developers with the ability to tune their games and applications into FM radio stations within their games. This appendix covered how to use the FMRadio type to have users’ phones tune into radio stations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.163.58