4MK Mobile Dev Blog

Adventures in Mobile (and Startup) Development

Quick tip: Playing a streamed mp3


In an attempt to start changing this blog from nothing but a platform for me to spout off about projects, or talking about contests :) , 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:

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

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:

 public class XNAAsyncDispatcher : IApplicationService
        private DispatcherTimer frameworkDispatcherTimer;

        public XNAAsyncDispatcher(TimeSpan dispatchInterval)
            this.frameworkDispatcherTimer = new DispatcherTimer();
            this.frameworkDispatcherTimer.Tick += new EventHandler(frameworkDispatcherTimer_Tick);
            this.frameworkDispatcherTimer.Interval = dispatchInterval;

        void IApplicationService.StartService(ApplicationServiceContext context) { this.frameworkDispatcherTimer.Start(); }
        void IApplicationService.StopService() { this.frameworkDispatcherTimer.Stop(); }
        void frameworkDispatcherTimer_Tick(object sender, EventArgs e) { FrameworkDispatcher.Update(); }

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

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.

8 Responses to “Quick tip: Playing a streamed mp3”

  1. [...] This post was mentioned on Twitter by Indrajit Chakrabarty and Ginny Caughey, 4MK Mobile. 4MK Mobile said: New Blog Post: Quicktip: Playing a streamed mp3 http://bit.ly/9SY0cv #WP7 #WP7Dev [...]

    Pingback by Tweets that mention Quick tip: Playing a streamed mp3 -- Topsy.com — August 19, 2010 @ 5:25 pm

  2. Hey Barranger,

    Thanks a ton for the info. I really appreciate your response and for attaching a sample code… Nice work …
    Thanks again man ..!!

    Comment by Ishmeet — August 19, 2010 @ 9:23 pm

  3. Now I can finally have speech to text in my translator application :D

    Comment by Ishmeet — August 19, 2010 @ 9:25 pm

  4. That’s fantastic, how is the voice on that, the one for Twilio leaves a little to be desired.

    Comment by barranger — August 19, 2010 @ 10:05 pm

  5. Voice is kinda ok.. but not upto the mark :( .
    I am still trying to find a good solution for converting speech to text.

    I have a solution running now.. but you really have to be at a very quiet place and speak directly in the mic to transcribe. Android phones use a very well system to transcribe speech to text. I wish I could lay my hands on that..

    I am hoping TellMe on wp7 will help me do the transcribing.. :S

    thanks again mate.. :)

    Comment by Ishmeet — August 20, 2010 @ 10:40 am

  6. I see you have my latest tweets working… :) !!

    Comment by Ishmeet — August 20, 2010 @ 10:41 am

  7. Yeah, I think it was a php4 vs php5 kinda thing. I’m pretty sure it’s the only thing I changed, and now it works

    Comment by barranger — August 20, 2010 @ 3:30 pm

  8. [...] Quick Tip : Playing a streamed MP3 [...]

    Pingback by Windows Phone 7 资源汇总 | DanceCoder — May 17, 2012 @ 8:09 pm

RSS feed for comments on this post. TrackBack URL

Leave a Response