Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping MediaElement freezes after some minutes or halts for a second

I have a .net framwork (4.7.2) wpf application in which a video (duration: 10 to 50 seconds) should be looped all day long.

First I have implemented the way which is suggested by mirosoft (using a story board): How to: Repeat Media Playback (learn.microsoft.com)

<MediaElement Name="VideoMediaElement" Width="1920" Height="1080" Visibility="{Binding Path=IsVideoUriAvailable, Converter={StaticResource BooleanToVisibilityConverter}}">
    <MediaElement.Triggers>
        <EventTrigger RoutedEvent="MediaElement.Loaded">
            <EventTrigger.Actions>
                <BeginStoryboard>
                    <Storyboard>
                        <MediaTimeline Source="{Binding Path=VideoUri, FallbackValue=null}" Storyboard.TargetName="VideoMediaElement"
                                       RepeatBehavior="Forever" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger.Actions>
        </EventTrigger>
    </MediaElement.Triggers>
</MediaElement>

But on my test I noticed that the video feezes after 3 to 5 minutes.
I found this article, which descripes the same problem.
The solution: Using a 3rd party library or manage the repetition yourself.

I want to avoid using the suggested library, because it looks like it is no longer maintained. So I implemented the (very simple) solution.

<MediaElement Name="VideoMediaElement"
              Width="1920" Height="1080" 
              Visibility="{Binding Path=IsVideoUriAvailable, Converter={StaticResource BooleanToVisibilityConverter}}"
              Source="{Binding Path=VideoUri, FallbackValue=null}"
              LoadedBehavior="Manual"
              Loaded="Video_OnLoaded"
              MediaEnded="Video_OnMediaEnded"/>
private void Video_OnLoaded(object sender, RoutedEventArgs e)
{
    VideoMediaElement.Play();
}

private void Video_OnMediaEnded(object sender, RoutedEventArgs e)
{
    VideoMediaElement.Position = TimeSpan.FromSeconds(0);
}

It seems this implementation is more reliable, but I have still one problem: the video stops for like one second when OnMediaEnded is fired. After that short halt the video start at the beginning.

Do you have any suggestions how to fix this short halt?

like image 439
D. Weber Avatar asked Nov 25 '25 13:11

D. Weber


1 Answers

private void Video_OnLoaded(object sender, RoutedEventArgs e)
{
    VideoMediaElement.Play();
}

private void Video_OnMediaEnded(object sender, RoutedEventArgs e)
{
    VideoMediaElement.Position = TimeSpan.FromSeconds(0);
    VideoMediaElement.Play();
}
like image 126
Byoung Sam Moon Avatar answered Nov 27 '25 03:11

Byoung Sam Moon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!