I want to play multiple videos in a loop one after other on a single mediaElement control. Also i want that when the current video transit to other video then i shouldn't get black screen.

Vishal2 Bansal 185 Reputation points
2023-11-08T05:58:48.8866667+00:00

I want to play multiple videos in a loop one after the other on a single mediaElement control. Also i want that when the current video transits to another video then i shouldn't get black screen in between on mediaelement.

So I am curious if such playlist store type method is available for mediaElement?

I searched on internet. I couldn't able to found out any solutions

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,809 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,193 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,601 Reputation points Microsoft Vendor
    2023-11-09T07:21:25.9633333+00:00

    Hi,@ Vishal2 Bansal. Welcome to Microsoft Q&A Forum.

    To ensure smoother transitions between videos, you could try to set the MediaElement to be transparent during transitions.

    
     <Grid Margin="50" Width="400" Height="200" HorizontalAlignment="Left" VerticalAlignment="Top">
                <MediaElement x:Name="mediaElement" LoadedBehavior="Manual" MediaEnded="MediaElement_MediaEnded" />
            </Grid>
    
    
    

    Codebedhind:

      private List<string> videoPaths = new List<string>
            {
                "C:\\...\\MediaElementDemo\\10MB_WMV.wmv" ,
                "C:\\...\\MediaElementDemo\\20.wmv",
               "C:\\...\\MediaElementDemo\\5MB_WMV.wmv"
              
            };
    
            private int currentVideoIndex = 0;
    
            public MainWindow()
            {
                InitializeComponent();
                PlayCurrentVideo();
            }
    
            private void PlayCurrentVideo()
            {
                var currentVideoPath = videoPaths[currentVideoIndex];
                mediaElement.Source = new Uri(currentVideoPath, UriKind.RelativeOrAbsolute);
                mediaElement.Play();
            }
    
    
            private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
            {
                CrossfadeToNextVideo();
            }
    
            private void CrossfadeToNextVideo()
            {
                var nextVideoIndex = (currentVideoIndex + 1) % videoPaths.Count;
                var nextVideoPath = videoPaths[nextVideoIndex];
    
                var fadeInDuration = TimeSpan.FromSeconds(1);
                var fadeOutDuration = TimeSpan.FromSeconds(1);
    
                var fadeOutAnimation = new DoubleAnimation(1, 0, fadeOutDuration);
                fadeOutAnimation.Completed += (s, e) =>
                {
                    mediaElement.Source = new Uri(nextVideoPath, UriKind.RelativeOrAbsolute);
                    mediaElement.Position = TimeSpan.Zero;
                    mediaElement.Play();
                    var fadeInAnimation = new DoubleAnimation(0, 1, fadeInDuration);
                    mediaElement.BeginAnimation(UIElement.OpacityProperty, fadeInAnimation);
                };
    
                mediaElement.BeginAnimation(UIElement.OpacityProperty, fadeOutAnimation);
                currentVideoIndex = nextVideoIndex;
            }
    
    
    

    The result:

    1

    Or you can also see if the solution here helps you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. Viorel 118.9K Reputation points
    2023-11-08T11:23:21.02+00:00

    For example, use a media element and a black rectangle:

    <Window . . .>
        <Grid>
            <Grid Margin="50" Width="400" Height="200" HorizontalAlignment="Left" VerticalAlignment="Top">
                <MediaElement x:Name="mediaElement" LoadedBehavior="Play" UnloadedBehavior="Stop" MediaOpened="mediaElement_MediaOpened" MediaEnded="mediaElement_MediaEnded" Visibility="Hidden" />
                <Rectangle x:Name="br" Fill="Black" Visibility="Visible"/>
            </Grid>
    
            <Button x:Name="button" Content="START" HorizontalAlignment="Left" Margin="500,50,0,0" VerticalAlignment="Top" Click="button_Click"/>
    
        </Grid>
    </Window>
    

    The code is:

    Uri[] files = new[]
    {
        new Uri( @"C:\MyFolder\MyFile1.wmv" ),
        new Uri( @"C:\MyFolder\MyFile2.wmv" ),
        new Uri( @"C:\MyFolder\MyFile3.wmv" ),
    };
    
    int index = 0;
    
    void StartNext( )
    {
        if( index < 0 || index >= files.Length ) return;
    
        mediaElement.Source = files[index];
    }
    
    private void button_Click( object sender, RoutedEventArgs e )
    {
        index = 0;
        StartNext( );
    }
    
    private void mediaElement_MediaOpened( object sender, RoutedEventArgs e )
    {
        mediaElement.Visibility = Visibility.Visible;
        br.Visibility = Visibility.Hidden;
    }
    
    private async void mediaElement_MediaEnded( object sender, RoutedEventArgs e )
    {
        mediaElement.Visibility = Visibility.Hidden;
        br.Visibility = Visibility.Visible;
    
        if( ++index >= files.Length ) return;
    
        await Task.Delay( TimeSpan.FromSeconds( 2 ) );
    
        StartNext( );
    }
    

    The files array is a playlist.

    0 comments No comments

  2. Castorix31 86,491 Reputation points
    2023-11-09T08:44:44.5833333+00:00

    You should use MediaComposition for that...

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.