Domanda

I'm trying to load the WindowsMediaPlayer control dynamically, but I can't get it to work properly.

The code as is, seems to play the audio (and probably video) but does not show the video on the form. The form keeps blank and the audio is playing. (playing a WVM file, cant be encoder issue) I'm importing the 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.");

    }

Help with getting this to work would be highly appreciated.

È stato utile?

Soluzione

I've found the solution. Here it is for anyone who ever needs it in the future. The code is: 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 { }

Don't forget to import the library AxWMPLib. After this you'll need to add [STAThread] on top of your class, otherwise you will get an exception.

Goodluck!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top