.NET中对WindowsMediaPlayer控件的调用

本文介绍了如何使用WindowsMediaPlayer控件创建播放器,并解决在.NET环境中遇到的问题,包括生成必要的动态链接库文件,以及如何在项目中正确引用这些控件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

调用WindowsMediaPlayer控件编写播放器时,遇到了不少问题!

发现MS.NET2003中缺少对一些动态链接库的引用!
在调用ActiveX等多媒体控件时,需要用到的MediaPlayer.dll AxMediaPlayer.dll需要自己生成。
首先用下面的命令为媒体播放器控件:msdxm.ocx 生成 MediaPlayer.dll AxMediaPlayer.dll

使用Visual Studio .NET 2003 命令提示运行下面命令
而通常msdxm.ocx中的ActiveX控件都未注册!
先运行regsvr32 msdxm.ocx手动注册便生成需要的动态连接库文件.
然后运行aximp c:/windows/system32/msdxm.ocx
生成成功后会提示: C:/Documents and Settings/Administrator>aximp c:/windows/system32/msdxm.ocx
生成的程序集: C:/Documents and Settings/Administrator/MediaPlayer.dll
生成的程序集: C:/Documents and Settings/Administrator/AxMediaPlayer.dll
再如图2所示,在项目中添加对MediaPlayer.dll AxMediaPlayer.dll的引用;
并在程序中插入:using MediaPlayer便完成了整个调用过程!
播放器如图:

源程序代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using MediaPlayer;

namespace AdvancePlayer
{
    
/**//// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>
    public class Form1 : System.Windows.Forms.Form
    
{
        
private AxMediaPlayer.AxMediaPlayer axWindowsMediaPlayer1;
        
private System.Windows.Forms.OpenFileDialog openFileDialog1;
        
private System.Windows.Forms.MainMenu mainMenu1;
        
private System.Windows.Forms.MenuItem menuItemOpen;
        
private System.Windows.Forms.MenuItem menuItemClose;
        
private System.Windows.Forms.MenuItem menuItemInitSize;
        
private System.Windows.Forms.MenuItem menuItemFullScreen;
        
private System.Windows.Forms.MenuItem menuItemShowAudioCtrl;
        
private System.Windows.Forms.MenuItem menuItemShowPositionCtrl;
        
private System.Windows.Forms.MenuItem menuItemShowTrackbarCtrl;
        
private System.Windows.Forms.MenuItem menuItemFile;
        
private System.Windows.Forms.MenuItem menuItemVideo;
        
private System.Windows.Forms.MenuItem menuItemWindow;     
        
/**//// <summary>
        
/// 必需的设计器变量。
        
/// </summary>
        private System.ComponentModel.Container components = null;
        
public Form1()
        
{
            // Windows 
窗体设计器支持所必需的
            InitializeComponent();
            // TODO: 
 InitializeComponent 调用后添加任何构造函数代码
        }

        
/**//// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>
        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }
            }
            
base.Dispose( disposing );
        }
        
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
        
/**//// <summary>
        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
        
/// 此方法的内容。
        
/// </summary>
        private void InitializeComponent()
        
{
            System.Resources.ResourceManager resources = 
new System.Resources.ResourceManager(typeof(Form1));
            
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            
this.mainMenu1 = new System.Windows.Forms.MainMenu();
            
this.menuItemFile = new System.Windows.Forms.MenuItem();
            
this.menuItemOpen = new System.Windows.Forms.MenuItem();
            
this.menuItemClose = new System.Windows.Forms.MenuItem();
            
this.menuItemVideo = new System.Windows.Forms.MenuItem();
            
this.menuItemInitSize = new System.Windows.Forms.MenuItem();
            
this.menuItemFullScreen = new System.Windows.Forms.MenuItem();
            
this.menuItemWindow = new System.Windows.Forms.MenuItem();
            
this.menuItemShowAudioCtrl = new System.Windows.Forms.MenuItem();
            
this.menuItemShowPositionCtrl = new System.Windows.Forms.MenuItem();
            
this.menuItemShowTrackbarCtrl = new System.Windows.Forms.MenuItem();
            
this.axWindowsMediaPlayer1 = new AxMediaPlayer.AxMediaPlayer();
            ((System.ComponentModel.ISupportInitialize)(
this.axWindowsMediaPlayer1)).BeginInit();
            
this.SuspendLayout();
            
// 
            // openFileDialog1
            // 
            this.openFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
            
// 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]  {
                                                                                      
this.menuItemFile,
                                                                                      
this.menuItemVideo,
                                                                                      
this.menuItemWindow});
            
