看了网上一些朋友写的关于COM组件Windows Media Player控件的使用心得,我也做了一个很简易的音乐播放器,对于Windows Media Player的初学者朋友们应该有一点帮助。
值得一说的是,很多的资料都没有把控件的一些方法说明白(包括MSDN上面),比如控件的play、pause、stop方法,MSDN上面提供的我都没有在控件中直接找到,而这些方法都是包含在Ctlcontrol属性中,更详细的资料,请看COM组件Windows Media Player(一)(转)
下面我把自做的简易音乐播放器分享上来。
一、文件:
点击打开,会弹出浏览窗口选择歌曲;点击关闭,会关闭正在播放的歌曲;点击退出,退出程序。
二、操作:
设置目录,可以设置播放的路径,相当于批量导入列表;播放,暂停分别实现对应功能。
目前并没有实现列表的记录,也就是程序需要每次都重新导入列表。
三、布局:
列表:点击显示列表,并在此标签前面打勾,再次点击隐藏列表,取消前面的勾。(循环)
四、关于:
弹出介绍作者和播放器的窗口。这个不用多说了。。
五、运行
程序运行就是这个效果,很简单的播放器。
双击列表中的歌曲可实现歌曲的切换。
六、源程序
列表窗体中没有加入代码,而主窗体中的Desinger.cs文件中的代码就不贴了,都是自动生成的。
把主窗体的实现代码粘贴在此:
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Media;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace EX03_1
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public partial class Player : Form
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
私有变量#region 私有变量
private ArrayList alpath = new ArrayList();//保存路径的列表
private ArrayList alname = new ArrayList();//保存文件名,用于显示正在播放的音乐文件
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
名称
private List MusicList = new List();//弹出的列表窗口
private bool ListShow;//判断列表窗口的现实状态
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
构造函数#region 构造函数
public Player()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InitializeComponent();
//将列表窗体的listbox的双击事件绑定在自定义事件上
MusicList.listBox1.DoubleClick += new EventHandler
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
(this.MusicListBox_DoubleCilck);
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
方法#region 方法
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 主窗体加载时的方法,从文件中读取参数,判断是否显示列表窗体
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Player_Load(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FileInfo finfo = new FileInfo(@"..\..\Config.ini");
StreamReader sr = finfo.OpenText();
//循环读取每一行,此处循环的目的是为了方便
//以后扩展,比如加上其他的子窗体
//文件每行存储一个参数,对应一个子窗体的显示状态
while (sr.Peek() >= 0)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string MusicListShow = sr.ReadLine();
if (MusicListShow.Contains("MusicListShow") && MusicListShow.Contains
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
("true"))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
列表ToolStripMenuItem.Checked = ListShow = true;
MusicList.Show();
MusicList.Left = this.Left + this.Width;//仅靠主窗体右侧
MusicList.Top = this.Top;//与主窗体齐平
}
}
sr.Close();
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 打开音乐文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string fn = openFileDialog1.FileName;
axWindowsMediaPlayer1.URL = fn;
//显示正在播放的音乐
label1.Text = "正在播放: " + fn.Substring(fn.LastIndexOf("\\") + 1);
}
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 关闭正在播放的音乐文件,再次点击播放可从头开始
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 关闭CToolStripMenuItem_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
axWindowsMediaPlayer1.close();
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 退出程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Dispose();
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设置播放目录,相当于到导入列表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 设置目录DToolStripMenuItem_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
folderBrowserDialog1.ShowDialog();
//选择路径
DirectoryInfo dir = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
foreach (FileInfo f in dir.GetFiles())
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//支持wav,mp3(Windows Media Player还支持其他的格式,包括视频格式)
if (f.Name.ToLower().EndsWith(".wav") || f.Name.ToLower().EndsWith(".mp3"))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
alpath.Add(f.FullName);
alname.Add(f.Name);
}
}
foreach (string s in alname)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MusicList.listBox1.Items.Add(s);//显示在listbox中
}
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 播放音乐
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 播放PToolStripMenuItem_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (MusicList.listBox1.SelectedItem != null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//用MusicList.listBox1.SelectedIndex作为下表在listbox中选择
axWindowsMediaPlayer1.URL = alpath
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[MusicList.listBox1.SelectedIndex].ToString();
label1.Text = "正在播放: "+alname
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[MusicList.listBox1.SelectedIndex].ToString();
}
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 暂停播放,点击播放按键可继续(不从头开始)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 暂停SToolStripMenuItem_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
axWindowsMediaPlayer1.Ctlcontrols.pause();
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 点击切换列表的显示状态
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 列表ToolStripMenuItem_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (ListShow == true)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MusicList.Hide();
列表ToolStripMenuItem.Checked = false;//取消打勾
ListShow = false;
}
else if (ListShow == false)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MusicList.Show();
列表ToolStripMenuItem.Checked = true;//打勾
ListShow = true;
}
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 列表的双击事件,双击后调用“播放PToolStripMenuItem_Click”方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MusicListBox_DoubleCilck(object sender,EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
播放PToolStripMenuItem_Click(sender, e);
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 显示关于窗口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 本播放器ToolStripMenuItem_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
About a = new About();
a.ShowDialog();
}
#endregion
}
}