locked
Application with MediaElement hangs after playing video about 60 times

    Question

  • I'm making an application which plays lots of videos in a row. However it seems like whenever I hit 60 videos (sometimes less, sometimes a bit more) my app hangs. I made a small sample app which also does this so to me it seems like the problem is within MediaElement control or somewhere deeper.

    Here is the code for sample app:

    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>
    
    



    Code behind:

    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";
            }
        }
    }
    

    To test put one wmv file into TempState folder. Preferably short video as it requires to run tens of times to reproduce issue. I used a countdown clip from this site: http://www.movietools.info/video-background-loops/countdown-loops.html

    Any ideas what's the issue or how to work around it?

    Sunday, November 16, 2014 3:23 PM

Answers

All replies

  • I tested couple of applications in Windows Store and they all had the same problem when playing about 60 videos in a row.

    I'll try VLC player next as it doesn't seem to use regular MediaElement to play video. As it is open source, maybe I could use it as a base for playing videos if that doesn't hang after multiple videos are played (gotta check license).

    Monday, November 17, 2014 10:41 AM
  • Does you test your app using Performance and Diagnostics? I think it maybe some memory issue.
    Monday, November 17, 2014 3:45 PM
  • Thank you for the feedback.

    I ran app using Performance and Diagnostics to check for memory problems. It seems that the memory is increasing from around 80MB to 116MB during the lifespan of the app. Managed memory is only a few KBs as the code is using RandomAccessStream which is unmanaged underneath. Once the video gets stuck the memory stays at the 116MB until I close the program.

    Hard to say if the actual problem is this memory increment thing as 116MB isn't that much and the PC which I have used for testing has 4GB and other PC I have tried has 16GB. But that is of course an option.

    I tried disposing the file stream at MediaEnded event handler but that didn't lower memory usage. Any ideas?

    Wednesday, November 19, 2014 8:00 AM
  • The problem seemed to be related to AMD graphics card and seems to be fixed after updating drivers to latest version.
    Thursday, November 20, 2014 12:01 PM