1

I have a WPF application which dynamically loads video or image files(depending on user's choice, to a MediaElement control. It is working fine when it is a video and gets the MediaEnded event fired on ending the video. But when I load an image, the MediaEndedevent is fired within 5 seconds. Is it a default value? or can I change it programmatically? Is there any property to change this interval or disable such an option? Is it possible to make it paused until a specific action?

I have set the following properties as follows

MediaControl1.LoadedBehavior = MediaState.Manual;
MediaControl1.UnloadedBehavior = MediaState.Manual;
JijeshKV
  • 670
  • 2
  • 7
  • 26

2 Answers2

2

MediaElement is a (very thin) wrapper around Windows Media Player (or rather - uses the same framework which is used by Windows Media Player). If you open an image in Windows Media Player - you will see it will "play" it like a slideshow (even for 1 image), for about 5 seconds. That's why you get MediaEnded event in 5 seconds - Windows Media Player plays slideshow with your image for that duration. I doubt there is a way to change this from WPF (because it's behavior of external program\framework, not related to MediaElement itself) and I'm not aware of the way to change this for Windows Media Player (and even if there is such a way - it will have global effect and you probably don't want to modify your clients computer in such a way).

To solve your problem - just don't use MediaElement for displaying images - use something like Image control. If you have really strong reasons to do that - you can pause MediaElement with Pause method after your "slideshow" has been loaded, then it will not fire MediaEnded event. All in all - I cannot imagine any use case where you really have to use MediaElement for images.

Evk
  • 98,527
  • 8
  • 141
  • 191
  • Definitely not WMP. This is all done by MilCore, surely it is wrapping Media Foundation. I suspect it comes [from this](https://msdn.microsoft.com/en-us/library/windows/desktop/bb787399(v=vs.85).aspx) with UseValueForDefault = True and Medium. But too hard to reverse-engineer. – Hans Passant Oct 08 '17 at 15:49
0

You can repeat the media when you load image. Handle MediaEnded event like this:

void me_MediaEnded(object sender, EventArgs e)
{
    //play video again
    mediaElement.Position = new TimeSpan(0, 0, 1);
    mediaElement.Play();
}

Additionally see this. It may help: https://stackoverflow.com/a/3406857/5675763

Mahdi Ataollahi
  • 4,544
  • 4
  • 30
  • 38