Show first frame of video in WPF MediaElement

你离开我真会死。 提交于 2019-11-27 19:12:01

问题


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?


回答1:


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 wont 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 rember 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.




回答2:


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

<MediaElement Source="foo.wmv" 
              MediaOpened="videoElement_MediaOpened" 
              LoadedBehavior="Pause" ScrubbingEnabled="True" />



回答3:


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!



来源:https://stackoverflow.com/questions/1346886/show-first-frame-of-video-in-wpf-mediaelement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!