2012-05-29 21 views
8

Estoy intentando crear mediante programación el control del Reproductor de Windows Media para que pueda interceptar cualquier error de inicialización. Antes, cuando simplemente dejaba caer el control sobre mi formulario, todo funcionaba bien. Pero ahora que intento reproducir cosas programáticamente, el video no aparece en el control. Solo veo un video negro pero escucho el audio.El video del Reproductor de Windows Media es negro si se crea el control mediante programación

¿Alguna 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); 
     } 


    } 

Respuesta

10

El problema exacto con la creación de tiempo de ejecución MediaPalyer es el hecho de que no podemos realizar cualquier cambio de estado de MediaPlayer (como cualquier configuración url/uimode etc.) antes del componente han sido completamente inicializado. El estado del componente serializado VS-designer es un objeto AxHost.State y no afecta a ninguna otra configuración. En tiempo de ejecución puede usar el siguiente enfoque:

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

¡Eso funcionó! Muchas gracias. –

Cuestiones relacionadas