After playing a MediaElement, how can I play it again?

Edward Tanguay picture Edward Tanguay · Mar 22, 2010 · Viewed 7.9k times · Source

I have a variable MediaElement variable named TestAudio in my Silverlight app.

When I click the button, it plays the audio correctly.

But when I click the button again, it does not play the audio.

How can I make the MediaElement play a second time?

None of the tries below to put position back to 0 worked:

private void Button_Click_PlayTest(object sender, RoutedEventArgs e)
{
    //TestAudio.Position = new TimeSpan(0, 0, 0);
    //TestAudio.Position = TestAudio.Position.Add(new TimeSpan(0, 0, 0));
    //TestAudio.Position = new TimeSpan(0, 0, 0, 0, 0);
    //TestAudio.Position = TimeSpan.Zero;

    TestAudio.Play();
}

Answer

Edward Tanguay picture Edward Tanguay · Mar 22, 2010

I found it, you just have to stop the audio first, then set the position:

TestAudio.Stop();
TestAudio.Position = TimeSpan.Zero;