播放约60个视频后,WinRT MediaElement挂起

时间:2014-11-17 11:02:45

标签: windows-runtime windows-store-apps winrt-xaml

我正在制作一个连续播放大量视频的应用程序。然而,似乎每当我点击60个视频(有时更少,有时更多)我的应用程序挂起。我做了一个小样本应用程序也这样做,所以对我来说似乎问题是在MediaElement控件内或更深的地方。

以下是示例应用的代码:

MainPage.xaml中:

<Page
    x:Class="App2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App2"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <MediaElement x:Name="player" AutoPlay="False" MediaOpened="player_MediaOpened" MediaEnded="player_MediaEnded" Volume="0.01" />
        <TextBlock x:Name="txtDebug" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="40" FontSize="20" Text="Videos played: 0" Foreground="Yellow" />
        <Button Content="Test" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="40,100,0,0" Click="Button_Click" />
    </Grid>
</Page>

代码背后:

namespace App2
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    protected override async void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        await SetPlayerVideo();
    }

    private async System.Threading.Tasks.Task SetPlayerVideo()
    {
        // get next video file, open it and set it to MediaElement.
        var nextVideo = await this.SelectNextVideo();
        var videoStream = await nextVideo.OpenAsync(Windows.Storage.FileAccessMode.Read);
        this.player.SetSource(videoStream, "");
    }

    private void player_MediaOpened(object sender, RoutedEventArgs e)
    {
        // When MediaElement.SetSource finishes, begin play.
        MediaElement elem = sender as MediaElement;
        elem.Play();
    }

    private async void player_MediaEnded(object sender, RoutedEventArgs e)
    {
        // Update debug text and start next video when one ends.
        this.txtDebug.Text = string.Format("Videos played: {0}", this.videoIndex);
        await SetPlayerVideo();
    }

    private int videoIndex = 0; // Index used to loop through files in temp folder in case there are multiple video files

    private async System.Threading.Tasks.Task<Windows.Storage.StorageFile> SelectNextVideo()
    {
        var files = await Windows.Storage.ApplicationData.Current.TemporaryFolder.GetFilesAsync();
        var file = files.ElementAt(this.videoIndex % files.Count);
        videoIndex++;
        return file;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        txtDebug.Text = "button click";
    }
}
}

测试将一个wmv文件放入TempState文件夹。最好是短视频,因为它需要运行数十次才能重现问题。我使用了此网站的倒计时剪辑:http://www.movietools.info/video-background-loops/countdown-loops.html

我从Windows应用商店下载了一些带有播放列表功能的应用程序,添加了大约100x 10秒的剪辑并点击播放。他们都有同样的问题,在播放了大约60个视频后被绞死。

如何解决问题的任何建议或想法?我每次视频结束时都尝试创建新的MediaElement,但它没有帮助。

接下来我可能会研究一下VLC播放器,因为他们似乎没有使用默认的MediaElement来播放视频。但我还没有真正检查他们的许可是否允许在我的应用程序中使用他们的dll,或者我是否可以编译源代码。

1 个答案:

答案 0 :(得分:0)

问题似乎与AMD显卡有关,似乎在将驱动程序更新到最新版本后得到修复。