// 
            // menuItemFile
            // 
            this.menuItemFile.Index = 0;
            
this.menuItemFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]  {
                                                                                         
this.menuItemOpen,
                                                                                         
this.menuItemClose});
            
this.menuItemFile.Shortcut = System.Windows.Forms.Shortcut.CtrlF;
            
this.menuItemFile.Text = "文件(&F)";
            
// 
            // menuItemOpen
            // 
            this.menuItemOpen.Index = 0;
            
this.menuItemOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO;
            
this.menuItemOpen.Text = "打开(&O)";
            
this.menuItemOpen.Click += new System.EventHandler(this.menuItemOpen_Click);
            
this.menuItemOpen.Select += new System.EventHandler(this.Form1_Load);
            
// 
            // menuItemClose
            // 
            this.menuItemClose.Index = 1;
            
this.menuItemClose.Shortcut = System.Windows.Forms.Shortcut.CtrlC;
            
this.menuItemClose.Text = "关闭(&C)";
            
this.menuItemClose.Click += new System.EventHandler(this.menuItemClose_Click);
            
// 
            // menuItemVideo
            // 
            this.menuItemVideo.Index = 1;
            
this.menuItemVideo.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]  {
                                                                                          
this.menuItemInitSize,
                                                                                          
this.menuItemFullScreen});
            
this.menuItemVideo.Shortcut = System.Windows.Forms.Shortcut.CtrlV;
            
this.menuItemVideo.Text = "视频(&V)";
            
// 
            // menuItemInitSize
            // 
            this.menuItemInitSize.Index = 0;
            
this.menuItemInitSize.Shortcut = System.Windows.Forms.Shortcut.CtrlI;
            
this.menuItemInitSize.Text = "默认尺寸(&I)";
            
this.menuItemInitSize.Click += new System.EventHandler(this.menuItemInitSize_Click);
            
// 
            // menuItemFullScreen
            // 
            this.menuItemFullScreen.Index = 1;
            
this.menuItemFullScreen.Shortcut = System.Windows.Forms.Shortcut.CtrlF;
            
this.menuItemFullScreen.Text = "全屏(&F)";
            
this.menuItemFullScreen.Click += new System.EventHandler(this.menuItemFullScreen_Click);
            
// 
            // menuItemWindow
            // 
            this.menuItemWindow.Index = 2;
            
this.menuItemWindow.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]  {
                                                                                           
this.menuItemShowAudioCtrl,
                                                                                           
this.menuItemShowPositionCtrl,
                                                                                           
this.menuItemShowTrackbarCtrl});
            
this.menuItemWindow.Shortcut = System.Windows.Forms.Shortcut.CtrlW;
            
this.menuItemWindow.Text = "窗口(&W)";
            
// 
            // menuItemShowAudioCtrl
            // 
            this.menuItemShowAudioCtrl.Checked = true;
            
this.menuItemShowAudioCtrl.Index = 0;
            
this.menuItemShowAudioCtrl.Shortcut = System.Windows.Forms.Shortcut.CtrlU;
            
this.menuItemShowAudioCtrl.Text = "音频控制(&U)";
            
this.menuItemShowAudioCtrl.Click += new System.EventHandler(this.menuItemShowAudioCtrl_Click);
            
// 
            // menuItemShowPositionCtrl
            // 
            this.menuItemShowPositionCtrl.Checked = true;
            
this.menuItemShowPositionCtrl.Index = 1;
            
this.menuItemShowPositionCtrl.Shortcut = System.Windows.Forms.Shortcut.CtrlP;
            
this.menuItemShowPositionCtrl.Text = "播放进度(&P)";
            
this.menuItemShowPositionCtrl.Click += new System.EventHandler(this.menuItemShowPositionCtrl_Click);
            
// 
            // menuItemShowTrackbarCtrl
            // 
            this.menuItemShowTrackbarCtrl.Checked = true;
            
this.menuItemShowTrackbarCtrl.Index = 2;
            
this.menuItemShowTrackbarCtrl.Shortcut = System.Windows.Forms.Shortcut.CtrlT;
            
this.menuItemShowTrackbarCtrl.Text = "滚动条(&T)";
            
this.menuItemShowTrackbarCtrl.Click += new System.EventHandler(this.menuItemShowTrackbarCtrl_Click);
            
// 
            // axWindowsMediaPlayer1
            // 
            this.axWindowsMediaPlayer1.Location = new System.Drawing.Point(0, -8);
            
this.axWindowsMediaPlayer1.Name = "axWindowsMediaPlayer1";
            
this.axWindowsMediaPlayer1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWindowsMediaPlayer1.OcxState")));
            
