循环时 UWP MediaPlayer 上的内存泄漏

Memory Leak on UWP MediaPlayer when looping

我们目前正在使用 MediaPlayerElement 在 UWP 应用程序中播放 4K h.265 视频作为背景。我们使用 IsLoopingEnabled = true 将 MediaPlayer 设置为无限循环。问题是,每次视频循环时内存使用量都在增加。如果我们禁用循环,则不会发生内存泄漏。我们尝试通过在视频结束时将位置重置为零来手动循环播放视频,但它仍然会泄漏内存。我们还尝试调用 System.GC.collect() 但也没有任何作用。 这是 UWP 错误还是我们遗漏了什么?

编辑:

这是我们使用的代码:

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

    [...]
}

我们不再对 MediaSource 或 mediaPlayer 本身做任何事情。当我们禁用 IsLoopingEnabled 时,不再发生内存泄漏。

WPF 中的不同媒体播放器元素数量总是令人困惑。您正在使用 MediaPlayerElement。它的文档明确提到它仍处于开放的 Beta 阶段:

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.

所以您完全有可能只是发现了一个错误。

据我所知,在 MPE 之前播放媒体的方法是 Storyboard 和 MediaPlayer/MediaElement combination. As far as I can tell, MediaPlayer has no repeat mode. So a event will have to do。有更深WPF/UWP知识的人将不得不告诉你暂时是正确的。

如此处所述 这似乎是 Windows 10 中的错误。甚至内置视频播放器 "Movies and TV" 也有此问题。这似乎只发生在 h.265 编解码器上。也许是解码器驱动程序错误或类似问题。