How to avoid MediaElement.Source and Play race condition?


  • I've got a mediaElement.  I also have some events that need to occur as soon as it's done loading and some others that need to happen right before it finishes.  So I've created a timer to take place in the future.  Here is my trouble. 

    VideoME.source = "//URI.mp4";

    If at this point I do something like

    TimerObj.Interval = VideoME.NaturalDuration.Timespan - (new Timespan(0,0,5,0));

    Half of the time the VideoME.source won't be done loading yet and the VideoME.NaturalDuration == (0,0,0,0).

    So my first question is, how do I do an "await" on the .source for the source to be loaded before proceeding? 

    I have a similar case where I load a source and then much later (avoiding any loading race scenario) I do:

    TimerObj.Interval = VideoME.NaturalDuration.Timespan - (new Timespan(0,0,1,0));

    If the .Play takes a couple seconds to start my Timer will be a couple seconds off. 

    Is there an async equivalent to both commands that will return a Task when they complete? 

    Saturday, August 9, 2014 12:07 AM