using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace kuWoMusic
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Lyric lyric = new Lyric();
//是否播放
bool isPlaying = false;
private void picPlay_Click(object sender, EventArgs e)
{
isPlaying = !isPlaying;
if(isPlaying)
{
axWindowsMediaPlayer1.Ctlcontrols.play();
//更换为pause ||
picPlay.BackgroundImage = Properties.Resources.pause;
//播放mp3
axWindowsMediaPlayer1.URL = lstMusicName[curMusicIndex % lstMusicName.Count] + ".mp3";
//载入歌词
lyric.Load("Lyric//" + lstMusicName[curMusicIndex % lstMusicName.Count] + ".lrc");
this.BackgroundImage = Image.FromFile("bg//" + lstMusicName[curMusicIndex % lstMusicName.Count] + ".jpg");
//更换背景图
picPlay.BackgroundImage = Properties.Resources.play;
//启动定时器
timer1.Enabled = true;
}
else
{
//更换为play>
axWindowsMediaPlayer1.Ctlcontrols.pause();
picPlay.BackgroundImage = Properties.Resources.play;
}
}
//存放歌曲名称列表
List<string> lstMusicName = new List<string>();
//当前播放歌曲的索引
int curMusicIndex = 0;
/// <summary>
/// 加载资源
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
// 添加歌曲名
lstMusicName.Add("陈一发儿-童话镇");
lstMusicName.Add("大壮-我们不一样");
lstMusicName.Add("金南玲-逆流成河");
lstMusicName.Add("薛之谦-演员");
lstMusicName.Add("音阙诗听-红昭愿");
}
//头像
Image img;
//是否显示弹幕
bool isShowBarrage = false;
//弹幕位置
int barrageXPos = 0;
//实时显示歌曲时间
private void timer1_Tick(object sender, EventArgs e)
{
//获取播放歌曲的当前时间
double curTime = axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
//根据当前的时间找到对应的歌词
lbltext.Text = findLyric(curTime);
//进度条循环
musicBar.CurTime = axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
musicBar.duration = axWindowsMediaPlayer1.currentMedia.duration;
//时间进度
lblLyric.Text = axWindowsMediaPlayer1.Ctlcontrols.currentPositionString + " / " + axWindowsMediaPlayer1.currentMedia.durationString;
//弹幕移动
barrageXPos -= 5;
this.Invalidate();
}
//显示歌词
public string findLyric(double curTime)
{
int curIdx = 0;
//根据当前的时间找到对应的歌词
for (int i = 0; i < lyric.lstLyric.Count - 1; i++)
{
if (lyric.lstLyric[i].time < curTime && lyric.lstLyric[i + 1].time > curTime)
{
curIdx = i;
break;
}
}
return lyric.lstLyric[curIdx].context; ;
}
Point startPoint = new Point();
bool isMoved = false;//鼠标是否点下去
//鼠标点下去,随着鼠标移动
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
isMoved = true;
//鼠标点下去的位置
startPoint =e.Location;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if(isMoved)
{
//鼠标移动,位置不断变化
Point movePoint = e.Location;
int offsetX = movePoint.X - startPoint.X;
int offsetY = movePoint.Y - startPoint.Y;
//改变Form位置
this.Location = new Point(this.Location.X + offsetX, this.Location.Y + offsetY);
}
}
//鼠标不点,窗口不移动
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
isMoved = false;
}
//画弹幕
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
if(isShowBarrage)
{
/* g.DrawImage(img, barrageXPos - 20, 100);*/
g.DrawString("<<<<<<<I Love SWPU", new Font("微软雅黑", 20), Brushes.Orange, barrageXPos + 60, 100);
g.DrawString("<<<<<<<I Love SWPU", new Font("微软雅黑", 20), Brushes.Gold, barrageXPos + 0, 140);
g.DrawString("<<<<<<<I Love SWPU", new Font("微软雅黑", 20), Brushes.Gray, barrageXPos + 50, 180);
}
}
private void picNext_Click(object sender, EventArgs e)
{
//播放mp3
axWindowsMediaPlayer1.URL = lstMusicName[curMusicIndex % lstMusicName.Count] + ".mp3";
//载入歌词
lyric.Load("Lyric//" + lstMusicName[curMusicIndex % lstMusicName.Count] + ".lrc");
this.BackgroundImage = Image.FromFile("bg//" + lstMusicName[curMusicIndex % lstMusicName.Count] + ".jpg");
//更换背景图
picPlay.BackgroundImage = Properties.Resources.play;
//启动定时器
timer1.Enabled = true;
isPlaying = true;
//注意下标
curMusicIndex++;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
//播放mp3
axWindowsMediaPlayer1.URL = lstMusicName[curMusicIndex % lstMusicName.Count] + ".mp3";
//载入歌词
lyric.Load("Lyric//" + lstMusicName[curMusicIndex % lstMusicName.Count] + ".lrc");
this.BackgroundImage = Image.FromFile("bg//" + lstMusicName[curMusicIndex % lstMusicName.Count] + ".jpg");
//更换背景图
picPlay.BackgroundImage = Properties.Resources.stop;
//启动定时器
timer1.Enabled = true;
isPlaying = true;
//注意下标
curMusicIndex--;
if(curMusicIndex < 0)
{
curMusicIndex += lstMusicName.Count;
}
}
}
}
歌词类
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace kuWoMusic
{
struct LyricLine
{
public double time;//时间:秒
public string context;//内容
}
//专门处理歌词的类
class Lyric
{
//字段
public List<LyricLine> lstLyric = new List<LyricLine>();
//方法
public void Load(String fileName)
{
//清空以前的内容
lstLyric.Clear();
//歌词显示出来,从文件中读取内容
FileStream fs = new FileStream(fileName, FileMode.Open);
Encoding encode = Encoding.GetEncoding("GB2312");//编码模式
StreamReader sr = new StreamReader(fs, encode);
string s;
//循环读出所有的歌词行
while ((s = sr.ReadLine()) != null)
{
if (s.Equals(""))
{
continue;
}
//提取时间
string strTimeMin = s.Substring(1, 2);
string strTimeSec = s.Substring(4, 5);
double time = int.Parse(strTimeMin) * 60 + double.Parse(strTimeSec);
//提取内容
string strContext = s.Substring(10);
//保存歌词
LyricLine ll = new LyricLine();
ll.time = time;
ll.context = strContext;
lstLyric.Add(ll);
}
sr.Close();
fs.Close();
}
//获取当前歌词内容的方法
public String getCurLyric(double curTime)
{
int curIdx = 0;
for (int i = 0; i < lstLyric.Count - 1; i++)
{
if (curTime > lstLyric[i].time && curTime < lstLyric[i + 1].time)
{
curIdx = i;
break;
}
}
return lstLyric[curIdx].context;
}
}
}
自定义控件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace musicPb
{
public partial class UserControl1: UserControl
{
public UserControl1()
{
InitializeComponent();
}
private double curTime = 0;//当前时间
public double CurTime
{
get
{
return curTime;
}
set
{
curTime = value;
this.Invalidate();//让控件界面无效,调用paint
}
}
public double duration;
//绘制
private void UserControl1_Paint(object sender, PaintEventArgs e)
{
//画家
Graphics g = e.Graphics;
//底色
Pen pen = new Pen(Color.Gray, 7);
g.DrawLine(pen, 0, this.Height / 2, this.Width, this.Height / 2);
//当前位置
int pos = 0;
if (duration > 0)
{
//当前时间
pos = (int)(curTime / duration * this.Width);
}
//黄色
Pen penFocus = new Pen(Color.SkyBlue, 7);
g.DrawLine(penFocus, 0, this.Height / 2, pos, this.Height / 2);
//绘制图
int r = 5;
int x = pos - r;
int y = this.Height / 2 - r;
int w = 2 * r;
int h = 2 * r;
g.DrawEllipse(penFocus, x, y, w, h);
}
}
}