WPF MediaPlayer: How to play in sequence, sync?

前端 未结 2 571
生来不讨喜
生来不讨喜 2021-01-17 02:17

I have this function:

public static void Play(string FileName, bool Async = false)
    {
        System.Windows.Media.MediaPlayer mp = new System.Windows.Med         


        
2条回答
  •  -上瘾入骨i
    2021-01-17 02:50

    I will share my Solution with you:

    I created a Window: WindowPlay.xaml

    
        
            
        
    
    

    WindowPlay.xaml.cs:

    using System;
    using System.Windows;
    
    namespace Sistema.Util
    {
        /// 
        /// Interaction logic for WindowPlay.xaml
        /// 
        public partial class WindowPlay : Window
        {
            public WindowPlay()
            {
                try
                {
                    InitializeComponent();
                }
                catch
                {
                    this.Close();
                }
            }
    
            /// 
            /// Plays the specified file name
            /// 
            /// The filename to play
            /// If True play in background and return immediatelly the control to the calling code. If False, Play and wait until finish to return control to calling code.
            public static void Play(Uri FileName, bool Async = false)
            {
                WindowPlay w = new WindowPlay();
    
                var mp = w.MediaElement1;
    
                if (mp == null)
                {
                    // pode estar sendo fechada a janela
                    return;
                }
                mp.Source = (FileName);
    
                if (Async)
                    w.Show();
                else
                    w.ShowDialog();
            }
    
            private void MediaElement1_MediaEnded(object sender, RoutedEventArgs e)
            {
                this.Close();
            }
    
        }
    }
    

提交回复
热议问题