I have a user control with a WPF MediaElement and an Image control. This user control is again used in a few more user control which uses this in two differnt layouts. The application is supposed to show a list of videos and images for a long period of time in loops. Each user control will be loaded for a particular time period and after that time, this will be unloaded and the next control will be loaded. When the videos are running every now and then the position of the video is changed to show a particular part of the video.
XAML Code. The MediaElement is inside a Grid with one column and one row.
<MediaElement Volume="0" Name="m1" Stretch="Fill" Visibility="Hidden" LoadedBehavior="Manual" UnloadedBehavior="Manual" />
C# Code, setting source
this.mediaElement.Source = item.Path; this.mediaElement.Position = new TimeSpan(0, 0, 0); this.mediaElement.Play();
C# Code changing position. This position change is done using a DispatcherTimer
t = TimeSpan.FromMilliseconds(Position); mediaElement.Position = t;
C# Code disposing the MediaElement. I use this code to dispose the MediaElement because previously I found out after playing for a while
the MediaElement was using too much memory and becomes frozen after a while.. The below code fixed that issue.
mediaElement.Close(); mediaElement = null; GC.Collect(); GC.WaitForPendingFinalizers();
The application works fine, but the problem is after some time (like 20 mins) the WPF MediaElement disappears. The application works
smoothly (It doesn't get frozen), the images are shown, but the MediaElement is gone.
When this problem occurred, I did some debugging and found these.
- The WPF MediaElement visibility is Visible. The Source has correct filenames. The MediaElement is not hidden behind any other controls.
- However the actual width and actual height are shown as 0.
I checked all the code to find out where the height and width becomes zero. But could not find any. So I decided to try and override the Height and Width evey millisecond using a DispatcherTimer (Just to check if it was possible). But even then the 0 in actual height and width remained the same.
Also, the application doesn't seem to use excessive memory and everything else seems to be fine.
Does anyone got a clue as to why this is happening? Any solutions or workarounds? Thanks