SoFunction
Updated on 2025-03-07

C# Use WPF to achieve video loop playback using MediaElement control

In WPF, use the MediaElement control to realize a program that plays a single video loop, and can control the playback, pause and stop of the video.

One way to use events to automatically replay after the video playback is finished;

Another way is to use a WPF timer to write video playback code in the timer event.

The latter advantage is that it can control the loop duration and you don’t have to wait until the video playback is over before the next playback can be started. For example, starting multiple playback programs at the same time, so that multiple videos with different durations can be played simultaneously, and looped infinitely. If the first method is adopted, the video content cannot be synchronized after accumulating multiple automatic playbacks.

The first method:

XAML:
    <MediaElement x:Name="mediaElement" HorizontalAlignment="Left" Height="261" VerticalAlignment="Top" Width="507"/>
    <Button x:Name="btnPlay" Content="Play" HorizontalAlignment="Left" Margin="68,279,0,0" VerticalAlignment="Top" Width="75" Click="btnPlay_Click"/>
    <Button x:Name="btnPause" Content="Pause" HorizontalAlignment="Left" Margin="170,279,0,0" VerticalAlignment="Top" Width="75" Click="btnPause_Click"/>
    <Button x:Name="btnStop" Content="Stop" HorizontalAlignment="Left" Margin="295,279,0,0" VerticalAlignment="Top" Width="75" Click="btnStop_Click"/>
C#:
    // Window loading event    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
      // Bind video file       = new Uri("D:/bird.mp4");
      // Interactive control       = ;
      // Add element loading completion event -- automatically start playback       += new RoutedEventHandler(media_Loaded); 
      // Add media playback end event -- replay       += new RoutedEventHandler(media_MediaEnded);
      // Add element uninstall completion event -- Stop playback       += new RoutedEventHandler(media_Unloaded);
    }
    /*
       Element Events
     */
    private void media_Loaded(object sender, RoutedEventArgs e)
    {
      (sender as MediaElement).Play();
    }
    private void media_MediaEnded(object sender, RoutedEventArgs e)
    {
      // MediaElement needs to stop playing before starting playing.      // Otherwise it will stop at the last frame      (sender as MediaElement).Stop();
      (sender as MediaElement).Play();
    }
    private void media_Unloaded(object sender, RoutedEventArgs e)
    {
      (sender as MediaElement).Stop();
    }
    /*
       Click event of playback control button
     */
    private void btnPlay_Click(object sender, RoutedEventArgs e)
    {
      ();
    }
    private void btnPause_Click(object sender, RoutedEventArgs e)
    {
      ();
    }
    private void btnStop_Click(object sender, RoutedEventArgs e)
    {
      ();
    }

The second method:

Note: Using DispatcherTimer, you need to add a namespace.

XAML:
    <MediaElement x:Name="mediaElement" HorizontalAlignment="Left" Height="243" Margin="19,10,0,0" VerticalAlignment="Top" Width="394" LoadedBehavior ="Manual"/>
    <Button x:Name="btnPlay" Content="Play" HorizontalAlignment="Left" Margin="52,270,0,0" VerticalAlignment="Top" Width="75" Click="btnPlay_Click"/>
    <Button x:Name="btnPause" Content="Pause" HorizontalAlignment="Left" Margin="163,270,0,0" VerticalAlignment="Top" Width="75" Click="btnPause_Click"/>
    <Button x:Name="btnStop" Content="Stop" HorizontalAlignment="Left" Margin="266,270,0,0" VerticalAlignment="Top" Width="75" Click="btnStop_Click"/>
C#:
    DispatcherTimer timer = new DispatcherTimer(); // Timer timer    int durTime = 5; // The video playback time is the cycle period    // Window loading event    private void Window_Loaded(object sender, RoutedEventArgs e) 
    {
       = new Uri("D:/bird.mp4"); // Bind video file      (); // Set start playback       = new TimeSpan(0, 0, 0, durTime); // Set the timer repetition period       += new EventHandler(timerEvent); // Set timer event      (); // Start the timer    }
    // Timer event    public void timerEvent(object sender, EventArgs e)
    {
      // MediaElement needs to stop playing before starting playing.      // Otherwise it will stop at the last frame      (); 
      (); 
    }
    /*
       Click event of playback control button
     */
    private void btnPlay_Click(object sender, RoutedEventArgs e)
    {
      (); // Start playing      (); // Restart the timer    }
    private void btnPause_Click(object sender, RoutedEventArgs e)
    {
      (); // Pause the current playback      (); // Stop the timer    }
    private void btnStop_Click(object sender, RoutedEventArgs e)
    {
      (); // Stop the current playback      (); // Stop the timer    }

Summarize

The above is the editor’s introduction to C# using WPF to realize video loop playback using MediaElement control. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!