开发者

Change mediaelement Source Based on Actions

I have a WPF 4 and VB.net 2010 project. I am playing videos in a single mediaelement. This is what I need to do:

When the window first opens, I have the first video play just fine. However, it is after this video plays that I run into trouble figuring out how to do the following.

  1. I need the video source to change immediately following a single play through of any video, and I need this video (henceforth referred to as an "ambient" video) to loop forever.

  2. When a certain event happens, I need to change the video source again, have it play once through, and then go back to looping ambient video in step 1.

Here is the rub, however. Many of the video triggers are inside of If-Then or Select Case statements in code behind, so I'm not exclusively using simple WPF events such as "MouseUp" or "MouseEnter".

Also, all videos must play in the same mediaelement, for performanc开发者_如何学Ce reasons.

Thank you in advance!

How do I do this?


The Source of the media element is a DependencyProperty, as such any changes to it will be immediately reflected in the UI.

If you combine this with the MediaEnded event that is fired, you can set the Source and your problem is solved.

When you hit the triggers in code, you can either call a method or fire an event. You will have to use some semblance of a State Pattern to deal with the other logic. As an aside, check out Programming Without Ifs, it's an awesome intro on how to avoid insane conditional logic.


I set the mediaelement's LoadedBehavior to "Play" and Unloadedbehavior to "Stop", then I was able to just change the source of the mediaelement itself in code, and put the video I needed played after every video into the MediaEnded event.

It turns out that MediaEnded does not fire automatically when the LoadedBehavior is set to "Manual", unless "Stop" is explicitly called in code.

I hate accepting my own answers, so Nate Noonen gets the bounty (he was going down the right alley originally)! TY!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