A Silverlight program can get access to the phone’s photo library for
retrieving photos and saving them, but it needs to use an XNA class
named MediaLibrary in the Microsoft.Xna.Framework.Media namespace. You need that same class—and other classes in that namespace—for accessing and playing music
Any program that uses MediaLibrary needs a reference to the Microsoft.Xna.Framework DLL; The MusicByComposer program also needs a reference to Microsoft.Phone.Controls for the Pivot control.
When you use XNA
services to play music from a Silverlight application, some issues are
involved. As described in the topic in the XNA documentation entitled
“Enable XNA Framework Events in Windows Phone Applications,” you need a
class that calls the XNA static method FrameworkDispatcher.Update at the same rate as the video refresh rate, thirty times per second. The following class in the MusicByComposer project is basically the class shown in that documentation topic:
Example 1. Silverlight Project: MusicByComposer File: XnaFrameworkDispatcherService.cs
using System; using System.Windows; using System.Windows.Threading; using Microsoft.Xna.Framework;
namespace MusicByComposer { public class XnaFrameworkDispatcherService : IApplicationService { DispatcherTimer timer;
public XnaFrameworkDispatcherService() { timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromTicks(333333); timer.Tick += OnTimerTick; FrameworkDispatcher.Update(); }
void OnTimerTick(object sender, EventArgs args) { FrameworkDispatcher.Update(); }
void IApplicationService.StartService(ApplicationServiceContext context) { timer.Start(); }
void IApplicationService.StopService() { timer.Stop(); } } }
|
You’ll need to instantiate that class in the ApplicationLifetimeObjects section of the App.xaml file. Notice the XML namespace declaration for “local”:
Example 2. Silverlight Project: MusicByComposer File: App.xaml
<Application x:Class="MusicByComposer.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:local="clr-namespace:MusicByComposer">
<!--Application Resources--> <Application.Resources> </Application.Resources>
<Application.ApplicationLifetimeObjects>
<!-- Required for playing music from a Silverlight app --> <local:XnaFrameworkDispatcherService /> <!--Required object that handles lifetime events for the application--> <shell:PhoneApplicationService Launching="Application_Launching" Closing="Application_Closing" Activated="Application_Activated" Deactivated="Application_Deactivated"/> </Application.ApplicationLifetimeObjects> </Application>
|
For testing purposes, the phone emulator has a music library that consists of a single album with three short songs, which is great for establishing basic album retrieval and playing logic, but it hardly gives the program a real workout.
For debugging
a program running on the actual phone from Visual Studio, you’ll need
to exit the desktop Zune program (because it wants exclusive access to
the music library) and instead run the Connect tool, WPDTPTConnect32 on 32-bit Windows or WPDTPTConnect64 on 64-bit Windows.
I also discovered another
problem. When the program was deployed to the phone and running apart
from Visual Studio, the program would report that the music library on
the phone had no music…. except if I first ran an XNA program. I am told
this is a bug in the initial release of Windows Phone 7, and I decided
to work around this bug by making the program accessible from the Games
hub on the phone. To do this I set the following attribute in the App tag of the WMAppManifest.xml file:
Genre="apps.games"
I also gave the
program Background.png and ApplicationIcon.png images containing a
portrait of perhaps the most famous individual in the composer-centric
tradition.