WP7 Tips–Part I– Media File Coding Techniques to help pass the Windows Phone 7 Marketplace Certification Requirements
- by seaniannuzzi
Overview Developing an application that plays media files on a Windows Phone 7 Device seems fairly straight forward. However, what can make this a bit frustrating are the necessary requirements in order to pass the WP7 marketplace requirements so that your application can be published. If you are new to this development, be aware of these common challenges that are likely to be made. Below are some techniques and recommendations on how optimize your application to handle playing MP3 and/or WMA files that needs to adhere to the marketplace requirements. Windows Phone 7 Certification Requirements Windows Phone 7 Developers Blog Some common challenges are: Not prompting the user if another media file is playing in the background before playing your media file Not allowing the user to control the volume Not allowing the user to mute the sound Not allowing the media to be interrupted by a phone call To keep this as simple as possible I am only going to focus on what “not to do” and what “to do” in order to implement a simple media solution. Things you will need or may be useful to you before you begin: Visual Studio 2010 Visual Studio 2010 Feature Packs Windows Phone 7 Developer Tools Visual Studio 2010 Express for Windows Phone Windows Phone Emulator Resources Silverlight 4 Tools For Visual Studio XNA Game Studio 4.0 Microsoft Expression Blend for Windows Phone Note: Please keep in mind you do not need all of these downloaded and installed, it is just easier to have all that you need now rather than add them on later. Objective Summary Create a Windows Phone 7 – Windows Media Sample Application. The application will implement many of the required features in order to pass the WP7 marketplace certification requirements in order to publish an application to WP7’s marketplace. (Disclaimer: I am not trying to indicate that this application will always pass as the requirements may change or be updated) Step 1: – Create a New Windows Phone 7 Project Step 2: – Update the Title and Application Name of your WP7 Application For this example I changed: the Title to: “DOTNETNUZZI WP7 MEDIA SAMPLE - v1.00” and the Page Title to: “media magic”. Note: I also updated the background. Step 3: – XAML - Media Element Preparation and Best Practice Before we begin the next step I just wanted to point out a few things that you should not do as a best practice when developing an application for WP7 that is playing music. Please keep in mind that these requirements are not the same if you are playing Sound Effects and are geared towards playing media in the background. If you have coded this – be prepared to change it: To avoid a failure from the market place remove all of your media source elements from your XAML or simply create them dynamically. To keep this simple we will remove the source and set the AutoPlay property to false to ensure that there are no media elements are active when the application is started. Proper example of the media element with No Source: Some Additional Settings - Add XAML Support for a Mute Button Step 4: – Boolean to handle toggle of Mute Feature Step 5: – Add Event Handler for Main Page Load Step 6: – Add Reference to the XNA Framework Step 7: – Add two Using Statements to Resolve the Namespace of Media and the Application Bar using Microsoft.Xna.Framework.Media; using Microsoft.Phone.Shell; Step 8: – Add the Method to Check the Media State as Shown Below Step 9: – Add Code to Mute the Media File Step 10: – Add Code to Play the Media File //if the state of the media has been checked you are good to go. media_sample.Play(); Note: If we tried to perform this operation at this point you will receive the following error: System.InvalidOperationException was unhandled Message=FrameworkDispatcher.Update has not been called. Regular FrameworkDispatcher.Update calls are necessary for fire and forget sound effects and framework events to function correctly. See http://go.microsoft.com/fwlink/?LinkId=193853 for details. StackTrace: at Microsoft.Xna.Framework.FrameworkDispatcher.AddNewPendingCall(ManagedCallType callType, UInt32 arg) at Microsoft.Xna.Framework.UserAsyncDispatcher.HandleManagedCallback(ManagedCallType managedCallType, UInt32 managedCallArgs) at Microsoft.Xna.Framework.UserAsyncDispatcher.AsyncDispatcherThreadFunction() It is not recommended that you just add the FrameworkDispatcher.Update(); call before playing the media file. It is recommended that you implement the following class to your solution and implement this class in the app.xaml.cs file. Step 11: – Add FrameworkDispatcher Features I recommend creating a class named XNAAsyncDispatcher and adding the following code: After you have added the code accordingly, you can now implement this into your app.xaml.cs file as highlighted below. Note: If you application sound file is not playing make sure you have the proper “Build Action” set such as Content. Running the Sample Now that we have some of the foundation created you should be able to run the application successfully. When the application launches your sound options should be set accordingly when the “checkMediaState” method is called. As a result the application will properly setup the media options and/or alert the user accordinglyper the certification requirements. In addition, the sample also shows a quick way to mute the sound in your application by simply removing the URI source of the media file. If everything successfully compiled the application should look similar to below. <sound playing> Summary At this point we have a fully functional application that provides techniques on how to avoid some common challenges when working with media files and developing applications for Windows Phone 7. The techniques mentioned above should make things a little easier and helpful in getting your WP7 application approved and published on the Marketplace. The next blog post will be titled: WP7 Tips–Part II - How to write code that will pass the Windows Phone 7 Marketplace Requirements for Themes (light and dark). If anyone has any questions or comments please comment on this blog.