通过本文主要向大家介绍了c#音乐播放器源代码,c#制作音乐播放器,c#编写音乐播放器,用c#编写音乐播放器,c#做音乐播放器等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
本文所述为一个由C#编写的音乐播放器的主Form代码,里面有一些小技巧还是不错的,现共享给大家参考一下。里面有播放器背景设置、线程定义、调用读取文件目录方法、播放时间计数器、设置LV背景、获取播放歌曲、播放按钮,切换播放or暂停、切换歌曲到下一首,调用切歌方法、显示播放列表、歌词局中、播放窗体最小化隐藏到托盘设置、进度条滚动模块、从歌曲列表中删除文件等等功能。且各个功能模板均备有较为详细的注释,便于大家阅读理解。
程序主要代码如下:
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;
using System.Drawing.Imaging;
using System.Threading;
namespace MyMusicBox
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
}
Song mySong = null;//播放的歌曲对象
ListViewItem itemLrc;//打开歌词路径项
Bitmap bm ;//用于动态设置LV背景
public SavingInfo sa = null;//持久化信息类
Thread thread;//后台线程
#region 加载事件
private void MainForm_Load(object sender, EventArgs e)
{
//调用读取配置文件方法
Path.ReadPath();
//窗体背景设置为配置文件中的图片
this.BackgroundImage = Image.FromFile(Path.bgPath);
//调用读取文件目录方法
Path.ReadSong();
//读取播放列表
sa = new SavingInfo();
sa.LoadPlayList();
//绑定LV
BindAllLV();
BindPlayLV();
SetLV();
SetLrcBG();
timer1.Enabled = Path.Playing;
//显示时间
lblTime.Text = System.DateTime.Now.ToString();
//两个lbl用来根据歌曲总长和播放长度显示进度,设置位置重叠
lblRuning.Location = lblLong.Location;
//启动线程
thread = new Thread(Renovate);
thread.IsBackground = true;
thread.Start();
}
#endregion
#region 后台刷新
/// <summary>
/// 时间计数状态等刷新
/// </summary>
private void Renovate()
{
//while (true)
//{
// //计数
// Count();
// //获取当前时间
// lblTime.Text = System.DateTime.Now.ToString();
// //无歌曲提示,如果播放列表为空且当前播放列表为显示状态
// if (lvPlay.Items.Count == 0 && lvPlay.Visible)
// lblTs.Visible = true;
// else
// lblTs.Visible = false;
// Thread.Sleep(1000);
//}
}
#endregion
#region 设置LV背景
/// <summary>
/// 设置LV背景
/// </summary>
public void SetLV()
{
bm = new Bitmap(this.BackgroundImage, this.Width, this.Height);
//绘制矩形,定义起始位置和宽高
Rectangle r = new Rectangle(lvPlay.Location.X, lvPlay.Location.Y, lvPlay.Width, lvSong.Height);
//按矩形尺寸和起始位置截取bm的一部分
bm= bm.Clone(r,bm.PixelFormat);
//把截取到的图片设置为lv背景,达到与主窗体背景完美契合的效果
lvSong.BeginUpdate();
lvSong.BackgroundImage = (Image)bm;
lvSong.EndUpdate();
lvPlay.BeginUpdate();
lvPlay.BackgroundImage = (Image)bm;
lvPlay.EndUpdate();
}
#endregion
#region 获取歌曲
/// <summary>
/// 获取播放歌曲
/// </summary>
private void GetSong()
{
//接收播放歌曲方法,获得一个歌曲对象
this.mySong = PlayList.Play();
//如果歌曲对象不为空
if (mySong != null)
{
//播放器路径设置为该歌曲路径
wmp.URL = mySong.FileName;
//调用方法,显示歌曲名、歌手
lblName.Text = PlayList.GetName();
lblSinger.Text = PlayList.GetSinger();
mySong.SongState = SongPlayState.played;//修改播放状态
//播放按钮图片修改为暂停图片
btnPlay.Image = Image.FromFile("Images\\stop.png");
try
{
//读取歌词路径
Path.ReadLrc();
AddLrc();
//启动一个定时器,此定时器只控制歌词轮播
timer2.Start();
}
catch (Exception)
{
LrcNull();
itemLrc = new ListViewItem();
itemLrc.SubItems.Add("找不到该歌曲歌词文件!");
itemLrc.ForeColor = Color.Blue;
itemLrc.Font = new Font("微软雅黑", 14.25F, ((FontStyle)((FontStyle.Bold | FontStyle.Underline))), GraphicsUnit.Point, ((byte)(134)));
lvLrc.Items.Add(itemLrc);
}
}
}
#endregion
#region 主定时器
//定时器1
private void timer1_Tick(object sender, EventArgs e)
{
if (Path.songPath == "")
{
timer1.Enabled = false;
return;
}
//如果当前无播放歌曲,调用获取歌曲的方法
if (this.mySong == null)
{
GetSong();
}
//自动下一首,如果当前播放完毕
if (this.wmp.playState == WMPLib.WMPPlayState.wmppsStopped)
{
this.mySong = null; // 将歌曲设为空
Path.lrcPath = "";
Path.dicLrc.Clear();
PlayModel();
PlayList.PlayNext();
Thread.Sleep(1000);
}
// 切歌,当前有播放歌曲且播放状态为cut
if (this.mySong != null && this.mySong.SongState == SongPlayState.cut)
{
this.wmp.URL = "";//置空
timer2.Stop();
Path.dicLrc.Clear();
Path.lrcPath = "";
this.mySong = null;
}
//如果当前有播放歌曲,需要获取的一些属性
if (wmp.URL!="")
{
//设置当前播放歌曲颜色
SongColor();
//获取音量控件显示值,根据控制刻度数量计算
this.trackBar1.Value = wmp.settings.volume / 10;
//歌曲时间显示,一个是总长度,一个是已播放长度,字符串类型
lblAll.Text = wmp.currentMedia.durationString;
lblRun.Text = wmp.Ctlcontrols.currentPositionString;
//进度条,使用了两个lbl控件,歌曲长度/已播放长度=lbl1的宽/lbl2的宽
//乘1000为防止数据过小出现错误
double temp = (wmp.currentMedia.duration*1000) / (wmp.Ctlcontrols.currentPosition*1000);
double width = lblLong.Width;
double avg = width / temp;
//判断>1为了防止avg数值小于int界限
if(avg>1)
lblRuning.Width = Convert.ToInt32(avg);
picRun.Left = lblRuning.Right;
//托盘显示播放歌曲
this.notifyIcon1.Text =lblName.Text + lblSinger.Text;
}
}
#endregion
#region 播放按钮
/// <summary>
/// 播放按钮,切换播放or暂停
/// </summary>
private void btnPlay_Click(object sender, EventArgs e)
{
if(!timer1.Enabled)
timer1.Start();
//如果当前为正在播放
if (wmp.playState == WMPLib.WMPPlayState.wmppsPlaying)
{
//暂停播放
wmp.Ctlcontrols.pause();
//按钮图片修改为播放
btnPlay.Image = Image.FromFile("Images\\play.png");
//停止控制歌词滚动的定时器
if (timer2.Enabled)
timer2.Stop();
}
else if (wmp.playState == WMPLib.WMPPlayState.wmppsPaused)
{
//开始播放
wmp.Ctlcontrols.play();
//按钮图片修改为暂停
btnPlay.Image = Image.FromFile("Images\\stop.png");
//启动歌词滚动
if (!timer2.Enabled&&Path.dicLrc.Count>0)
timer2.Start();
}
}
#endregion
#region 切歌
//切换下一首,调用切歌方法
private void btnRight_Click(object sender, EventArgs e)
{
PlayList.Cut(false);
}
//上一首,调用切歌方法
private void btnLeft_Click(object sender, EventArgs e)
{
PlayList.Cut(true);
}
#endregion
#region 音量
//静音,静音!=音量为0
private void btnModel_Click(obj

