WPF MediaElement with image as Source

548 Views Asked by At

The WPF MediaElement displays both videos and images. This seems nice, because I want to show a playlist containing both videos and images.

In the MediaEnded event handler, I change the source, which then triggers the playback of the next video.

<MediaElement Name="Player" Source="{Binding MediaSource}"  MediaEnded="Player_MediaEnded" />

private void Player_MediaEnded(object sender, RoutedEventArgs e)
{
    MediaSource = "the_next_file.jpg";
}

But... When setting the Source to an image file, this image is always displayed for 5 seconds.

Question

I want to define the number of seconds that image is visible. But there does not seem to be a way to do that.

Whatever I do... images are always displayed for 5 seconds

2

There are 2 best solutions below

1
Bron Davies On

Have you tried setting the MediaClock when an image is the source so that you can control how long it's displayed?

See https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.mediaelement.clock?view=windowsdesktop-7.0#system-windows-controls-mediaelement-clock

0
mm8 On

You could use a timer to change the Source after a specified period of time, e.g.:

private async void Player_MediaEnded(object sender, RoutedEventArgs e)
{
    Player.MediaEnded -= Player_MediaEnded;

    MediaSource = "pic1.jpg";
    await Task.Delay(TimeSpan.FromSeconds(10));
    MediaSource = "pic2.jpg";
}