Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MediaElement not displaying

Tags:

.net

wpf

I've placed a MediaElement on a newly created window in an application we're building, set the source of it a wmv (also tried it with a gif), but it doesn't show up on the screen when I run it.

I then created a new separate project and it all works as expected.

this is the code that works:

<MediaElement Source="pre-loader.wmv" LoadedBehavior="Manual" Name="Video" />

and this is the code that doesn't work:

<MediaElement Source="pre-loader.wmv" LoadedBehavior="Manual" Name="Video" />

I know, its the same... I dont get it?

I have tried a couple of different ways but I get the same result. The test project works main application doesn't.

I am using the same wmv file in both projects, I also made sure that in both cases the Copy to Output dir. is set to Copy if Newer.

Any help would be... well, helpfull

Update: I have tried looking for the MediaElement using snoop and the ActualWidth, ActualHeigt are both 0 but the height property is 140 and the width property is NAN. Also, the source = Default.

I'm also getting "Error occured loading preview" when I look at the screen in Blend, but it doesnt let me what the error is.

like image 695
Organ Grinding Monkey Avatar asked Feb 15 '11 18:02

Organ Grinding Monkey


1 Answers

The first place to look is in your project resources. The "Source" property indicates that you are getting the video from a relative URI, which only works when the video is located in your project's files.

MediaElement by default has a silent fail, meaning it will not throw an exception unless you program it to specifically. This is usually set with some sort of action in the Mediaelement's "MediaFailed" (VB)/"OnMediaFailed" (C#) property in code behind. I usually put a messagebox into this event to tell the user that the video could not load.

In the project that you are having the problems in, the first thing you need to do is go into the project properties, go to the "Resources" tab, and import the video as a resource. Then, in the Solution Explorer, click on the video (in the Resources folder) and (as you have done) set its properties to BuildAction = None and CopyToOutput = Copy if newer. Do NOT set it to "Embedded Resource", or the MediaElement will not be able to play it. It HAS to be set to "None".

Then, change the mediaelement source to "Resources/pre-loader.wmv"

The other (rather obvious) piossiblity is checking if you are calling the mediaelement.play function in your code behind. If you don't need to explicitly control the play/pause/stop functions, I would set the the LoadedBehavior to "play".

Hope that helps! Comment if you have any more questions.

like image 93
CodeMouse92 Avatar answered Oct 22 '22 02:10

CodeMouse92