Jay Bazuzi
Jay Bazuzi

Reputation: 46506

How to smoothly play a sequence of videos with WPF's MediaElement?

I want to compose a sequence of videos at runtime, making the result look like one, smooth video.

This snippet shows my current approach:

void mediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
    if (_currentVideoIndex == this.videos.Length) 
        this._currentVideoIndex = 0;
    PlayNext();
}

void PlayNext()
{
    mediaElement.Source = new Uri(videos[_currentVideoIndex++]);
    mediaElement.Play();
}

The videos play fine, but the player goes blank for about half of a second between each video.

I'm not tied to a specific platform at this point, WPF / MediaElement is just where I'm starting.

Upvotes: 1

Views: 4075

Answers (1)

Jason
Jason

Reputation: 6926

You could try buffering the next video. So you'd always have two videos loaded - that way, when one is finished, you can instantly switch to the next already loaded MediaElement.

It's the same concept as the GDI+ flickering problem when you constantly draw and refresh - the solution for that is double-buffering the form. Without double buffering, GDI+ clears the form (this is the momentary flickering users see) and draws. With double buffering, GDI+ draws to a temporary screen, and then instantly updates it on the form, thereby eliminating flicker.

Your scenario calls for a similar solution. In a List videos, always keep two loaded (you can asynchronously load the second after loading the first to reduce lag time). This way, after the first video is done playing, you can immediately display the second MediaElement which is already laoded.

Upvotes: 3

Related Questions