【WPF】使用控件MediaElement播放視頻


需求是點擊按鈕后,彈出彈窗播放視頻。按鈕的點擊事件如下。

public void ShowVideo()
{
    Window window = new Window();
    window.Width = 800;
    window.Height = 600;
    
    // 控制彈出位置在屏幕正中
    double screenHeight = SystemParameters.FullPrimaryScreenHeight;
    double screenWidth = SystemParameters.FullPrimaryScreenWidth;
    window.Top = (screenHeight - window.Height) / 2;
    window.Left = (screenWidth - window.Width) / 2;

    MediaElement player = new MediaElement();
    //player.Margin = new Thickness(1, 1, 1, 1);
    player.Width = 800;
    player.Height = 600;

  // 視頻資源放在Debug\bin目錄下
var mp4_path = AppDomain.CurrentDomain.BaseDirectory + @"\video.mp4"; player.Source = new Uri(mp4_path, UriKind.RelativeOrAbsolute); player.LoadedBehavior = MediaState.Manual; player.Stop(); player.Play(); // 視頻播放控件加入到窗體中 window.Content = player; window.ShowDialog(); }

運行效果如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM