首先,需要把Medio Player 控件添加到工具箱中,具体操作如下:
在VS编程环境的“工具箱”中单击右键,选择“选择项”菜单,打开“选择工具箱项”窗口,选择“COM组件”标签,在列表中找到并勾选“Windows Media Player”组件,单击“确定”按钮。将该组件添加到指定的工具箱选项卡中
然后在工具箱里面找 Windows Media Player 控件,拉到form里面,拉出来的控件就是AxWindowsMediaPlayer了,打开属性窗口,在点击拉过来的控件,可以清楚的看到控件名称为AxWindowsMediaPlayer1
再次,进行player 的编程操作,默认的控件属性介绍如下:
属性/方法名: 说明:
[基本属性]
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 = @".datavideo";//媒体路径
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);//设置类表循环播放
}
第三,简单应用实例:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using WMPLib; namespace Audio_Player { public partial class Form1 : Form { public Form1() { InitializeComponent(); lstFiles.View = View.List; AddFilesToolStripMenuItem.Click += AddFilesToolStripMenuItem_Click; ExitToolStripMenuItem.Click += ExitToolStripMenuItem_Click; HelpToolStripMenuItem.Click += HelpToolStripMenuItem_Click; lstFiles.DoubleClick += LstFiles_DoubleClick; } private void HelpToolStripMenuItem_Click(object sender, EventArgs e) { MessageBox.Show("Audio Player By Kevin Lu " + "Tel: 1234567898"); } private void ExitToolStripMenuItem_Click(object sender, EventArgs e) { this.Close(); Application.Exit(); } private void AddFilesToolStripMenuItem_Click(object sender, EventArgs e) { //List<string> filePath=new List<string>(); OpenFileDialog fileDialog=new OpenFileDialog(); fileDialog.Multiselect = true; fileDialog.Filter = "Mp3文件|*.mp3|Wav文件|*.wav|Wma文件|*.wma|Wmv文件|*.wmv|所有格式|*.*"; if (fileDialog.ShowDialog()==DialogResult.OK) { axWindowsMediaPlayer1.currentPlaylist = axWindowsMediaPlayer1.newPlaylist("新建列表", ""); foreach (string filePath in fileDialog.FileNames ) { ListViewItem lstViewItem=new ListViewItem(); lstViewItem.Text = filePath.Substring(filePath.LastIndexOf('\') + 1, filePath.Length - filePath.LastIndexOf('\') - 1); lstViewItem.SubItems.Add(filePath); lstFiles.Items.Add(lstViewItem); // lstFiles.Items.Add(filePath.Substring(filePath.LastIndexOf('\') + 1, filePath.Length - filePath.LastIndexOf('\') - 1), filePath); // } } // axWindowsMediaPlayer1.Ctlcontrols.play(); } private void Form1_Load(object sender, EventArgs e) { axWindowsMediaPlayer1.settings.autoStart = true; } private void LstFiles_DoubleClick(object sender, EventArgs e) {foreach (ListViewItem item in lstFiles.Items) { axWindowsMediaPlayer1.currentPlaylist.appendItem(axWindowsMediaPlayer1.newMedia(item.SubItems[1].Text)); } axWindowsMediaPlayer1.Ctlcontrols.play(); axWindowsMediaPlayer1.currentPlaylist.clear(); axWindowsMediaPlayer1.Ctlcontrols.stop(); if (lstFiles.SelectedItems!=null) { ListViewItem item = lstFiles.SelectedItems[0]; axWindowsMediaPlayer1.currentPlaylist.appendItem(axWindowsMediaPlayer1.newMedia(item.SubItems[1].Text)); axWindowsMediaPlayer1.Ctlcontrols.play(); } } private void 清空列表ToolStripMenuItem_Click(object sender, EventArgs e) { lstFiles.Items.Clear(); } private void 单曲循环ToolStripMenuItem_Click(object sender, EventArgs e) { axWindowsMediaPlayer1.URL = lstFiles.Items[0].SubItems[1].Text; axWindowsMediaPlayer1.settings.playCount = 100000; axWindowsMediaPlayer1.Ctlcontrols.play(); } private void 列表循环播放ToolStripMenuItem_Click(object sender, EventArgs e) { foreach (ListViewItem item in lstFiles.Items) { axWindowsMediaPlayer1.currentPlaylist.appendItem(axWindowsMediaPlayer1.newMedia(item.SubItems[1].Text)); } axWindowsMediaPlayer1.Ctlcontrols.play(); } private void 列表播放ToolStripMenuItem_Click(object sender, EventArgs e) { axWindowsMediaPlayer1.currentPlaylist.clear(); foreach (ListViewItem item in lstFiles.Items) { axWindowsMediaPlayer1.currentPlaylist.appendItem(axWindowsMediaPlayer1.newMedia(item.SubItems[1].Text)); } axWindowsMediaPlayer1.Ctlcontrols.play(); } private void 单曲播放ToolStripMenuItem1_Click(object sender, EventArgs e) { axWindowsMediaPlayer1.currentPlaylist.clear(); if (lstFiles.SelectedItems.Count==0) { axWindowsMediaPlayer1.URL = lstFiles.Items[0].SubItems[1].Text; } else { axWindowsMediaPlayer1.URL = lstFiles.SelectedItems[0].SubItems[1].Text; } axWindowsMediaPlayer1.Ctlcontrols.play(); } } }