简体   繁体   中英

Why does Play() on media element reset or otherwise ignore set position?

I want to pause and play a mp3 using 2 buttons like this:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    TimeSpan time_input = media.Position;
    media.Pause();
}

private void Button_Click_2(object sender, RoutedEventArgs e)
{
    media.Play();
    media.Position = time_input;
}

but clicking the second button plays media from beginning instead of from time_input span why?

In Button_Click_1 method TimeSpan time_input = media.Position; will create a new time_input variable within the scope of that method. You won't be able to use it in the other method.

private TimeSpan time_input = new TimeSpan(0);
private void Button_Click_1(object sender, RoutedEventArgs e)
{
    time_input = media.Position;
    media.Pause();
}

private void Button_Click_2(object sender, RoutedEventArgs e)
{
    media.Play();
    media.Position = time_input;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM