您的当前位置:首页正文

音乐播放器开发模块

2022-03-28 来源:好走旅游网


音乐播放器开发文档

1.动能介绍

开始菜单包含:a.添加音乐文件b.添加音乐文件夹c.添加背景图片d.退出

播放模式:a.单曲循环 b.顺序循环 c.列表播放 d.随机播放

透明度:透明百分之几或不透明

界面设置:迷你模式

关于:版本信息

2、代码讲解

全局变量:private SoundPlayer Player = new SoundPlayer();

private SoundPlayer SencondPlayer = new SoundPlayer();

Image[] image = new Image[500];//存放图片数组

private int Imagecount = 0;//计算背景图片数

private int order = 0;//存储图片总数

private int fileCount = 0;//数组中计算音乐文件数

private int fileCount1 = 0;//文件系数

string[] musicfile = new string[500];//存放音乐文件

int selectOne = 0;//双击事件选择的音乐的序号

static int stop = 2;//判断是暂停还是播放

static double CurrentPosition = 0;//获得文件播放的位置

static int State = 0;//当前状态等于时进行单曲循环,等于零的时候进行顺序播放, //当前状态等于时为随机播放//当前状态等于时为列表播放

OpenFileDialog openfile = new OpenFileDialog();//打开本地磁盘对话框

添加音乐文件

private void 添加文件ToolStripMenuItem1_Click(object sender, EventArgs e)

{

fileCount1++;

openfile.Filter = \"|*.mp3\";

openfile.Multiselect = true;//支持多行文件

try

{

if (DialogResult.OK == openfile.ShowDialog())

{

musicfile[fileCount] = openfile.FileName;

FileInfo fileinfo = new FileInfo(musicfile[fileCount]);

//FileInfo类是将文件去相对路径

string filename = fileCount1.ToString() + \"_\" + fileinfo.Name;//使音乐名前加序号并只取文件名

this.listBox1.Items.Add(filename);//将文件多行显示在列表中

this.musicname.Text = fileinfo.Name;

PlayAndStop(fileCount); //启动MP3播放器

fileCount++;

}

}

catch (Exception)

{

MessageBox.Show(musicfile[fileCount].ToString());

}

}

添加音乐文件夹

private void 添加文件夹ToolStripMenuItem1_Click(object sender, EventArgs e)

