using System;
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 播放器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MusicPlayer.settings.autoStart = false;
//MusicPlayer.URL = @"C:\KuGou\Coyote Ugly - Can't Fight The Moonlight.mp3";
//lsbliebiao.Items.Add("Can't Fight The Moonlight.mp3");
//list.Add(@"C:\KuGou\Coyote Ugly - Can't Fight The Moonlight.mp3");
//lsbliebiao.SelectedIndex = 0;
btnplay.Enabled = false;
stop.Enabled = false;
btnNext.Enabled = false;
btnUp.Enabled = false;
trackBar1.Value = 10;
MusicPlayer.settings.volume = 10;
}
bool falg = true;
List<string> list = new List<string>();
List<string> listlrc = new List<string>();
List<string> list1 = new List<string>();
List<string> list2 = new List<string>();
private void btnplay_Click(object sender, EventArgs e)
{
if (falg)
{
int index = lsbliebiao.SelectedIndex;
if (index != lsbliebiao.SelectedIndex)
{
MusicPlayer.URL = list[index];
}
MusicPlayer.Ctlcontrols.play();
btnplay.Text = "停止";
falg = false;
}
else
{
MusicPlayer.Ctlcontrols.pause();
btnplay.Text = "播放";
falg = true;
}
}
private void stop_Click(object sender, EventArgs e)
{
MusicPlayer.Ctlcontrols.stop();
btnplay.Text = "播放";
falg = true;
lblsong.Text = "歌词";
}
private void btnOpen_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "请选择音乐";
ofd.Multiselect = true;
ofd.Filter = "(*.mp3)|*.mp3";
ofd.InitialDirectory = @"C:\KuGou";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string[] fileName = ofd.FileNames;
for (int i = 0; i < fileName.Length; i++)
{
string name = fileName.Substring(fileName.LastIndexOf('\\') + 1);
lsbliebiao.Items.Add(name);
list.Add(fileName);
string strLrc = fileName.Substring(0,fileName.LastIndexOf ('.'))+".lrc";
if (File.Exists(strLrc))
{
list2.Add(strLrc);
//listlrc.AddRange(File.ReadAllLines(strLrc, Encoding.UTF8));
}
else
{
lblsong.Text = "未找到歌词";
}
}
lsbliebiao.SelectedIndex = list.Count - 1;
MusicPlayer.URL = list[list.Count-1];
//list1 = FormatLrc();
btnplay.Text = "停止";
falg = false;
MusicPlayer.Ctlcontrols.play();
btnplay.Enabled = true;
stop.Enabled = true;
btnNext.Enabled = true;
btnUp.Enabled = true;
}
}
private List<string> FormatLrc()
{
List <string> listsonglrc = new List<string> ();
for (int i = 0; i < listlrc.Count; i++)
{
string[] lrc1 = listlrc .Split (new char []{'[',']'}, StringSplitOptions.RemoveEmptyEntries );
if (lrc1.Length > 1)
{
for (int j = 0; j < lrc1.Length-1;j++ )
{
listsonglrc.Add(lrc1[j]+'|'+lrc1[lrc1.Length -1]);
}
}
}
for (int i = 0; i < listsonglrc.Count; i++)
{
for (int j = 0; j < listsonglrc.Count - 1 - i; j++)
{
if (string .Compare (listsonglrc[j ],listsonglrc [j +1])>0)
{
string temp = listsonglrc[j];
listsonglrc[j] = listsonglrc[j + 1];
listsonglrc[j + 1] = temp;
}
}
}
return listsonglrc;
}
private void lsbliebiao_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (lsbliebiao.Items.Count > 0)
{
int index = lsbliebiao.SelectedIndex;
MusicPlayer.URL = list[index];
MusicPlayer.Ctlcontrols.play();
btnplay.Text = "停止";
falg = false;
}
}
private void btnNext_Click(object sender, EventArgs e)
{
int index = lsbliebiao.SelectedIndex;
index++;
if (index == lsbliebiao.Items.Count)
{
index = 0;
}
lsbliebiao.SelectedIndex = index;
MusicPlayer.URL = list[index];
MusicPlayer.Ctlcontrols.play();
btnplay.Text = "停止";
}
private void btnUp_Click(object sender, EventArgs e)
{
int index = lsbliebiao.SelectedIndex;
index--;
if (index < 0)
{
index = lsbliebiao.Items.Count - 1;
}
lsbliebiao.SelectedIndex = index;
MusicPlayer.URL = list[index];
MusicPlayer.Ctlcontrols.play();
btnplay.Text = "停止";
}
private void btnMu_Click(object sender, EventArgs e)
{
if (btnMu.Text == "静音")
{
MusicPlayer.settings.mute = true;
btnMu.Text = "取消静音";
}
else
{
MusicPlayer.settings.mute = false;
btnMu.Text ="静音";
}
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
MusicPlayer.settings.volume = trackBar1.Value;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (MusicPlayer.currentMedia != null)
{
lbltime.Text = MusicPlayer.Ctlcontrols.currentPositionString;
lblname.Text = MusicPlayer.currentMedia.name;
}
if (list1 .Count >0&&MusicPlayer.currentMedia !=null )
{
string time = MusicPlayer.Ctlcontrols.currentPositionString;
for (int i = 0; i < list1.Count-1;i++)
{
if (string.Compare(list1, time) < 0 && string.Compare(time, list1[i + 1]) < 0)
{
lblsong.Text = list1.Split('|')[1];
}
}
}
}
private void MusicPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (list2.Count>0)
{
listlrc.Clear();
listlrc.AddRange(File.ReadAllLines(list2[lsbliebiao.SelectedIndex], Encoding.UTF8));
list1.Clear();
}
else
{
list1.Clear();
}
list1 = FormatLrc();
if (MusicPlayer.playState ==WMPLib.WMPPlayState .wmppsMediaEnded)
{
int index = lsbliebiao.SelectedIndex;
index++;
if (index == lsbliebiao.Items.Count)
{
index = 0;
}
lsbliebiao.SelectedIndex = index;
MusicPlayer.URL = list[index];
}
if (MusicPlayer.playState == WMPLib.WMPPlayState.wmppsReady)
{
try
{
MusicPlayer.Ctlcontrols.play();
}
catch
{ }
}
}
}
}
|