Working with Audio in Windows Phone 7
Smart phones are constantly evolving to fit your mobile lifestyle. Most modern phones function as full featured music and video players. Windows Phone 7 follows the path blazed by other smart phones, but adds its own twist. Your musical life on this device revolves around the Music + Videos hub. This article contains details on how to interact with the Music hub from your application. Audio shows up in games and frequently provides auditory feedback for touch and gesture-based applications. This article shows you how to enhance your application by incorporating music and sound effects. I will concentrate on Silverlight applications in this article but the XNA libraries for the phone have rich media libraries too. While it is fairly easy to add sound to your application, there are a few pitfalls to avoid. I learned about these issues while creating applications for the Windows Phone 7 Marketplace and I want to share what I discovered with you. I’ll discuss the most common media classes and teach you how use them correctly to ensure that your application passes the Marketplace certification process. See the January/February 2011 issue of CODE Magazine for other articles on how to get started with Windows Phone 7. Joining the Music + Video Hub Microsoft envisions a seamless media experience with the Music + Video hub. This hub is the central location for everything related to media. This is where you find the Zune player, but you can also find third-party applications located here. Music hub applications share the same data stores and have certain responsibilities to uphold. Under the right conditions, your app becomes a full-fledged member of the Music + Videos Hub. During certification, Microsoft determines if your app belongs in this hub by checking whether your code calls the Microsoft.Devices.MediaHistory or Microsoft.Devices.MediaHistoryItem classes. If so, your application is automatically added to the hub when installed. The Windows Phone 7 Application Certification Requirements document (http://tinyurl.com/32jqzqt) spells out the details but here are a few tidbits for your enjoyment. - 6.4.3 The application must update the “History” area of the Music + Videos Hub when the application plays media.
- 6.4.4 The application must update the “New” area of the Music + Videos Hub when media is added to the device or when the user creates an “object” in the application (for example, a radio station is created, a music tag is created).
The hub consists of four areas. - Zune: the player for podcasts, music, radio and videos. Also contains the Zune Marketplace and your playlists.
- New: shows a list of the latest media added to the phone.
- History: lists all recently played media.
- Marquee: shows a list of all Music + Video hub applications. If your app qualifies it is automatically added to the marquee.
Creating a full-fledged member of the Music hub is beyond the scope of this article. Microsoft requires all third-party hub applications to behave in a consistent way. Because Microsoft enforces more rigorous certification requirements for Music + Video hub applications, they have better access to media and can update the user’s history list or play items from the Music hub. Not every app that plays music needs to be a member of the Music + Video hub, however. Not Part of the Hub Club Computer games are the cardinal example of applications that use music and sound effects to enhance the user experience. Music plays during gameplay but it is not coming from the user’s music library. A game that acts like this is not considered a Music + Videos Hub application by Microsoft. Instead it goes by a different designation. It’s called a MediaLib application. To qualify for this title, an application must access the MediaLibrary, MediaPlayer, MediaSource, MediaElement, or FMRadio classes. Microsoft adds the MEDIALIB capability to your application manifest during the certification process when they detect calls to these classes. <Capabilities> <!-- A sample of application capabilities within the WMAppManifest file --> <Capability Name="ID_CAP_GAMERSERVICES"/> <Capability Name="ID_CAP_IDENTITY_DEVICE"/> <Capability Name="ID_CAP_IDENTITY_USER"/> <Capability Name="ID_CAP_LOCATION"/> <Capability Name="ID_CAP_MEDIALIB"/> <Capability Name="ID_CAP_MICROPHONE"/> <Capability Name="ID_CAP_NETWORKING"/> <Capability Name="ID_CAP_PHONEDIALER"/> <Capability Name="ID_CAP_PUSH_NOTIFICATION"/> <Capability Name="ID_CAP_SENSORS"/> </Capabilities>
Microsoft uses capabilities to signal to the consumer what an application needs on the phone. For example, if your app launches the web browser, the consumer will be informed of this requirement before downloading the app. The product description page in the Zune Marketplace shows a list of required capabilities. The idea behind this proclamation is obvious - the user knows about the program requirements before installing it on their phone. Figure 1 shows my Coin Flipper product description page. Coin Flipper plays an animation when the coin is flipped and provides further feedback by playing a sound effect and vibrating the phone briefly. Because Coin Flipper uses the phone’s VibrateController my app was flagged with the sensors capability. Since I play a few sound effects, it also got marked as a media library application. The other three capabilities are there because I use the Silverlight Ad control.  Figure 1: Coin Flipper application page on Zune Marketplace. |