winform音乐播放器的实现

时间:2021-08-19 10:14:36

1.皮肤的实现,需要用到skinEngline,而我们的工具箱没有

 请下载IrisSkin4.dll直接拖拽到工具箱里就会有skinEngline;

在启动load方法中写:skinEngine1.SkinFile = @"绝对路径";就可以设置默认的皮肤!皮肤自己下载,后最ssk,随意先写个

Widows Media Player组件需要载工具箱右键“选择项”添加com组件

其中用到的工具箱不知道的可以提问

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace 音乐播放器1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> list=new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
//执行之前我们换一个皮肤,来一个默认皮肤
skinEngine1.SkinFile = @"pifu_ssk\SteelBlack.ssk";
//给当前pictureBox赋值默认图片
pictureBox1.Image = Image.FromFile(@"meinv\6.jpg");
//设置图片在pictureBox的布局
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

//取消自动播放功能,如果我们先赋值路径的话我们就会自动线播放音乐
musicPler.settings.autoStart = false;

<strong>//默认一个播放路径,音乐放到音乐播放器1\音乐播放器1\bin\Debug目录下</strong>
musicPler.URL = @"孙思怡 - 因为爱上你.mp3";
}

int i = 0;
private void btnChange_Click(object sender, EventArgs e)
{
//把所用的皮肤读取进来
string[] stylePath = Directory.GetFiles(@"pifu_ssk");//直接就可以读取
i++;
if (i == stylePath.Length)
{
i = 0;
}
skinEngine1.SkinFile = stylePath[i];
}

private void btnPlayorstar_Click(object sender, EventArgs e)
{
if (btnPlayorstart.Text == "播放")
{
//播放器开始播放
musicPler.Ctlcontrols.play();
//如果是播放则变为暂停
btnPlayorstart.Text = "暂停";
}
else if (btnPlayorstart.Text == "暂停")
{
musicPler.Ctlcontrols.pause();
btnPlayorstart.Text = "播放";
}
}

int j = 0;
private void timer1_Tick(object sender, EventArgs e)
{
//每隔指定的时间更换图片
string[] imgsPath = Directory.GetFiles(@"meinv");
j++;
if (j == imgsPath.Length)
{
j = 0;
}
pictureBox1.Image = Image.FromFile(imgsPath[j]);
}

//停止是暂停到开始的时候,
private void start_Click(object sender, EventArgs e)
{
musicPler.Ctlcontrols.stop();
}

//选择音乐
private void xuanzeyinyue_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
//在show出来志气啊,设置一下对话框的属性
ofd.Title = "请选择要播放的音乐我文件";
//设置对话框可以多选
ofd.Multiselect = true;
//设置其文件类型
ofd.Filter = "音乐文件|*.mp3|所有文件|*.*";
//设置文件打开的初始路径
ofd.InitialDirectory = @"F:\音乐";
ofd.ShowDialog();

//获得我们在对话框中选择文件的全路径
string[] filePath = ofd.FileNames;
//根据全路径截取文件名加载到listbox中
//需要全部存储起来
for (int i = 0; i < filePath.Length; i++)
{

list.Add(filePath[i]);
listBox1.Items.Add(Path.GetFileName(filePath[i]));
}
}
//双击播放
private void listBox1_DoubleClick(object sender, EventArgs e)
{
if (listBox1.SelectedIndex == -1)
{
return;
}
//当我们双击以后,我们需要找到双击的文件名所对应的的
//全路径<list>找到全路径
musicPler.URL = list[listBox1.SelectedIndex];
musicPler.Ctlcontrols.play();
//加载歌词
Load_geci();
}


private void button1_Click_1(object sender, EventArgs e)
{
//获得当前的索引
int index = listBox1.SelectedIndex;
listBox1.SelectedIndices.Clear();
//上一曲是--
index--;
//我在第一首
if (index < 0)
{
index = listBox1.Items.Count - 1;
}
//将当前选中先得索引将改变后的赋上去
listBox1.SelectedIndex = index;
//通过索引
musicPler.URL = list[index];
musicPler.Ctlcontrols.play();
}

private void button2_Click_1(object sender, EventArgs e)
{
int index = listBox1.SelectedIndex;
listBox1.SelectedIndices.Clear();
if (index == -1)
{
index = 0;
}
index++;
if (index == listBox1.Items.Count)
{
index = 0;
}
listBox1.SelectedIndex = index;
musicPler.URL = list[index];
musicPler.Ctlcontrols.play();
}

//右键菜单,删除
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
//获取要清除歌曲的数量
int count = listBox1.SelectedItems.Count;
for(int i=0;i<count;i++)
{
//list集合和listbox列表,先删除集合
//因为我们是根据索引来删除的,所以应该先删除
//集合,这样就保证了要删除数据一直都在
list.RemoveAt(listBox1.SelectedIndex);
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}

}

private void timer2_Tick(object sender, EventArgs e)
{
if (musicPler.playState == WMPLib.WMPPlayState.wmppsPlaying)
{
//显示歌曲信息
label1.Text = musicPler.Ctlcontrols.currentPosition + "\r\n" + musicPler.Ctlcontrols.currentPositionString + "\r\n" + musicPler.currentMedia.duration + "\r\n" + musicPler.currentMedia.durationString;

//如果当前播放的总时间减去播放总时间--时间差
if ((musicPler.currentMedia.duration - musicPler.Ctlcontrols.currentPosition) <= 1)
{
int index = listBox1.SelectedIndex;
listBox1.SelectedIndices.Clear();
if (index == -1)
{
index = 0;
}
index++;
if (index == listBox1.Items.Count)
{
index = 0;
}
listBox1.SelectedIndex = index;
musicPler.URL = list[index];
musicPler.Ctlcontrols.play();

}
// Ready--开始play Playing Pause Ended--跳转 Stop
}
}

private void musicPler_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
//当播放器状态改变时候 判断
if (musicPler.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
{
//下一曲
//显示歌曲信息
label1.Text = musicPler.Ctlcontrols.currentPosition + "\r\n" + musicPler.Ctlcontrols.currentPositionString + "\r\n" + musicPler.currentMedia.duration + "\r\n" + musicPler.currentMedia.durationString;

//如果当前播放的总时间减去播放总时间--时间差
if ((musicPler.currentMedia.duration - musicPler.Ctlcontrols.currentPosition) <= 1)
{
int index = listBox1.SelectedIndex;
listBox1.SelectedIndices.Clear();
if (index == -1)
{
index = 0;
}
index++;
if (index == listBox1.Items.Count)
{
index = 0;
}
listBox1.SelectedIndex = index;
musicPler.URL = list[index];
}

if(musicPler.playState==WMPLib.WMPPlayState.wmppsReady)
{
try{
musicPler.Ctlcontrols.play();
}
catch{}
}

}

}


void Load_geci()
{
string songPath = list[listBox1.SelectedIndex];
songPath += ".lrc";
if (File.Exists(songPath))
{
string[] lrc = File.ReadAllLines(songPath, Encoding.Default);
format(lrc);
}
else
{
//不存在
label2.Text = "歌词没找到";
}
}
List<double> listtime = new List<double>();
List<string> listgeci = new List<string>();
void format(string[] lrc)
{
for (int i = 0; i < lrc.Length; i++)
{
string[] lrctem=lrc[i].Split(new char[] { '[', ']' }, StringSplitOptions.RemoveEmptyEntries);

listgeci.Add(lrctem[1]);

string[] lrcnewtem=lrctem[0].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);

double time=double.Parse(lrcnewtem[0]) * 60 + double.Parse(lrcnewtem[1]);
listtime.Add(time);

}
}
}
}