Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
648 views
in Technique[技术] by (71.8m points)

.net - Show first frame of video in WPF MediaElement

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...