using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WinMediaPlayer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnPlay_Click(object sender, EventArgs e)
{
MusicPlayer.Ctlcontrols.play();
}
private void btnPuse_Click(object sender, EventArgs e)
{
MusicPlayer.Ctlcontrols.pause();
}
private void btnStop_Click(object sender, EventArgs e)
{
MusicPlayer.Ctlcontrols.stop();
}
private void Form1_Load(object sender, EventArgs e)
{
MusicPlayer.settings.autoStart = false;
//MusicPlayer.URL = @"C:\Users\Administrator\Desktop\Net\1初级--net基础\第10天--高级--面向对象多态\2\1、复习~1.avi";
label1.Image = Image.FromFile("Play.jpg");
}
bool flag = true;
private void btnPlayOrPause_Click(object sender, EventArgs e)
{
try
{
if (btnPlayOrPause.Text == "播放")
{
if (flag)
{
MusicPlayer.URL = listPath[lbMusics.SelectedIndex];
}
MusicPlayer.Ctlcontrols.play();
btnPlayOrPause.Text = "暂停";
}
else if (btnPlayOrPause.Text == "暂停")
{
MusicPlayer.Ctlcontrols.pause();
btnPlayOrPause.Text = "播放";
flag = false;
}
}
catch { }
}
private void btnStopTwo_Click(object sender, EventArgs e)
{
MusicPlayer.Ctlcontrols.stop();
}
/// <summary>
/// 选择音乐文件,并加载到listBox中
/// </summary>
List<string> listPath = new List<string>();
private void btnOpen_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "音乐文件|*.mp3|WAV文件|*.WAV|所有文件|*.*";
//ofd.InitialDirectory = @"C:\Users\Administrator\Desktop\Music";
ofd.InitialDirectory = "Music";
ofd.Multiselect = true;
ofd.ShowDialog();
//将选中的文件保存到数组中
string[] strPath = ofd.FileNames;
//循环将选中的音乐文件显示在ListBox中
for (int i = 0; i < strPath.Length; i++)
{
lbMusics.Items.Add(strPath[i].Substring(strPath[i].LastIndexOf('\\') + 1));
listPath.Add(strPath[i]);
}
//加载完毕后让listBox中默认选择第一行数据
lbMusics.SelectedIndex = 0;
}
catch { }
}
/// <summary>
/// 双击listBox中的音乐列表播放音乐
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lbMusics_DoubleClick(object sender, EventArgs e)
{
try
{
if (lbMusics.Items.Count == 0)
{
MessageBox.Show("请选择音乐文件...");
return;
}
if (lbMusics.SelectedIndex != -1)
{
string strSelectPath = listPath[lbMusics.SelectedIndex];
MusicPlayer.URL = strSelectPath;
MusicPlayer.Ctlcontrols.play();
btnPlayOrPause.Text = "暂停";
ExistsLrc(listPath[lbMusics.SelectedIndex]);
}
}
catch { }
}
/// <summary>
/// 单击播放上一曲
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPre_Click(object sender, EventArgs e)
{
try
{
int index = lbMusics.SelectedIndex;
if (index == 0)
{
index = listPath.Count;
}
index--;
lbMusics.SelectedIndex = index;
MusicPlayer.URL = listPath[index];
MusicPlayer.Ctlcontrols.play();
btnPlayOrPause.Text = "暂停";
}
catch { }
}
/// <summary>
/// 单击播放下一曲
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnNext_Click(object sender, EventArgs e)
{
try
{
int index = lbMusics.SelectedIndex;
index++;
if (index == listPath.Count)
{
index = 0;
}
lbMusics.SelectedIndex = index;
MusicPlayer.URL = listPath[index];
MusicPlayer.Ctlcontrols.play();
btnPlayOrPause.Text = "暂停";
}
catch { }
}
/// <summary>
/// 右键删除歌曲列表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void contextMenuStrip1_Click(object sender, EventArgs e)
{
try
{
if (lbMusics.SelectedItems.Count != 0)
{
for (int i = 0; i < lbMusics.SelectedItems.Count; i++)
{
listPath.RemoveAt(lbMusics.SelectedIndex);
lbMusics.Items.RemoveAt(lbMusics.SelectedIndex);
}
}
}
catch { }
}
/// <summary>
/// 切换静音与放音
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void label1_Click(object sender, EventArgs e)
{
try
{
if (label1.Tag.ToString() == "1")
{
MusicPlayer.settings.mute = true;
label1.Image = Image.FromFile("Stop.jpg");
label1.Tag = "2";
}
else if (label1.Tag.ToString() == "2")
{
MusicPlayer.settings.mute = false;
label1.Image = Image.FromFile("Play.jpg");
label1.Tag = "1";
}
}
catch { }
}
/// <summary>
/// 自动播放下一曲
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
if (MusicPlayer.playState == WMPLib.WMPPlayState.wmppsPlaying)
{
if (double.Parse(MusicPlayer.currentMedia.duration.ToString()) <= (double.Parse(MusicPlayer.Ctlcontrols.currentPosition.ToString()) + 1))
{
try
{
int index = lbMusics.SelectedIndex;
index++;
if (index == listPath.Count)
{
index = 0;
}
lbMusics.SelectedIndex = index;
MusicPlayer.URL = listPath[index];
MusicPlayer.Ctlcontrols.play();
btnPlayOrPause.Text = "暂停";
}
catch { }
}
}
}
void ExistsLrc(string lrcPath)
{
try
{
lrcPath += ".lrc";
if (File.Exists(lrcPath))
{
string[] lrcText = File.ReadAllLines(lrcPath, Encoding.Default);
//格式化歌词
FormateLrc(lrcText);
}
else
{
lblLrc.Text = "=================未找到歌词=================";
}
}
catch { }
}
List<double> listTime = new List<double>();
List<string> listLrc = new List<string>();
void FormateLrc(string[] lrcText)
{
try
{
for (int i = 0; i < lrcText.Length; i++)
{
string[] lrcTemp = lrcText[i].Split(new char[] { '[', ']' }, StringSplitOptions.RemoveEmptyEntries);
string[] newLrcTemp = lrcTemp[0].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
double time = (double.Parse(newLrcTemp[0]) * 60) + double.Parse(newLrcTemp[1]);
//将获得的时间保存到集合中
listTime.Add(time);
//将时间对应的歌词保存到集合中
listLrc.Add(lrcTemp[1]);
}
}
catch { }
}
private void timer2_Tick(object sender, EventArgs e)
{
try
{
for (int i = 0; i < listTime.Count; i++)
{
if (MusicPlayer.Ctlcontrols.currentPosition >= listTime[i] && MusicPlayer.Ctlcontrols.currentPosition < listTime[i + 1])
{
lblLrc.Text = listLrc[i];
}
}
}
catch { }
}
}
}