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

by APIJunkie 4/26/2008 9:44:00 AM

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();

}

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

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





Live preview

9/5/2008 5:04:50 PM

Powered by BlogEngine.NET 1.2.0.0
Theme by Mads Kristensen

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. I am currently stuck in C++ and sniffing .NET.

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.

E-mail me Send mail


Calendar

<<  September 2008  >>
MoTuWeThFrSaSu
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

View posts in large calendar

Pages

    Recent comments

    Authors

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2008

    Sign in