用基本C#知识实现制作一个音乐播放器
前言
写这个博客并不是说我的作品多么高级或完美,只是希望能在一些功能方面给你们一些启发,能帮助到你们做出真正好的程序,这就足够了
话不多说,让我们开始吧~~~
截图示意

总体思路
首先呢,选择这种无边框窗口,我们会发现移动窗口成了问题,所以呢,就需要特意对主窗口写事件,即按下时,移动时,松开时等各是怎样,这个应该不难。
···
Point downpoint;
Point movepoint;
bool ismoving;
//实现鼠标控制窗口移动
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
downpoint = e.Location;
ismoving = true;
}
private void timermove_Tick(object sender, EventArgs e)
{
}
//实现鼠标控制窗口移动(2)
private void mainForm_MouseMove(object sender, MouseEventArgs e)
{
if (ismoving == true)
{
movepoint = e.Location;
this.Location = new Point(
this.Location.X + (movepoint.X - downpoint.X),
this.Location.Y + (movepoint.Y - downpoint.Y)
);
}
}
//鼠标按键抬起窗口停止移动
private void mainForm_MouseUp(object sender, MouseEventArgs e)
{
ismoving = false;
}
····
然后就是引入WindowsMediaPlayer后我们需要实现音频文件资源的打开,这里我设置了文件过滤器,只能打开“.MP3”“.MP4”“.flac”三种文件,同时实现了基本功能,例如点击播放器的播放(暂停)键时图标的变化、歌曲继续、暂停等(所以我们用到一个bool型变量方便判断)
···
bool isplaying = true;
string a;//歌曲总时间(字符串)
//点击播放:加载歌词
private void start_Click(object sender, EventArgs e)
{
if (tick == 0)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "mp3音乐|*.mp3|mp4音乐|*.mp4|flac文件|*.flac";//设置文件过滤,使之只能打开.mp3/.mp4/flac三种格式的音频文件
ofd.ShowDialog();
string fileName = ofd.FileName;
media.URL = fileName;
media.Ctlcontrols.play();
// start_Click(object sender, EventArgs e);
timer2.Enabled = true;
}
tick = 1;
timer1.Enabled = true;
Encoding encode = Encoding.GetEncoding("GB2312");
FileStream fs = new FileStream("陈一发儿-童话镇.lrc", FileMode.Open);
StreamReader sr = new StreamReader(fs, encode);
string s = sr.ReadLine();
//int xPos = 100;
int yPos = 50;
string a= media.currentMedia.durationString;
stime.Text = a;
while (s != null)
{
s = sr.ReadLine();
if(s==null)
{
break;
}
if (s.Equals(""))
{
continue;
}
Lyric1 lyric =new Lyric1();
lyric.minute =int.Parse(s.Substring(1,2));
lyric.second = int.

本文分享了使用C#从零开始制作音乐播放器的过程,包括窗口无边框设计、鼠标控制窗口移动、音频文件资源打开、播放控制、歌词同步显示及进度条实现等关键功能。
1635

被折叠的 条评论
为什么被折叠?



