C#中axWindowsMediaPlayer控件的用法

属性/方法名: 说明:

[基本属性]  

URL:String; 指定媒体位置,本机或网络地址

uiMode:String; 播放器界面模式,可为Full, Mini, None, Invisible

playState:integer; 播放状态,1=停止,2=暂停,3=播放,6=正在缓冲,9=正在连接,10=准备就绪

enableContextMenu:Boolean; 启用/禁用右键菜单

fullScreen:boolean; 是否全屏显示

[controls] wmp.controls //播放器基本控制

controls.play; 播放

controls.pause; 暂停

controls.stop; 停止

controls.currentPosition:double; 当前进度

controls.currentPositionString:string; 当前进度,字符串格式。如“00:23”

controls.fastForward; 快进

controls.fastReverse; 快退

controls.next; 下一曲

controls.previous; 上一曲

[settings] wmp.settings //播放器基本设置

settings.volume:integer; 音量,0-100

settings.autoStart:Boolean; 是否自动播放

settings.mute:Boolean; 是否静音

settings.playCount:integer; 播放次数

[currentMedia] wmp.currentMedia //当前媒体属性

currentMedia.duration:double; 媒体总长度

currentMedia.durationString:string; 媒体总长度,字符串格式。如“03:24”

currentMedia.getItemInfo(const string); 获取当前媒体信息"Title"=媒体标题,"Author"=艺术家,"Copyright"=版权信息,"Description"=媒体内容描述,"Duration"=持续时间(秒),"FileSize"=文件大小,"FileType"=文件类型,"sourceURL"=原始地址

currentMedia.setItemInfo(const string); 通过属性名设置媒体信息

currentMedia.name:string; 同 currentMedia.getItemInfo("Title")

[currentPlaylist] wmp.currentPlaylist //当前播放列表属性

currentPlaylist.count:integer; 当前播放列表所包含媒体数

currentPlaylist.Item[integer]; 获取或设置指定项目媒体信息,其子属性同wmp.currentMedia

// 创见打开对话框对象实例

OpenFileDialog openFileDialog = new OpenFileDialog();

//设置为可以打开多个文件

openFileDialog.Multiselect = true;

//设置打开文件格式

openFileDialog.Filter = "Mp3文件|*.mp3|Wav文件|*.wav|Wma文件|*.wma|Wmv文件|*.wmv|所有格式|*.*";

//判断是否单击确定按钮

if (openFileDialog.ShowDialog() == DialogResult.OK)

{

//建立播放列表,名字为aa

axWindowsMediaPlayer1.currentPlaylist = axWindowsMediaPlayer1.newPlaylist("aa", "");

//遍历打开的集合

foreach (string fn in openFileDialog.FileNames)

{

//添加播放列表

axWindowsMediaPlayer1.currentPlaylist.appendItem(axWindowsMediaPlayer1.newMedia(fn));

}

}

//播放

axWindowsMediaPlayer1.Ctlcontrols.play();

}

using System.IO;

using WMPLib;

public videoPlay()

{

InitializeComponent();

//全屏设置及隐藏鼠标

this.WindowState = FormWindowState.Maximized;

this.FormBorderStyle = FormBorderStyle.None;

//Cursor.Hide();

//播放器全屏

Rectangle screenSize = System.Windows.Forms.SystemInformation.VirtualScreen;//获取屏幕的宽和高

this.panel1.Location = new System.Drawing.Point(0, 0);

this.panel1.Size = new System.Drawing.Size(screenSize.Width,screenSize.Height);

this.axWindowsMediaPlayer1.Location = new System.Drawing.Point(0, 0);

this.axWindowsMediaPlayer1.Size = new System.Drawing.Size(screenSize.Width, screenSize.Height);

//播放器设置

axWindowsMediaPlayer1.uiMode = "None";//播放器样式

axWindowsMediaPlayer1.stretchToFit = true;//非全屏状态时是否伸展到最佳大小

axWindowsMediaPlayer1.enableContextMenu = false;//禁用播放器右键菜单

}

private IWMPPlaylist videoList;//创建播放列表

private bool ifLoop = true;//视频是否循环

//设置是否循环播放

public bool IfLoop

{

get { return ifLoop; }

set { ifLoop = value; }

}

//播放状态改变时发生

private void axWindowsMediaPlayer1_StatusChange(object sender, EventArgs e)

{

//判断视频是否已停止播放

if ((int)axWindowsMediaPlayer1.playState == 1)

{

//停顿2秒钟再重新播放

//System.Threading.Thread.Sleep(2000);

//重新播放

//axWindowsMediaPlayer1.Ctlcontrols.play();

}

}

//播放

public void videoStart()

{

axWindowsMediaPlayer1.Ctlcontrols.play();

}

//列表播放

public void videoListStart()

{

videoPlayList();//重新获取播放列表

axWindowsMediaPlayer1.Ctlcontrols.play();

}

//暂停

public void videoPause()

{

axWindowsMediaPlayer1.Ctlcontrols.pause();

}

//重播

public void videoReplay()

{

videoStop();

videoStart();

}

//列表重播

public void videoListReplay()

{

axWindowsMediaPlayer1.currentPlaylist = videoList;//重新载入播放列表

videoStart();

}

//停止播放

public void videoStop()

{

//axWindowsMediaPlayer1.currentPlaylist.clear();//清除列表

axWindowsMediaPlayer1.Ctlcontrols.stop();

}

//视频静音

public void videoMute(bool t)

{

axWindowsMediaPlayer1.settings.mute = t;

}

//播放下一个视频

public void videoNext()

{

//判断当前所播放的视频是否是列表的最后一个

if (axWindowsMediaPlayer1.currentMedia.name == axWindowsMediaPlayer1.currentPlaylist.Item[axWindowsMediaPlayer1.currentPlaylist.count - 1].name)

{

}

else

{

axWindowsMediaPlayer1.Ctlcontrols.next();//播放下一个

}

}

//播放上一个媒体

public void videoPrevious()

{ //判断当前所播放的视频是否是列表的第一个

if (axWindowsMediaPlayer1.currentMedia.name == axWindowsMediaPlayer1.currentPlaylist.Item[0].name)

{

}

else

{

axWindowsMediaPlayer1.Ctlcontrols.previous();//播放上一个

}

}

//获取播放类表及初始化

public void videoPlayList()

{

videoList = axWindowsMediaPlayer1.playlistCollection.newPlaylist("one");//创建播放列表

string path = @".\data\video";//媒体路径

DirectoryInfo dir = new DirectoryInfo(path);

foreach (FileSystemInfo fsi in dir.GetFileSystemInfos())

{

if (fsi is FileInfo)

{

FileInfo fi = (FileInfo)fsi;

videoList.appendItem(axWindowsMediaPlayer1.newMedia(fi.FullName));

}

}

axWindowsMediaPlayer1.currentPlaylist = videoList;//查找到视频、播放类表

axWindowsMediaPlayer1.settings.setMode("loop", ifLoop);//设置类表循环播放

}