基于C#的最简单的音乐播放器的设计
//不知道为什么截图显示不了
最简单的音乐播放器当然是调用基于WindowsMediaPlayer的组件,
1:在C#中,首先要添加windows media player 1.0 ...C://windows/system32/wmp.dll
2:右击toolbox ,单价chooseItem,然后再COM组件中选择 WindowsMediaPlayer
把 WindowsMediaPlayer 放到自己选定的工具箱选项中,然后就像拖拽其他组件一样,将它拖拽到form中,然后要做的就是添加歌曲文件或者歌曲文件夹,以及设置开始,暂停,结束等各种按钮。以及为各种按钮添加事件响应函数。
3:设置form的布局
4:核心的部分,就是依次添加各种事件响应函数
namespace dddplayer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private static Int32 m_TotalSongs=1000;
private string[] m_PlayList = new string[m_TotalSongs];
private Int32 m_Index;
private void AddFile(string path)
{
if (m_Index < m_TotalSongs)
{
m_PlayList[m_Index++] = path;
}
}
private void AddFolder(string path, ListBox list)
{
DirectoryInfo directory = new DirectoryInfo(path);
foreach (FileInfo file in directory.GetFiles("*.mp3"))
{
AddFile(file.FullName);
string strItem = Convert.ToString(m_Index);
strItem += " ";
strItem += file.Name;
//this.listBox1.Items.Add(strItem);
list.Items.Add(strItem);
}
}
private void button2_Click(object sender, EventArgs e)
{
folderBrowserDialog1.SelectedPath = "C:\\";
folderBrowserDialog1.ShowNewFolderButton = false;
folderBrowserDialog1.Description = "选?择?音°?乐¤?文?件t夹D";
folderBrowserDialog1.ShowDialog();
AddFolder(folderBrowserDialog1.SelectedPath,listBox1);
}
private void button4_Click(object sender, EventArgs e)
{
Int32 SelectedIndex = listBox1.SelectedIndex;
string strSelected = m_PlayList[SelectedIndex];
axWindowsMediaPlayer1.URL = strSelected;
}
private void button3_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.pause();
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.InitialDirectory = "C:\\";
openFileDialog1.Filter = "*.mp3|*.wma";
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fname = this.openFileDialog1.FileName;
FileInfo file = new FileInfo(fname);
AddFile(file.FullName);
string strItem = Convert.ToString(m_Index);
strItem += " " + file.Name;
this.listBox1.Items.Add(strItem);
}
}
}
}