this.axWindowsMediaPlayer1.Size = new System.Drawing.Size(296, 280);
            
this.axWindowsMediaPlayer1.TabIndex = 0;
            
// 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            
this.BackColor = System.Drawing.SystemColors.Control;
            
this.ClientSize = new System.Drawing.Size(292, 273);
            
this.Controls.Add(this.axWindowsMediaPlayer1);
            
this.ForeColor = System.Drawing.SystemColors.ControlText;
            
this.Menu = this.mainMenu1;
            
this.Name = "Form1";
            
this.Text = "Form1";
            
this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(
this.axWindowsMediaPlayer1)).EndInit();
            
this.ResumeLayout(false);

        }
        
#endregion
        
/**//// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>
        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new Form1());
        }
        
private void menuItemOpen_Click(object sender, System.EventArgs e)
        
{
            openFileDialog1.Filter = "
视频文件(*.avi;*.wmv;*.dat;*.mpg;*.mpeg;*.mov;*.wm;*.wma)|*.avi;*.wmv;*.dat;*.mpg;*.mpeg;*.mov;*.wm;*.wma|音频文件(*.wav;*.mp3;*.snd;*.au;*.midi;*.mid)|*.wav;*.mp3;*.snd;*.au;*.midi;*.mid|所有文件(*.*)|*.*";       
            
//打开的文件类型
            if(openFileDialog1.ShowDialog()==DialogResult.OK)
            
{
                axWindowsMediaPlayer1.FileName = openFileDialog1.FileName;
                
//如果打开的是音频文件,则禁止【视频】菜单组
                if(openFileDialog1.FilterIndex == 2)
                    menuItemVideo.Enabled = 
false;
                
else
                    menuItemVideo.Enabled = 
true;
            }
        
        }
        
private void menuItemClose_Click(object sender, System.EventArgs e)
        
{
            axWindowsMediaPlayer1.FileName = 
null;
            menuItemVideo.Enabled = 
false;
        }
        
private void menuItemInitSize_Click(object sender, System.EventArgs e)
        
{
            axWindowsMediaPlayer1.DisplaySize = MPDisplaySizeConstants.mpDefaultSize;
        }
        
private void menuItemFullScreen_Click(object sender, System.EventArgs e)
        
{
            axWindowsMediaPlayer1.DisplaySize = MPDisplaySizeConstants.mpFullScreen;
        }
        
private void menuItemShowAudioCtrl_Click(object sender, System.EventArgs e)
        
{
            menuItemShowAudioCtrl.Checked = !menuItemShowAudioCtrl.Checked;
            axWindowsMediaPlayer1.ShowTracker = 
false;
            axWindowsMediaPlayer1.ShowTracker = menuItemShowTrackbarCtrl.Checked;
            axWindowsMediaPlayer1.ShowAudioControls = menuItemShowAudioCtrl.Checked;
        }
        
private void menuItemShowPositionCtrl_Click(object sender, System.EventArgs e)
        
{
            menuItemShowPositionCtrl.Checked = !menuItemShowPositionCtrl.Checked;
            axWindowsMediaPlayer1.ShowPositionControls = menuItemShowPositionCtrl.Checked;
        }
        
private void menuItemShowTrackbarCtrl_Click(object sender, System.EventArgs e)
        
{
            menuItemShowTrackbarCtrl.Checked = !menuItemShowTrackbarCtrl.Checked;
            axWindowsMediaPlayer1.ShowAudioControls = 
false;
            axWindowsMediaPlayer1.ShowTracker = menuItemShowTrackbarCtrl.Checked;
            axWindowsMediaPlayer1.ShowAudioControls = menuItemShowAudioCtrl.Checked;
        }
        
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
        
{
            openFileDialog1.OpenFile();
        }
        
private void Form1_Load(object sender, System.EventArgs e)
        
{
        
        }
    }
}

 
        (图2
      如图2中选中的AxMediaPlayerMediaPlayer既需自己生成的dll2003系统默认中无迹可寻!他们所起的作用便是实现对ActiceX中播放器相关的函数的调用!
      
所以,对于多媒体应用程序的编写,关键在于了解对ActiveX相关控件的调用过程!
     
     
充分使用.NET中的控件技术,无疑是最直接和最高效的选择,这给我们提供了很大的方便,以往很多需要自己编写的函数和功能,现在都为我们封装成了各种控件,直接调用便可实现!相比往日,效率提高了不少!但其中相关的实现过程和细节还是应该多加了解才好,毕竟,只有让其工作原理烂熟于心,才能将.NET这门强大的技术运用得炉火纯青!
netken
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值