上一个版本的mp3 player时间仓促,代码中存在不少错误,功能也不完善。这一个多月一直用自己做的seaPlayer (给它暂时取个名字) 听歌, 改了不少漏洞,还完善了一些功能。这些功能有:
1. 最小化时隐藏界面,只在显示在状态栏里显示图标。
2. 退出程序时能将播放列表中的歌曲保存起来,以便下次启动播放器时能装载列表。
3. 增加了文件拖放功能,把想听的歌曲直接拖到窗口就可以添加进列表。
4. 增加了键盘del键对删除歌曲的控制,可以方便删除不想听的歌曲。
下面是全部的代码:
seaPlayer.cs:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace seaPlayer
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class seaPlayer : System.Windows.Forms.Form
{
private AxWMPLib.AxWindowsMediaPlayer axWindowsMediaPlayer1;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.MenuItem menuItem8;
private System.Windows.Forms.MenuItem menuItem9;
private System.Windows.Forms.MenuItem menuItem10;
private System.Windows.Forms.MenuItem menuItem11;
private System.Windows.Forms.MenuItem menuItem12;
private System.Windows.Forms.MenuItem menuItem13;
private System.Windows.Forms.MenuItem menuItem14;
private System.Windows.Forms.MenuItem menuItem15;
private System.Windows.Forms.MenuItem menuItem16;
private System.Windows.Forms.MenuItem menuItem17;
private System.Windows.Forms.MenuItem menuItem18;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.Timer timer1;
private Player MyPlayer;
public seaPlayer()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this.notifyIcon1.Icon = new System.Drawing.Icon("dog.ico");
this.notifyIcon1.Visible = false;
MyPlayer = new Player(axWindowsMediaPlayer1);
this.Load();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
MyPlayer.save();
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(seaPlayer));
this.axWindowsMediaPlayer1 = new AxWMPLib.AxWindowsMediaPlayer();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.menuItem8 = new System.Windows.Forms.MenuItem();
this.menuItem9 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem10 = new System.Windows.Forms.MenuItem();
this.menuItem11 = new System.Windows.Forms.MenuItem();
this.menuItem12 = new System.Windows.Forms.MenuItem();
this.menuItem13 = new System.Windows.Forms.MenuItem();
this.menuItem14 = new System.Windows.Forms.MenuItem();
this.menuItem15 = new System.Windows.Forms.MenuItem();
this.menuItem16 = new System.Windows.Forms.MenuItem();
this.menuItem17 = new System.Windows.Forms.MenuItem();
this.menuItem18 = new System.Windows.Forms.MenuItem();
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
this.listBox1 = new System.Windows.Forms.ListBox();
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.timer1 = new System.Windows.Forms.Timer(this.components);
((System.ComponentModel.ISupportInitialize)(this.axWindowsMediaPlayer1)).BeginInit();
this.SuspendLayout();
//
// axWindowsMediaPlayer1
//
this.axWindowsMediaPlayer1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.axWindowsMediaPlayer1.Enabled = true;
this.axWindowsMediaPlayer1.Location = new System.Drawing.Point(0, 256);
this.axWindowsMediaPlayer1.Name = "axWindowsMediaPlayer1";
this.axWindowsMediaPlayer1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWindowsMediaPlayer1.OcxState")));
this.axWindowsMediaPlayer1.Size = new System.Drawing.Size(216, 45);
this.axWindowsMediaPlayer1.TabIndex = 0;
this.axWindowsMediaPlayer1.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(this.axWindowsMediaPlayer1_PlayStateChange);
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem6,
this.menuItem18});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem3,
this.menuItem5,
this.menuItem4,
this.menuItem7,
this.menuItem8,
this.menuItem9});
this.menuItem1.Text = "文件";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "将文件添加到列表...";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.Text = "将文件夹添加到列表...";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// menuItem5
//
this.menuItem5.Index = 2;
this.menuItem5.Text = "-";
//
// menuItem4
//
this.menuItem4.Index = 3;
this.menuItem4.Text = "删除选定歌曲";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// menuItem7
//
this.menuItem7.Index = 4;
this.menuItem7.Text = "清除所有歌曲";
this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);
//
// menuItem8
//
this.menuItem8.Index = 5;
this.menuItem8.Text = "-";
//
// menuItem9
//
this.menuItem9.Index = 6;
this.menuItem9.Text = "退出";
this.menuItem9.Click += new System.EventHandler(this.menuItem9_Click);
//
// menuItem6
//
this.menuItem6.Index = 1;
this.menuItem6.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem10,
this.menuItem11,
this.menuItem12,
this.menuItem13,
this.menuItem14,
this.menuItem17});
this.menuItem6.Text = "控制";
//
// menuItem10
//
this.menuItem10.Index = 0;
this.menuItem10.Text = "上一首歌曲";
this.menuItem10.Click += new System.EventHandler(this.menuItem10_Click);
//
// menuItem11
//
this.menuItem11.Index = 1;
this.menuItem11.Text = "下一首歌曲";
this.menuItem11.Click += new System.EventHandler(this.menuItem11_Click);
//
// menuItem12
//
this.menuItem12.Index = 2;
this.menuItem12.Text = "-";
//
// menuItem13
//
this.menuItem13.Index = 3;
this.menuItem13.Text = "顺序播放";
this.menuItem13.Click += new System.EventHandler(this.menuItem13_Click);
//
// menuItem14
//
this.menuItem14.Index = 4;
this.menuItem14.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem15,
this.menuItem16});
this.menuItem14.Text = "重复播放";
//
// menuItem15
//
this.menuItem15.Index = 0;
this.menuItem15.Text = "重复播放所有歌曲";
this.menuItem15.Click += new System.EventHandler(this.menuItem15_Click);
//
// menuItem16
//
this.menuItem16.Index = 1;
this.menuItem16.Text = "重复播放一首歌曲";
this.menuItem16.Click += new System.EventHandler(this.menuItem16_Click);
//
// menuItem17
//
this.menuItem17.Index = 5;
this.menuItem17.Text = "随机播放";
this.menuItem17.Click += new System.EventHandler(this.menuItem17_Click);
//
// menuItem18
//
this.menuItem18.Index = 2;
this.menuItem18.Text = "关于";
this.menuItem18.Click += new System.EventHandler(this.menuItem18_Click);
//
// listBox1
//
this.listBox1.BackColor = System.Drawing.SystemColors.ControlDark;
this.listBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(0, 0);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(216, 256);
this.listBox1.TabIndex = 1;
this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);
this.listBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.listBox1_KeyUp);
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// notifyIcon1
//
this.notifyIcon1.Text = "seaPlayer";
this.notifyIcon1.Visible = true;
this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// seaPlayer
//
this.AllowDrop = true;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(216, 301);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.axWindowsMediaPlayer1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.Name = "seaPlayer";
this.Text = "seaPlayer";
this.Resize += new System.EventHandler(this.Form1_Resize);
this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop);
this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter);
((System.ComponentModel.ISupportInitialize)(this.axWindowsMediaPlayer1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main
()
{
Application.Run(new seaPlayer());
}
private void showfiles(string path,ListBox listBox1)
{
DirectoryInfo dir = new DirectoryInfo(path);
foreach(FileInfo f in dir.GetFiles("*.mp3"))
{
MyPlayer.AddFile(f.FullName);
}
foreach(DirectoryInfo f in dir.GetDirectories())
{
showfiles(f.FullName,listBox1);
}
}
private void Load()
{
listBox1.Items.Clear();
for(int i = 1;i <= MyPlayer.NumOfMusic; i++)
{
string STRFILE = Convert.ToString(i);
for(int j = 1;j<=5-STRFILE.Length;j++)STRFILE+=' ';
FileInfo f = new FileInfo(MyPlayer.PlayList(i));
STRFILE += f.Name;
this.listBox1.Items.Add(STRFILE);
}
}
private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
{
this.Visible = true;
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
this.notifyIcon1.Visible = false;
}
private void Form1_Resize(object sender, System.EventArgs e)
{
if(this.WindowState == System.Windows.Forms.FormWindowState.Minimized)
{
this.notifyIcon1.Visible = true;
this.Visible = false;
}
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
if(this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
string path = this.openFileDialog1.FileName;
FileInfo f = new FileInfo(path);
MyPlayer.AddFile(f.FullName);
Load();
}
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
if(this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
string path = this.folderBrowserDialog1.SelectedPath;
showfiles(path,listBox1);
Load();
}
}
private void listBox1_DoubleClick(object sender, System.EventArgs e)
{
if(listBox1.SelectedIndex >= 0)
MyPlayer.play(listBox1.SelectedIndex + 1);
}
private void menuItem4_Click(object sender, System.EventArgs e)
{
if(listBox1.SelectedIndex >= 0)
{
MyPlayer.DelFile(listBox1.SelectedIndex + 1);
listBox1.Items.Clear();
Load();
}
}
private void menuItem7_Click(object sender, System.EventArgs e)
{
listBox1.Items.Clear();
MyPlayer.NumOfMusic = 0;
}
private void menuItem9_Click(object sender, System.EventArgs e)
{
MyPlayer.save();
Application.Exit();
}
private void menuItem10_Click(object sender, System.EventArgs e)
{
if(MyPlayer.CurrentPlay > 0)
{
int play = MyPlayer.CurrentPlay - 1;
if(play == 0)play = MyPlayer.NumOfMusic;
MyPlayer.play(play);
listBox1.SelectedIndex = play - 1;
}
}
private void menuItem11_Click(object sender, System.EventArgs e)
{
if(MyPlayer.CurrentPlay > 0)
{
int play = MyPlayer.CurrentPlay + 1;
if(play > MyPlayer.NumOfMusic)play = 1;
MyPlayer.play(play);
listBox1.SelectedIndex = play - 1;
}
}
private void menuItem13_Click(object sender, System.EventArgs e)
{
menuItem13.Checked = true;
menuItem14.Checked = false;
menuItem15.Checked = false;
menuItem16.Checked = false;
menuItem17.Checked = false;
}
private void menuItem15_Click(object sender, System.EventArgs e)
{
menuItem13.Checked = false;
menuItem14.Checked = false;
menuItem15.Checked = true;
menuItem16.Checked = false;
menuItem17.Checked = false;
}
private void menuItem17_Click(object sender, System.EventArgs e)
{
menuItem13.Checked = false;
menuItem14.Checked = false;
menuItem15.Checked = false;
menuItem16.Checked = false;
menuItem17.Checked = true;
}
private void menuItem16_Click(object sender, System.EventArgs e)
{
menuItem13.Checked = false;
menuItem14.Checked = false;
menuItem15.Checked = false;
menuItem16.Checked = true;
menuItem17.Checked = false;
}
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if(MyPlayer.playstate == WMPLib.WMPPlayState.wmppsMediaEnded)
{
timer1.Start();
}
}
private void timer1_Tick(object sender, System.EventArgs e)
{
timer1.Stop();
int selectnum = 0;
if(menuItem13.Checked)selectnum = MyPlayer.NextPlay(0);
else if (menuItem15.Checked)selectnum = MyPlayer.NextPlay(1);
else if (menuItem16.Checked)selectnum = MyPlayer.NextPlay(2);
else if (menuItem17.Checked)selectnum = MyPlayer.NextPlay(3);
if(selectnum != 0)
{
listBox1.SelectedIndex = selectnum - 1;
MyPlayer.play(selectnum);
}
}
private void menuItem18_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Developed By Sea!\n ----2005.8.8");
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void listBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.Delete)
{
if(listBox1.SelectedIndex >= 0)
{
int temp = listBox1.SelectedIndex;
MyPlayer.DelFile(listBox1.SelectedIndex + 1);
Load();
if(temp > MyPlayer.NumOfMusic - 1)temp = -1;
listBox1.SelectedIndex = temp;
}
}
}
private void Form1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect=DragDropEffects.Copy;
else
e.Effect=DragDropEffects.None;
}
private void Form1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
if(e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
{
MyPlayer.AddFile(file);
}
Load();
}
}
}
}
Player.cs:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace seaPlayer
{
/// <summary>
/// Player 的摘要说明。
/// </summary>
public class Player
{
private AxWMPLib.AxWindowsMediaPlayer myPlayer;
private string[] playList;
private int numOfMusic;
private int currentPlay;
private string savepath;
public int CurrentPlay
{
get
{
return currentPlay;
}
}
public int NumOfMusic
{
get
{
return numOfMusic;
}
set
{
numOfMusic = value;
}
}
public WMPLib.WMPPlayState playstate
{
get
{
return myPlayer.playState;
}
}
public void save()
{
StreamWriter sw = new StreamWriter(savepath);
for(int i = 1; i <= numOfMusic; i ++)
{
sw.WriteLine(playList[i]);
}
sw.Close();
}
public void load()
{
try
{
StreamReader sr = new StreamReader(savepath);
String line;
numOfMusic = 0;
while ((line = sr.ReadLine()) != null)
{
numOfMusic ++;
playList[numOfMusic] = line;
}
sr.Close();
}
catch
{
}
}
public string PlayList(int num)
{
return playList[num];
}
public Player(AxWMPLib.AxWindowsMediaPlayer mediaPlayer)
{
FileInfo t = new FileInfo("save.msg");
savepath = t.FullName;
myPlayer = mediaPlayer;
playList = new string[1001];
numOfMusic = 0;
currentPlay = 0;
load();
}
public void AddFile(string path)
{
if(numOfMusic < 1000)
{
numOfMusic ++;
playList[numOfMusic] = path;
}
}
public void DelFile(int selectNum)
{
for(int i = selectNum; i <= numOfMusic - 1; i++)
{
playList[i] = playList[i + 1];
}
numOfMusic --;
}
public void play(int selectNum)
{
myPlayer.URL = playList[selectNum];
currentPlay = selectNum;
}
public int NextPlay(int type)
{
/* type = 0 顺序
type = 1 重复播放全部
type = 2 重复播放一首
type = 3 随机播放
*/
switch (type)
{
case 0:
currentPlay ++;
if(currentPlay > numOfMusic)return 0;
else return currentPlay;
case 1:
currentPlay ++;
if(currentPlay > numOfMusic) return 1;
else return currentPlay;
case 2:
return currentPlay;
case 3:
Random rdm = new Random(unchecked((int)DateTime.Now.Ticks));
currentPlay = rdm.Next() % numOfMusic;
if(currentPlay == 0) return numOfMusic;
else return currentPlay;
default:
return 0;
}
}
}
}
总算大功告成了,感觉和一般播放器比也差不多拉:) 反正现在我听mp3只用seaPlayer了,毕竟嘛,小巧又方便哦。还是免费的哦!