0

We are currently playing a 4K h.265 video in an UWP app as background using MediaPlayerElement. We have set the MediaPlayer to infinite loop using IsLoopingEnabled = true. The problem is, that the memory usage is increasing every time the video loops. If we disable looping, the memory leak does not occure. We tried looping the video manually by resetting the position to zero when the video finished, but still it leaks memory. We also tried to call System.GC.collect() but that also did nothing. Is this a UWP bug or are we missing something?

Edit:

Here is the code we are using:

MainPage.xaml

<Page
x:Class="MyProject.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="using:MyApp.Controls"
xmlns:xaml="using:Microsoft.Graphics.Canvas.UI.Xaml"
mc:Ignorable="d"
Background="Black" Loaded="MainPage_OnLoaded" Unloaded="MainPage_OnUnloaded">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="{StaticResource AppBrushNewBlue1}">
    <MediaPlayerElement Name="bgMovie" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AreTransportControlsEnabled="False" ></MediaPlayerElement>
    <Canvas Name="mainCanvas" ManipulationMode="None" Background="Transparent">
    </Canvas> 
</Grid>

MainPage.xaml.cs

private MediaSource ms;
private async void MainPage_OnLoaded(object sender, RoutedEventArgs e)
{
    ms = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/bg_animation_V2.mp4"));
    bgMovie.Source = ms;
    bgMovie.MediaPlayer.IsLoopingEnabled = true;
    bgMovie.MediaPlayer.Play();

    [...]
}

We are not doing anything more with the MediaSource or the mediaPlayer itself. When we disable IsLoopingEnabled no memory leak occurs anymore.

C0dR
  • 320
  • 1
  • 6
  • 23
  • Please show us the actuall code that is loading/assigning the video to the Media Player and the manual looping. In particular, what is the Video source? Do you add a reference to the video to any Collection? – Christopher Nov 29 '19 at 17:00
  • If GC.Collect() does nothing, you either have a Unamanged resource that is not cleaned up, or a Reference memory leak, or a genuine memory leak. But the former two are really a few orders more likelyx. – Christopher Nov 29 '19 at 17:05
  • I've added some code we are using. The video source is a h.265 mp4 file, nothing special. We are not adding a reference or anything to a collection – C0dR Dec 02 '19 at 08:56

2 Answers2

1

As stated here https://stackoverflow.com/a/54947557/1018232 this seems to be a bug in Windows 10. Even the builtin video player "Movies and TV" has this issue. It seems this only happens on h.265 codec. Maybe it is a decoder driver bug or something like this.

C0dR
  • 320
  • 1
  • 6
  • 23
0

There is always a confusing amount of different Media Player elements in WPF. You are using teh MediaPlayerElement. And it's documentation explicitly mentions that it is still in a open Beta-ish phase:

This control is currently available as a developer preview for Windows 10, version 1903, and later. Although we encourage you to try out this control in your own prototype code now, we do not recommend that you use it in production code at this time. For more information, see the XAML Islands feature roadmap. If you have feedback about this control, create a new issue in the Microsoft.Toolkit.Win32 repo and leave your comments there. If you prefer to submit your feedback privately, you can send it to XamlIslandsFeedback@microsoft.com.

So it is entirely possible you just found a bug.

As far as I can tell, the approach to playing Media before the MPE is the Storyboard and the MediaPlayer/MediaElement combination. As far as I can tell, MediaPlayer has no repeat mode. So a event will have to do. Someone with deeper WPF/UWP knowledge will have to tell you wich one is the right one for the time being.

Christopher
  • 9,634
  • 2
  • 17
  • 31
  • Thank you, I have tried replacing the MediaPlayerElement with a MediaElement (https://learn.microsoft.com/de-de/uwp/api/Windows.UI.Xaml.Controls.MediaElement). The control has a property called `IsLooping`, but even with this control memory usage is rising constantly. I guess its a problem within the codec or media framework itself? – C0dR Dec 02 '19 at 16:03