#用C#开发简单MP3播放器.docx
《#用C#开发简单MP3播放器.docx》由会员分享,可在线阅读,更多相关《#用C#开发简单MP3播放器.docx(19页珍藏版)》请在冰豆网上搜索。
#用C#开发简单MP3播放器
一、软件说明:
功能说明:
一个简易小巧的MP3播放器,可以播放已经下载好的MP3格式的音乐。
解决什么样的实际问题:
市场上的音乐播放器大都占用资源比较大,而且需要联网,即使播放本地音乐,也会占用较多的网速,而这款播放器解决了这些问题。
<虽然还是有很多缺陷,但用起来还是比较方便的。
)
性能说明:
可以播放单首或多首歌曲,可以保存或清空当前播放列表,支持列表循环,顺序播放,单曲循环。
<但目前只支持MP3格式。
)
程序类型说明:
这个播放器是winform应用程序。
二、软件设计
软件整体结构图及模块划分:
软件整体结构图:
控制模块:
歌曲列表显示区:
在VS的“解决方案资源管理器”中的文件说明:
整个项目的文件如下:
其中,Properties中所包含的的文件有:
这里包含的是定义程序集属性的文件,用来设定生成的有关程序集的常规信息dll文件的一些参数。
引用中包含的文件有:
这里包含的是这个项目所用的的一些类库信息。
Form1.cs中包含有:
这些是窗体的设计信息,而Program.cs是整个源程序的入口。
各模块对应的主要对象或方法:
控制模块中主要用到的是axWindowsmediaPlayer控件以及MenuStrip控件。
其中WindowsmediaPlayer控件用到的方法属性主要有:
axWindowsMediaPlayer1.BeginInit(>。
//初始化
axWindowsMediaPlayer1.settings.autoStart。
//自动播放
axWindowsMediaPlayer1.settings.setMode("shuffle",false>。
//顺序播放
axWindowsMediaPlayer1.settings.enableErrorDialogs。
axWindowsMediaPlayer1.settings.balance。
axWindowsMediaPlayer1.settings.mute。
axWindowsMediaPlayer1.settings.volume。
MenuStrip控件用到的方法事件有:
privatevoid打开文件ToolStripMenuItem1_Click(objectsender,EventArgse>。
privatevoid添加歌曲ToolStripMenuItem_Click(objectsender,EventArgse>。
privatevoid添加文件夹ToolStripMenuItem_Click(objectsender,EventArgse>。
privatevoid退出ToolStripMenuItem_Click(objectsender,EventArgse>。
privatevoid清空列表ToolStripMenuItem1_Click(objectsender,EventArgse>。
privatevoid顺序播放ToolStripMenuItem_Click(objectsender,EventArgse>。
privatevoid单曲循环ToolStripMenuItem_Click(objectsender,EventArgse>。
privatevoid随机播放ToolStripMenuItem_Click(objectsender,EventArgse>。
数据库设计或数据文件说明:
因为播放器使用数据量较小,故采用TXT保存歌曲文件列表。
使用的是文件处理。
设计思路:
播放器模块的设计结构如下:
三、测试:
测试用例列表:
F:
\Music\ALittleLove.mp3
F:
\Music\ChristmasInMyHeart.mp3
F:
\Music\Everytime.mp3
F:
\Music\LivingToLoveYou.mp3
F:
\Music\LoveParadise.mp3
F:
\Music\LuckyTwice-MeAndYou.mp3
F:
\Music\Morning.mp3
F:
\Music\TravelingLight.mp3
F:
\Music\TroubleIsAFriend.mp3
F:
\Music\WishYouWereHere.mp3
F:
\Music\YouandMe.mp3
F:
\Music\Ψһ.mp3
F:
\Music\一直很安静.mp3
F:
\Music\一首简单的歌.mp3
F:
\Music\七里香.mp3
F:
\Music\下一个天亮.mp3
F:
\Music\不再犹豫.mp3
F:
\Music\不完整的旋律.mp3
F:
\Music\东风破.mp3
F:
\Music\依恋.mp3
F:
\Music\依然爱你.mp3
F:
\Music\光辉岁月.mp3
F:
\Music\兰若词.mp3
F:
\Music\再见理想.mp3
F:
\Music\分手快乐.mp3
F:
\Music\勇气.mp3
F:
\Music\匿名的好友.mp3
F:
\Music\单身美好.mp3
F:
\Music\喜欢两个人.mp3
F:
\Music\围巾.mp3
F:
\Music\在树上唱歌.mp3
F:
\Music\夜曲.mp3
F:
\Music\大城小爱.mp3
F:
\Music\天空.mp3
F:
\Music\如果有一天.mp3
F:
\Music\孤单北半球.mp3
F:
\Music\孩子气.mp3
F:
\Music\宁夏.mp3
F:
\Music\左边.mp3
F:
\Music\带我走.mp3
软件运行截图:
测试情况分析
经测试,软件运行情况基本良好,但单曲循环功能有些缺陷,有待改进,总体情况还算良好。
四、小结
这一次的课程设计开发的这个软件是我的第一个软件,看到它能够基本实现我想要的功能,感到非常开心,尽管还有很多缺陷。
在做这个软件的时候,遇到过很多问题,一开始觉得有问题上网找就是了,但在做的的时候却并不是如此,上网XX,用C#开发的播放器源码很多,教程也不少,但都讲的很乱,而且那些属性名,控件名,方法名之类的都和自己的不同,原理不懂,给你源码也没用。
没办法,就只能恶补,像各种控件的属性,尤其是axWindowsmediaPlayer控件的各种属性及使用方法<这也是我这次课程设计最大的收获。
),文件处理等。
通过这次课程设计,我学到了很多课堂和书本学不到的东西,我觉得最重要的一点就是注意细节以及动手之前弄懂基本原理。
五、附录
用到的第三方控件或组件说明:
这个程序用到的第三方控件是axWindowsmediaPlayer控件,这个控件是一个COM组件,是windows自带的控件,用于播放媒体文件,在开发媒体播放器时使用是非常方便的。
程序的主要代码<对应上面第二条):
窗体设计代码From1.cs:
usingSystem。
usingSystem.Collections.Generic。
usingSystem.ComponentModel。
usingSystem.Data。
usingSystem.Drawing。
usingSystem.Linq。
usingSystem.Text。
usingSystem.Windows.Forms。
usingSystem.IO。
namespaceMyMusicPlayer
{
publicpartialclassMusicPlayer:
Form
{
publicMusicPlayer(>
{
InitializeComponent(>。
}
string[]musicPath=newstring[1000]。
//用于保存歌曲目录
intmusicCount=0。
privatevoidForm1_Load(objectsender,EventArgse>
{
myPlayer.BeginInit(>。
//初始化
myPlayer.settings.autoStart=true。
//自动播放
myPlayer.settings.setMode("shuffle",false>。
//顺序播放
myPlayer.settings.enableErrorDialogs=true。
myPlayer.settings.balance=0。
myPlayer.settings.mute=false。
myPlayer.settings.volume=100。
//声音设为最大
if(File.Exists("listbox.txt">>//如果存在播放列表,那么加载播放列表
{
StreamReaderreader=newStreamReader("listbox.txt">。
try
{
while(reader.Peek(>!
=-1>
{
stringfilepath=reader.ReadLine(>。
if(File.Exists(filepath>>
{
musicPath[musicCount++]=filepath。
stringfilename=Path.GetFileName(filepath>。
listBox1.Items.Add(filename>。
//listbox用来显示歌曲名
myPlayer.currentPlaylist.insertItem(myPlayer.currentPlaylist.count,myPlayer.newMedia(filepath>>。
}
}
listBox1.SelectedIndex=0。
}
catch(Exception>
{
listBox1.SelectedIndex=-1。
MessageBox.Show("加载播放列表失败或者列表为空!
","提示",MessageBoxButtons.OK,MessageBoxIcon.Error>。
}
finally
{
reader.Close(>。
}
}
}
privatevoid打开文件ToolStripMenuItem1_Click(objectsender,EventArgse>//打开单首歌曲,但不加载到播放列表
{
DialogResultdr=openFileDialog1.ShowDialog(>。
if(dr==DialogResult.OK>
{
myPlayer.URL=openFileDialog1.FileName。
}
}
privatevoid添加歌曲ToolStripMenuItem_Click(objectsender,EventArgse>//打开单首歌曲,并加载到播放列表
{
DialogResultdr=openFileDialog1.ShowDialog(>。
if(dr==DialogResult.OK>
{
myPlayer.URL=openFileDialog1.FileName。
stringfilepath=openFileDialog1.FileName。
stringfilename=Path.GetFileName(filepath>。
listBox1.Items.Add(filename>。
musicPath[musicCount++]=filepath。
myPlayer.currentPlaylist.insertItem(myPlayer.currentPlaylist.count,myPlayer.newMedia(filepath>>。
}
}
privatevoid添加文件夹ToolStripMenuItem_Click(objectsender,EventArgse>//打开媒体文件夹,并加载到播放列表
{
DialogResultdr=folderBrowserDialog1.ShowDialog(>。
if(dr==DialogResult.OK>
{
string[]filepath=Directory.GetFiles(folderBrowserDialog1.SelectedPath>。
foreach(stringsinfilepath>
{
if(Path.GetExtension(s>==".mp3">
{
stringfilename=Path.GetFileName(s>。
listBox1.Items.Add(filename>。
musicPath[musicCount++]=s。
myPlayer.currentPlaylist.insertItem(myPlayer.currentPlaylist.count,myPlayer.newMedia(s>>。
}
}
}
}
privatevoid退出ToolStripMenuItem_Click(objectsender,EventArgse>//退出播放器并保存当前播放列表
{
myPlayer.Ctlcontrols.stop(>。
myPlayer.close(>。
StreamWriterwriter=newStreamWriter("listbox.txt",false,Encoding.Unicode>。
//保存播放列表
for(inti=0。
i<=musicCount-1。
i++>
{
if(musicPath[i]!
=string.Empty>
{
writer.WriteLine(musicPath[i]>。
}
}
writer.Close(>。
Application.Exit(>。
}
privatevoidaxWindowsMediaPlayer1_PlayStateChange(objectsender,AxWMPLib._WMPOCXEvents_PlayStateChangeEvente>//显示播放状态
{
if(myPlayer.playState==WMPLib.WMPPlayState.wmppsPlaying>
{
strings=myPlayer.currentMedia.sourceURL。
for(inti=0。
ii++>
{
if(listBox1.Items[i].ToString(>==Path.GetFileName(s>>
{
listBox1.SelectedIndex=i。
break。
}
}
}
}
privatevoid清空列表ToolStripMenuItem1_Click(objectsender,EventArgse>//清空当前播放列表
{
myPlayer.Ctlcontrols.stop(>。
//先停止播放器
listBox1.Items.Clear(>。
//清空listbox
myPlayer.currentPlaylist.clear(>。
//清空播放列表
for(intj=0。
jj++>
{
musicPath[j]=""。
}
}
privatevoidForm1_KeyPress(objectsender,KeyPressEventArgse>
{
if(char.Equals(e.KeyChar,''>>
{
if(myPlayer.playState==WMPLib.WMPPlayState.wmppsPlaying>
{
myPlayer.Ctlcontrols.pause(>。
}
}
}
privatevoid顺序播放ToolStripMenuItem_Click(objectsender,EventArgse>//按列表顺序播放歌曲文件
{
myPlayer.settings.playCount=1。
myPlayer.currentPlaylist.clear(>。
for(intj=0。
jj++>
{
if(musicPath[j]!
=string.Empty>
{
myPlayer.currentPlaylist.insertItem(myPlayer.currentPlaylist.count,myPlayer.newMedia(musicPath[j]>>。
}
}
myPlayer.settings.setMode("shuffle",false>。
myPlayer.Ctlcontrols.playItem(myPlayer.currentPlaylist.get_Item(0>>。
}
privatevoid单曲循环ToolStripMenuItem_Click(objectsender,EventArgse>//单曲循环
{
myPlayer.Ctlcontrols.stop(>。
myPlayer.currentPlaylist.clear(>。
if(listBox1.SelectedIndex!
=-1&&listBox1.Items.Count>0>
{
intk=listBox1.SelectedIndex。
for(inti=0。
i<1000。
i++>
{
myPlayer.currentPlaylist.insertItem(myPlayer.currentPlaylist.count,myPlayer.newMedia(musicPath[k]>>。
}
}
myPlayer.Ctlcontrols.playItem(myPlayer.currentPlaylist.get_Item(0>>。
}
privatevoid随机播放ToolStripMenuItem_Click(objectsender,EventArgse>//随机播放列表歌曲文件
{
myPlayer.settings.playCount=1。
myPlayer.Ctlcontrols.stop(>。
myPlayer.currentPlaylist.clear(>。
Randomrd=newRandom(DateTime.Now.Millisecond>。
for(inti=0。
i<1000。
i++>
{
intj=rd.Next(0,musicCount>。
if(musicPath[j]!
=string.Empty>
{
myPlayer.currentPlaylist.insertItem(myPlayer.currentPlaylist.count,myPlayer.newMedia(musicPath[j]>>。
}
}
myPlayer.Ctlcontrols.playItem(myPlayer.currentPlaylist.get_Item(0>>。
}
privatevoidlistBox1_SelectedIndexChanged(objectsender,EventArgse>//点击列表歌曲时播放
{
intj=listBox1.SelectedIndex。
if(listBox1.Items.Count>0&&listBox1.SelectedIndex>=0>
{
myPlayer.Ctlcontrols.playItem(myPlayer.currentPlaylist.get_Item(listBox1.SelectedIndex>>。
}
}
}
}
程序入口代码Program.cs:
usingSystem。
usingSystem.Collections.Generic。
usingSystem.Linq。
usingSystem.Windows.Forms。
usingSystem.Runtime.InteropServices。
usingMicrosoft.Win32。
usingSystem.Threading。
namespaceMyMusicPlayer
{
staticclassProgram
{
/*显示已经运行程序的主界面*/
[DllImport("user32.dll",EntryPoint="ShowWindow">]
staticexternboolShowWindow(IntPtrhandle,intflags>。
//显示窗口
[DllImport("user32.dll",EntryPoint="SetForegroundWindow">]
staticexternboolSetForegroundWindow(IntPtrhandle>。
//将窗口置最前
///
///应用程序的主入口点。
///
[STAThread]
staticvoidMain(>
{
Application.EnableVisualStyles(>。
Application.SetCompatibleTextRenderingDefault(false>。
Application.Run(newMusicPlayer(>>。
}
}
}