How to solve the MediaElement does not play media problem when using Silverlight 2.0

by APIJunkie 26. April 2008 09:44

If you have been trying to play around with sound and video in Silverlight 2.0 you might have run into a problem where the media does not play the first time you call the play function.

The problem is that the media element might not be loaded and ready by the time you call play.

To solve the problem you can use the MediaElement MediaOpened Event that occurs when the media has been loaded and ready to play.

Example:

{

  .....

  // assume we have a sound/video media element called me

  MediaElement me;

  .....

  // play does not always work since media is not ready

  //me.Play();

  // add MediaOpened event handler

  me.MediaOpened += new RoutedEventHandler(me_MediaOpened);

}

// the event handler can play the sound/video since its ready to play

void me_MediaOpened(object sender, RoutedEventArgs e)

{

    ((MediaElement)sender).Play();

}

 

Tags:

Silverlight | Media

Comments

4/26/2008 9:56:57 AM #

trackback

Trackback from DotNetKicks.com

How to solve the MediaElement does not play problem in Silverlight 2

DotNetKicks.com

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



About the author

Name of author

My name is Bacon…James Bacon.

I am an API wars veteran. I was wounded by x86 assembly, recovered and moved on to C. Following a long addiction to C++ and a short stint at rehab I decided to switch to a healthier addiction so I am now happily sniffing .NET and getting hooked on Silverlight.

I am mainly here to ramble about coding, various API’s, Junkies(me especially) and everything else that happens between coders and their significant other.