C# Windows Media Player 控件使用实例 方法(转)

摘要: Windows Media Player是一种媒体播放器,可以播放当前最流行的音频、视频文件和大多数混合型的多媒体文件。为了便于程序的开发,Visual Studio 2005集成开发环境提供了Windows Media Player控件,

 Windows Media Player控件
Windows Media Player是一种媒体播放器,可以播放当前最流行的音频、视频文件和大多数混合型的多媒体文件。为了便于程序的开发,Visual Studio 2005集成开发环境提供了Windows Media Player控件,并且提供了相关的属性、方法,开发者根据提供的属性、方法完全可以实现Windows Media Player播放器的所有功能。
在使用Windows Media Player控件进行程序开发前,必须将Windows Media Player控件添加到工具箱中,步骤如下所示。
(1)选择工具箱,并单击鼠标右键,在弹出的快捷菜单中选择“选择项”。
(2)弹出“选择工具箱项”对话框,选择“COM组件”选项卡。
(3)在COM组件列表中,选择名称为“Windows Media Player”,单击【确定】按钮,Windows Media Player控件添加成功,如图1所示。

C Windows Media Player 控件使用实例 方法(转) - 魑魅魍魉福 - 魑魅魍魉HOME

 
图1  添加Windows Media Player控件
表1和表2介绍Windows Media Player控件提供的主要属性和方法。

 


另外,将Windows Media Player控件添加到窗体上,在该控件上单击鼠标右键,弹出“Windows Media Player控件属性”对话框,为Windows Media Player控件提供中文属性对话框,如图2所示。
C Windows Media Player 控件使用实例 方法(转) - 魑魅魍魉福 - 魑魅魍魉HOME
图2  Windows Media Player控件属性设置

示例
Windows Media Player控件制作多媒体播放器
本示例利用Windows Media Player控件来实现播放视频文件。程序运行结果如图3所示。

程序开发步骤如下所示。
(1)创建一个项目,命名为27_02,设置默认窗体的Text属性为“网站源代码多媒体播放器”。
(2)在窗体上添加Windows Media Player控件和3个Button控件。
(3)程序代码如下。
sing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace _7_02
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OpenFileDialog openFile = new OpenFileDialog();
private void button1_Click(object sender, EventArgs e)
{//本教程来自:http://www.isstudy.com
openFile.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = openFile.FileName;
}
private void button3_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.close();
}
}
}
完整程序代码如下:
★   ★★★★Form1.cs窗体代码文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace _7_02
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OpenFileDialog openFile = new OpenFileDialog();
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
openFile.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = openFile.FileName;
}
private void button3_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.close();
}
}
}
★   ★★★★Form1.Designer.cs窗体设计文件完整程序代码网站源代码★★★★★
namespace _7_02

{ partial class Form1 { /// summary /// 必需的设计器变量。 /// /summary private System.ComponentModel.IContainer components = null; /// summary /// 清理所有正在使用的资源。 /// /s


{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{//本教程来自:http://www.isstudy.com
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.axWindowsMediaPlayer1 = new AxWMPLib.AxWindowsMediaPlayer();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.axWindowsMediaPlayer1)).BeginInit();
this.SuspendLayout();
//
// axWindowsMediaPlayer1
//
this.axWindowsMediaPlayer1.Dock = System.Windows.Forms.DockStyle.Top;
this.axWindowsMediaPlayer1.Enabled = true;
this.axWindowsMediaPlayer1.Location = new System.Drawing.Point(0, 0);
this.axWindowsMediaPlayer1.Name = "axWindowsMediaPlayer1";
this.axWindowsMediaPlayer1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWindowsMediaPlayer1.OcxState")));
this.axWindowsMediaPlayer1.Size = new System.Drawing.Size(411, 211);
this.axWindowsMediaPlayer1.TabIndex = 0;
//
// button1
//本教程来自:http://www.isstudy.com
this.button1.Location = new System.Drawing.Point(155, 217);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(94, 23);
this.button1.TabIndex = 1;
this.button1.Text = "打开播放文件";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(255, 217);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "播放";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(336, 217);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 1;
this.button3.Text = "停止";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(411, 242);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.axWindowsMediaPlayer1);
this.Name = "Form1";
this.Text = "多媒体播放器";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.axWindowsMediaPlayer1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private AxWMPLib.AxWindowsMediaPlayer axWindowsMediaPlayer1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
}
}
★   ★★★★Program.cs主程序文件完整程序代码网站源代码★★★★★
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace _7_02
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

原文地址:https://www.cnblogs.com/zhihaowang/p/10128675.html