Playing a streamed mp3

Posted: August 20, 2010 in Development, Technology, Windows Phone 7

Capture

In an attempt to start changing this blog from nothing but a platform for me to spout off about projects, or talking aboutcontests :) , and actually try to provide some value to readers, I’m starting a new category on the site called “Quick tips”.  When I can, I’ll be posting small bits of code that I think would be helpful.  If you have a question on how I’ve done anything in the applications I’ve written (more to be posted soon), please feel free to ask, and I’ll answer what I can.

On my last post, Ishmeet asked if I could shed a bit of light on how I was able to play the audio clips in the PhoneTree application, so here goes:

One of the best things about having both Silverlight and XNA at your disposal when developing for WP7 is that you can use the XNA pieces within your Silverlight app when ever you want.

For the MediaPlayer functionality you’ll need to add the Microsoft.Xna.Framework.dll reference.  Once you’ve done that, use the following code:

1  MediaPlayer.Stop();

2  Song song = Song.FromUri("Sample Song", new Uri(txtUrl.Text ) );

3  MediaPlayer.Play(song);

If you try and run the code at this point, it will play, but the application will also crash complaining that FrameworkDispatcher.Update has not been called.  After looking around for a while I finally found a post by Danny on the XNA Team explaining the problem.  He did a better job explaining it then I can so you can read it here.

Basically you’ll need the following class:

01 public class XNAAsyncDispatcher : IApplicationService

02 {

03       private DispatcherTimer frameworkDispatcherTimer;

04

05       public XNAAsyncDispatcher(TimeSpan dispatchInterval)

06      {

07           this.frameworkDispatcherTimer = new DispatcherTimer();

08           this.frameworkDispatcherTimer.Tick += new EventHandler(frameworkDispatcherTimer_Tick);

09           this.frameworkDispatcherTimer.Interval = dispatchInterval;

10      }

11

12        void IApplicationService.StartService(ApplicationServiceContext context)  { this.frameworkDispatcherTimer.Start(); }

13        void IApplicationService.StopService() { this.frameworkDispatcherTimer.Stop(); }

14        void frameworkDispatcherTimer_Tick(object sender, EventArgs e) { FrameworkDispatcher.Update(); }

15 }

and then add it to the Application Lifetime Objects like so:

1  this.ApplicationLifetimeObjects.Add(new XNAAsyncDispatcher(TimeSpan.FromMilliseconds(50)));

Once you’ve done that, your good to go.

Here is a sample showing off this technique.

Source: 4MK Mobile Dev Blog

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s