1

我正在尝试动态加载 WindowsMediaPlayer 控件,但无法使其正常工作。

原样的代码似乎播放音频(可能还有视频),但未在表单上显示视频。表单保持空白,正在播放音频。(播放 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.");

    }

帮助使其工作将不胜感激。

4

1 回答 1

2

我找到了解决方案。在这里,它适合任何将来需要它的人。代码为: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],否则您将收到异常。

祝你好运!

于 2013-11-05T23:44:08.737 回答