2012-05-29 10 views
8

Sto provando a creare il controllo di Windows Media Player in modo programmatico in modo da poter intercettare eventuali errori di inizializzazione. Prima, quando ho semplicemente lasciato cadere il controllo sul mio modulo, tutto ha funzionato bene. Ma ora che sto cercando di riprodurre le cose a livello di programmazione, il video non viene visualizzato nel controllo. Vedo solo video nero ma ascolto l'audio.Il video di Windows Media Player è nero se il controllo viene creato a livello di programmazione

Qualche idea?

public TrimVideoControl() 
    { 
     InitializeComponent(); 

     // Try creating WMP control 
     // We do this here so we can gracefully catch errors if the control doesn't load 
     try 
     { 

      wmPlayer = new AxWMPLib.AxWindowsMediaPlayer(); 
      ((System.ComponentModel.ISupportInitialize)(wmPlayer)).BeginInit(); 
      //SuspendLayout(); 
      wmPlayer.CreateControl(); 
      wmPlayer.Name = "wmPlayer"; 
      wmPlayer.Ctlenabled = true; 
      System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(TrimVideoControl)); 
      wmPlayer.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("wmPlayer.OcxState"))); 
      wmPlayer.Location = new Point(12, 13); 
      wmPlayer.Size = new Size(636, 358); 
      wmPlayer.enableContextMenu = true; 
      wmPlayer.stretchToFit = true; 
      wmPlayer.uiMode = "none"; 
      wmPlayer.settings.autoStart = false; 
      wmPlayer.ErrorEvent += wmPlayer_ErrorEvent; 
      wmPlayer.MediaChange += wmPlayer_MediaChange; 
      wmPlayer.MediaError += wmPlayer_MediaError; 
      wmPlayer.OpenStateChange += wmPlayer_OpenStateChange; 
      wmPlayer.PlayStateChange += wmPlayer_PlayStateChange; 
      wmPlayer.Warning += wmPlayer_Warning; 
      this.Controls.Add(wmPlayer); 
      ((System.ComponentModel.ISupportInitialize)(wmPlayer)).EndInit(); 

      //this.ResumeLayout(false); 
      //this.PerformLayout(); 
      //wmPlayer.Show(); 
      //wmPlayer.BringToFront(); 
     } 
     catch (Exception ex) 
     { 
      Logger.Error("Error creating WMP control: " + ex); 
     } 


    } 

risposta

10

Il problema esatto con la creazione di runtime MediaPalyer è il fatto che non siamo in grado di svolgere tutti i cambiamenti di stato del MediaPlayer (qualsiasi impostazione come url/uimode ecc) prima del componente sono stati completamente inizializzati. Lo stato del componente serializzato del VS-designer è un oggetto AxHost.State e non influisce su altre impostazioni. In fase di runtime è possibile utilizzare il seguente approccio:

void AddMediaPlayer(string url) { 
    try { 
     var wmPlayer = new AxWMPLib.AxWindowsMediaPlayer(); 

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

     // After initialization you can customize the Media Player 
     wmPlayer.uiMode = "none"; 
     wmPlayer.URL = url; 
    } 
    catch { } 
} 
+0

Ha funzionato! Grazie mille. –

Problemi correlati