18

I have a WPF-application with a MediaElement which I use to run a video. I don't want the video to autoplay when loaded, so I set the LoadedBehavior to Manual.

<MediaElement LoadedBehavior="Manual" 
              Source="foo.wmv" 
              MediaOpened="videoElement_MediaOpened" />

However; I want the element to show the first frame of the video when loaded. Is there any magic way of doing this?

stiank81
  • 25,418
  • 43
  • 131
  • 202

3 Answers3

27

You also need

ScrubbingEnabled="True"

Then set an event on Load

Loaded="Video1_Loaded"

In the Video1_Load method add the following:

Video1.Play();
Video1.Pause();

None of the video content is rendered before the play method is called. So even if you set the position past the first frame you won't get any data rendered.

I think the MediaElement.ScrubbingEnabled Property is the key.

Gets or sets a value that indicates whether the MediaElement will update frames for seek operations while paused. This is a dependency property.

I tried the same approach without ScrubbingEnabled but found that the first frame wouldn't render. Not sure if it's just the hardware I am using for this. The other item to remember is that the way the MediaElement works, once you call Play() you may not actually Pause() on the first frame exactly, so you may want to reduce the volume on the stream before calling Play() and reset after calling Pause().

Dhruva N
  • 94
  • 1
  • 11
Sebastian Gray
  • 2,590
  • 3
  • 26
  • 36
  • 1
    Thx - this works! I must say I'm not all comfortable with the Play() Pause() trigging. Saw someone else suggesting it too, but found the approach a bit "hacky". But if this is the only way I guess we'll have to live with it. ScrubbingEnabled is a nice property, but doesn't seem to have anything to do with showing the first video frame on startup. – stiank81 Aug 30 '09 at 17:35
  • Using the `MediaOpened` event instead of `Loaded` works for me, as long as I play then pause in the event handler. – RandomEngy Sep 12 '20 at 22:11
9

You don't have to create an event, see following

<MediaElement Source="foo.wmv" 
              MediaOpened="videoElement_MediaOpened" 
              LoadedBehavior="Pause" ScrubbingEnabled="True" />
Ahmed Cogenli
  • 99
  • 1
  • 1
  • you can't controls the MediaElement with play, stop ... So you must set LoadedBehavior to manual. if you set it to pause how would you start it to play? – Alexander Zwitbaum May 16 '12 at 12:19
  • @AlexanderZwitbaum: By calling the `Play` method based on some other event, unrelated to the integrated buttons? – O. R. Mapper Mar 20 '15 at 16:56
2

I can't comment, so here is my answer: Scrubbing refers to updating the rendered image when the MediaElement is paused.

//Todo: Set your source

//if not stated in the xmal, you can still set this here.
Player.LoadedBehavior = MediaState.Manual;

//Turn scrubbing on
Player.ScrubbingEnabled = true;

Player.Pause();

Player.Position = TimeSpan.FromTicks(1);

Thanks for the scrubbing tip!

Invalid
  • 21
  • 1