{

FolderBrowserDialog folder = new FolderBrowserDialog();//文件夹初始化

folder.ShowDialog();

string[] file = Directory.GetFiles(folder.SelectedPath.ToString(),

\"*.MP3\");//使用数组存得到的文件

int fileName = 0;

try

{

{

fileCount1++;

musicfile[fileCount] = file[fileName];

fileName++;

fileCount++; 文件名

}

foreach (string Music in file)

FileInfo f = new FileInfo(file[fileName]);//读取文件

//数组存文件数

string total = fileCount1.ToString() + \"_\" + f.Name;//f.Name只取f中的this.listBox1.Items.Add(total);

}

catch (Exception)

{

MessageBox.Show(\"输入的音乐总数超出!\");

}

}

添加背景图片

private void 添加背景图片ToolStripMenuItem1_Click(object sender, EventArgs e)

{

FolderBrowserDialog folder = new FolderBrowserDialog();

folder.ShowDialog();

string[] BGfile = Directory.GetFiles(folder.SelectedPath.ToString(), \"*.jpg\");

foreach (string pic in BGfile)

{

image[order] = Image.FromFile(pic);

order++;

}

this.timer1.Enabled = true;//启动定时器

}

定时器

private void timer1_Tick(object sender, EventArgs e)

{

this.BGpicture.Image = image[Imagecount];

if (Imagecount >= order-1)

{

Imagecount = 0;

}

else

{

Imagecount++;

}

}

退出播放器

private void 退出ToolStripMenuItem1_Click(object sender, EventArgs e)

{

this.Close();

}

MP3音乐播放器 播放功能

//播放MP3文件

private void PlayAndStop(int fileCount)

{

try

{

stop = 3;

this.PlayOrStop.Text = \"暂停\";

FileInfo f = new FileInfo(musicfile[fileCount]);

this.musicname.Text = f.Name.ToString();//得到正在播放的音乐名

listBox1.SelectedIndex = fileCount;//自动选中跳转的文件

axWindowsMediaPlayer1.URL = musicfile[fileCount];

axWindowsMediaPlayer1.Ctlcontrols.play();

}

catch (Exception)

{

return;

}

}

a.单曲循环

private void 单曲循环ToolStripMenuItem1_Click(object sender, EventArgs e)

{

CurrentPosition = axWindowsMediaPlayer1.Ctlcontrols.currentPosition;//得到正在播放的位

axWindowsMediaPlayer1.Ctlcontrols.currentPosition

=

CurrentPosition;//接着停止的位置继续播放

State = 1;

axWindowsMediaPlayer1.settings.setMode(\"loop\", true);

}

b.顺序循环

private void 顺序播放ToolStripMenuItem1_Click(object sender, EventArgs e)

{

State = 0;

}

c.列表播放

private void 列表循环ToolStripMenuItem1_Click(object sender, EventArgs e)

{

State = 3;

}

d.随机播放

private void 随机播放ToolStripMenuItem1_Click(object sender, EventArgs e)

{

State = 2;

}

调用播放模式的事件

//改变播放状态,当音乐一首播放完后转入下一首,有线程调用

private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)

{

Thread thread = new Thread(new ThreadStart(PlayThread));

//顺序播放

if

(axWindowsMediaPlayer1.playState==WMPLib.WMPPlayState.wmppsMediaEnded && State == 0)

{

try

{

if (selectOne > fileCount-2)

{

selectOne = 0;

thread.Start();

}

else

{

selectOne++;

thread.Start();

}

}

catch (Exception)

{

return;

}

}

//随机播放

try

{

if (axWindowsMediaPlayer1.playState WMPLib.WMPPlayState.wmppsMediaEnded && State == 2)

{

Random Num = new Random();

int Sum = Num.Next(0, fileCount);

FileInfo f = new FileInfo(musicfile[Sum]);

this.musicname.Text = f.Name;

selectOne = Sum;

thread.Start();

==

}

}

catch (Exception)

{

return;

}

//列表播放

try

{

if (axWindowsMediaPlayer1.playState WMPLib.WMPPlayState.wmppsMediaEnded && State == 3)

{

if (selectOne > fileCount - 2)

{

==

selectOne = 0;

}

else

{

selectOne++;

thread.Start();

}

}

}

catch (Exception)

{

return;

}

}

调用线程

private void PlayThread()

{

PlayAndStop(selectOne);调用音乐播放功能

}

制作界面透明功能

//透明度%

private void toolStripMenuItem7_Click(object sender, EventArgs e)

{

this.Opacity = 0.1;

}

//透明度%

private void toolStripMenuItem8_Click(object sender, EventArgs e)

{

this.Opacity = 0.3;

}

//透明度%

private void toolStripMenuItem9_Click(object sender, EventArgs e)

{

this.Opacity = 0.5;

}

//透明度%

private void toolStripMenuItem10_Click(object sender, EventArgs e)

{

this.Opacity = 0.7;

}

//透明度%

private void toolStripMenuItem11_Click(object sender, EventArgs e)

{

this.Opacity = 0.9;

}

//不透明

private void 不透明ToolStripMenuItem1_Click(object sender, EventArgs e)

{

this.Opacity = 1.0;

}

界面设置

private void 迷ToolStripMenuItem_Click(object sender, EventArgs e)

{

Color color = Color.LightGoldenrodYellow;

this.musicname.BackColor = color;

this.label1.BackColor = color;

this.listBox1.BackColor = color;

this.Up.BackColor = color;

this.PlayOrStop.BackColor = color;

this.Down.BackColor = color;

this.Stop.BackColor = color;

this.MusicText.BackColor = color;

this.menu.BackColor = color;

this.statusStrip1.BackColor = color;

}

版本信息

private void 版本ToolStripMenuItem1_Click(object sender, EventArgs e)

{

MessageBox.Show(\"软件版本:.0 正式版\\n发布时间:.01.19\\n\\n新音乐播放器具备音乐播放、歌曲搜索、主题切换、窗口透明等功能,是一款性能卓越、体积“瘦小”、界面绚丽、操作简单的全功能音乐软件;\\n新音乐播放器满足用户的音乐需要,为您的音乐享受带来更美妙的体验。\\n\\n\\欢迎大家提出宝贵意见!\\n\\n\\\\思远IT学院\\n\\n\\\制作人:童新 联系方式QQ:\"865110135\"关于软件\");

}

上一曲功能:

private void Down_Click(object sender, EventArgs e)

{

try

{

if (selectOne < fileCount-1)

{

selectOne++;

PlayAndStop(selectOne);

}

else

{

selectOne=0;

PlayAndStop(selectOne);

}

}

catch (Exception)

{

return;

}

}

下一曲

private void Up_Click(object sender, EventArgs e)

{

try

{

if (selectOne <= 0)

{

selectOne = fileCount1-1;

PlayAndStop(selectOne);

}

else

{

selectOne--;

PlayAndStop(selectOne);

}

}

catch (Exception)

{

return;

}

}

播放或暂停

private void PlayOrStop_Click(object sender, EventArgs e)

{

try

{

if (stop % 2 == 0)//判断播放或暂停

{

this.PlayOrStop.Text = \"暂停\";

selectOne = this.listBox1.SelectedIndex;

FileInfo f = new FileInfo(musicfile[selectOne]);

this.musicname.Text = f.Name;

string playUrl = musicfile[selectOne];

axWindowsMediaPlayer1.URL = playUrl;

axWindowsMediaPlayer1.Ctlcontrols.currentPosition CurrentPosition;

stop = stop + 1;//当前stop = 3;为暂停状态

axWindowsMediaPlayer1.Ctlcontrols.play();

CurrentPosition = 0;

}

else

{

this.PlayOrStop.Text = \"播放\";

=

CurrentPosition

axWindowsMediaPlayer1.Ctlcontrols.currentPosition;

=

axWindowsMediaPlayer1.Ctlcontrols.pause();

stop = stop - 1;//当前stop = 2;为开始状态

}

}

catch (Exception)

{

return;

}

}

停止音乐播放

private void Stop_Click(object sender, EventArgs e)

{

axWindowsMediaPlayer1.Ctlcontrols.stop();

this.PlayOrStop.Text = \"播放\";

stop = 2;

}

双击播放音乐

//双击事件

private void listBox1_DoubleClick(object sender, EventArgs e)

{

try

{

if (listBox1.SelectedIndex < 0)//listBox1.SelectedIndex 选择的对象

selectOne = 1;

else

selectOne = listBox1.SelectedIndex ;

if (listBox1.Items.Count < 0)//listBox1.Items.Count listbox中文件的数

listBox1.SelectedIndex = 0;

PlayAndStop(selectOne);

}

catch (Exception)

{

{

return ;

}

}

音乐播放器开发文档

此文档仅供参考,不的抄袭或盗用!未经本人允许不许再次上传到网上,此文档是初学者的一点心得希望对你有所帮助!

因篇幅问题不能全部显示,请点此查看更多更全内容