Windows Media Player video is black if control is created programmatically
Asked Answered
S

1

9

I'm trying to programmatically create the Windows Media Player control so I can trap any initialization errors. Before when I simply dropped the control on my form, everything played fine. But now that I'm trying to play things programmatically, the video isn't appearing in the control. I only see black video but I hear the audio.

Any ideas?

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


    }
Slung answered 29/5, 2012 at 22:11 Comment(0)
T
12

The exact problem with the MediaPalyer runtime creation is the fact that we can't perform any changes of MediaPlayer's state (any settings like url/uimode etc.) before the component have been completely initialized. The VS-designer serialized component's state as an AxHost.State object and don't affect any other settings. At runtime you can use the following approach:

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 { }
}
Taddeo answered 5/6, 2012 at 9:34 Comment(1)
Wow! This worked after lots of debugging and trying everything else.Lillielilliputian

© 2022 - 2024 — McMap. All rights reserved.