2013-11-04 75 views
1

我試圖動態加載的WindowsMediaPlayer控制(目前音頻播放,不顯示視頻),但我不能讓它正常工作。C#添加WMP動態形成:

的代碼是,似乎播放音頻(也可能是視頻),但不顯示窗體上的視頻。表單保持空白,音頻播放。 (播放WVM文件,不能成爲編碼器問題)我正在導入WMPLib。

WindowsMediaPlayer videoPlayer; 

    public void createContent(Form form) { 
     PlayFile("F:\\Videos\\CantTouchThis.wmv"); 
    } 

    private void PlayFile(string url) { 
     videoPlayer = new WindowsMediaPlayer(); 
     videoPlayer.PlayStateChange += 
    new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(Player_PlayStateChange); 
     videoPlayer.MediaError += 
      new WMPLib._WMPOCXEvents_MediaErrorEventHandler(Player_MediaError); 
     videoPlayer.URL = url; 
     videoPlayer.controls.play(); 
    } 

    private void Player_PlayStateChange(int NewState) { 
     if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsStopped) { 

     } 
    } 

    private void Player_MediaError(object pMediaObject) { 
     MessageBox.Show("Cannot play media file."); 

    } 

幫助獲得這項工作將不勝感激。

+0

順便說一句:試圖將其添加到這樣的形式: form.controls.add(錄像機);給出一個參數異常錯誤。 – Serellyn

回答

1

我找到了解決方案。這裏是任何需要它的人。 代碼是: private static AxWMPLib.AxWindowsMediaPlayer wmPlayer;

public static void AddMediaPlayer(Form form1) { 
     Button b1 = new Button(); 
     b1.Text = "Button"; 
     try { 
      wmPlayer = new AxWMPLib.AxWindowsMediaPlayer(); 

      ((System.ComponentModel.ISupportInitialize)(wmPlayer)).BeginInit(); 
      wmPlayer.Name = "wmPlayer"; 
      wmPlayer.Enabled = true; 
      wmPlayer.Dock = System.Windows.Forms.DockStyle.Fill; 
      form1.Controls.Add(wmPlayer); 
      ((System.ComponentModel.ISupportInitialize)(wmPlayer)).EndInit(); 

      // After initialization you can customize the Media Player 
      wmPlayer.uiMode = "none"; 
      wmPlayer.URL = @"C:\ProjectSilver\assets\RadarDetectie\general\inlog_confirm.ogv"; 
      wmPlayer.Ctlcontrols.play(); 
     } 
     catch { } 

不要忘記導入庫AxWMPLib。 之後,你需要在你的課堂上添加[STAThread],否則你會得到一個異常。

Goodluck!