It is obvious what I am trying to do below, but I get the following exception:
Unable to return a TimeSpan property value for a Duration value of 'Automatic'.
I was discouraged to read that
NaturalDuration cannot be determined until after MediaOpened has occurred. (link)
Does this mean that I have to come up with a contrived method to open the file, wait for the media opened event in a separate thread, then return the duration only after the event has fired?
public static int PlayAudio(string fileName)
{
try
{
myMediaPlayer.Stop();
myMediaPlayer.Close();
myMediaPlayer.Open(new Uri(filename));
myMediaPlayer.Play();
return myMediaPlayer.NaturalDuration.TimeSpan.Milliseconds;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
return -1;
}
}