音乐操作类

 public class clsMCI
{
public clsMCI()
{
//
// TODO: 在此处添加构造函数逻辑
//
} //定义API函数使用的字符串变量
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = )]
private string Name = "";
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = )]
private string durLength = "";
[MarshalAs(UnmanagedType.LPTStr, SizeConst = )]
private string TemStr = "";
int ilong;
//定义播放状态枚举变量
public enum State
{
mPlaying = ,
mPuase = ,
mStop =
};
//结构变量
public struct structMCI
{
public bool bMut;
public int iDur;
public int iPos;
public int iVol;
public int iBal;
public string iName;
public State state;
}; public structMCI mc = new structMCI(); //取得播放文件属性
public string FileName
{
get
{
return mc.iName;
} set
{
//ASCIIEncoding asc = new ASCIIEncoding();
try
{
TemStr = "";
TemStr = TemStr.PadLeft(, Convert.ToChar(" "));
Name = Name.PadLeft(, Convert.ToChar(" "));
mc.iName = value;
ilong = APIClass.GetShortPathName(mc.iName, Name, Name.Length);
Name = GetCurrPath(Name);
//Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
Name = "open " + Convert.ToChar() + Name + Convert.ToChar() + " alias media";
ilong = APIClass.mciSendString("close all", TemStr, TemStr.Length, );
ilong = APIClass.mciSendString(Name, TemStr, TemStr.Length, );
ilong = APIClass.mciSendString("set media time format milliseconds", TemStr, TemStr.Length, );
mc.state = State.mStop;
}
catch
{
MessageBox.Show("出错错误!");
}
}
} //播放
public void play()
{
TemStr = "";
TemStr = TemStr.PadLeft(, Convert.ToChar(" "));
APIClass.mciSendString("play media", TemStr, TemStr.Length, );
mc.state = State.mPlaying;
}
//停止
public void StopT()
{
TemStr = "";
TemStr = TemStr.PadLeft(, Convert.ToChar(" "));
ilong = APIClass.mciSendString("close media", TemStr, , );
ilong = APIClass.mciSendString("close all", TemStr, , );
mc.state = State.mStop; } public void Puase()
{
TemStr = "";
TemStr = TemStr.PadLeft(, Convert.ToChar(" "));
ilong = APIClass.mciSendString("pause media", TemStr, TemStr.Length, );
mc.state = State.mPuase;
}
private string GetCurrPath(string name)
{
if (name.Length < ) return "";
name = name.Trim();
name = name.Substring(, name.Length - );
return name;
}
//总时间
public int Duration
{
get
{
durLength = "";
durLength = durLength.PadLeft(, Convert.ToChar(" "));
APIClass.mciSendString("status media length", durLength, durLength.Length, );
durLength = durLength.Trim();
if (durLength == "") return ;
return (int)(Convert.ToDouble(durLength) / 1000f);
}
} //当前时间
public int CurrentPosition
{
get
{
durLength = "";
durLength = durLength.PadLeft(, Convert.ToChar(" "));
APIClass.mciSendString("status media position", durLength, durLength.Length, );
durLength = durLength.Trim();
if (durLength == "") return ;
mc.iPos = (int)(Convert.ToDouble(durLength) / 1000f);
return mc.iPos;
}
set
{
string Command = String.Format("Seek Media to {0}", value * );
APIClass.mciSendString(Command, null, , );//使播放停止
mc.state = State.mStop;
mc.iPos = value * ;
play();
}
} } public class APIClass
{ [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName(
string lpszLongPath,
string shortFile,
int cchBuffer
); [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
public static extern int mciSendString(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
); }

clsMCI

进度条控件

public class MyProgressBar : Control
{
public MyProgressBar()
{
///在构造函数中设置控件Style,ControlStyles枚举可以参考
///http://technet.microsoft.com/zh-cn/subscriptions/system.windows.forms.controlstyles.aspx
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true); //通过三个鼠标事件函数,让鼠标可以拖动控制条
this.MouseDown += MyProgressBar_MouseDown;
this.MouseMove += MyProgressBar_MouseMove;
this.MouseUp += MyProgressBar_MouseUp; this.BackColor = Color.Transparent;
} #region 定义变量,并对一些变量进行一些默认设置
//已加载长度(灰色矩形)
Rectangle foreRect;
Color foregroundColor = Color.Gray;
//总长度(底部白色矩形)
Rectangle backRect;
Color backgroundColor = Color.White;
//控制块(黑色矩形)
Rectangle setRect;
Color setRectColor = Color.Black; //最大值
int maximum = ;
//最小值
int minimum = ;
//当前值
double myValue = ;
//是否显示百分比
bool showPercent; //进度条百分比的颜色
Color fontColor = Color.Black;
//进度条百分比的字体大小
float fontSize = ;
//进度条百分比的字体
FontFamily myFontFamily = new FontFamily("宋体");
#endregion #region 设置属性值
[Category("进度条"), Description("显示百分比"), Browsable(true)]
public bool ShowPercentTag
{
get { return showPercent; }
set
{
showPercent = value;
Invalidate();
}
} [Category("进度条"), Description("进度条的最大值"), Browsable(true)]
public int Maximum
{
get { return maximum; }
set
{
maximum = value;
Invalidate();
}
} [Category("进度条"), Description("进度条的最小值"), Browsable(true)]
public int Minimum
{
get { return minimum; }
set
{
minimum = value;
Invalidate();
}
} [Category("进度条"), Description("进度条的背景色,总长度(底部白色矩形)"), Browsable(true)]
public Color BackgroundColor
{
get { return backgroundColor; }
set
{
this.backgroundColor = value;
Invalidate();
}
} [Category("进度条"), Description("进度条的前景色,已加载长度(灰色矩形)"), Browsable(true)]
public Color ForegroundColor
{
get { return foregroundColor; }
set
{
this.foregroundColor = value;
Invalidate();
}
} [Category("进度条"), Description("进度条控制块的颜色(黑色矩形)"), Browsable(true)]
public Color SetRectColor
{
get { return setRectColor; }
set
{
this.setRectColor = value;
Invalidate();
}
} [Category("进度条"), Description("进度条的百分比字体大小"), Browsable(true)]
public float FontSize
{
get { return fontSize; }
set
{
this.fontSize = value;
Invalidate();
}
} [Category("进度条"), Description("进度条的百分比字体"), Browsable(true)]
public FontFamily MyFontFamily
{
get { return myFontFamily; }
set
{
this.myFontFamily = value;
Invalidate();
}
} [Category("进度条"), Description("进度条的百分比字体颜色"), Browsable(true)]
public Color FontColor
{
get { return fontColor; }
set
{
this.fontColor = value;
Invalidate();
}
}
#endregion #region 确定控件的位置
[Category("General"), Description("控件的宽度"), Browsable(true)]
public new int Width
{
get { return base.Width; }
set
{
base.Width = value;
foreRect.X = backRect.X = base.Width / ;
backRect.Width = base.Width * / ;
foreRect.Width = (int)(myValue / maximum * backRect.Width);
setRect.X = (int)(foreRect.X + foreRect.Width - setRect.Width / ); Invalidate();
}
} [Category("General"), Description("控件的高度"), Browsable(true)]
public new int Height
{
get { return base.Height; }
set
{
base.Height = value;
setRect.Height = setRect.Width = base.Height * / ;
foreRect.Height = backRect.Height = base.Height / ;
setRect.Y = base.Height / ;
foreRect.Y = backRect.Y = base.Height / ;
Invalidate();
}
}
#endregion #region value值改变事件 protected EventHandler OnValueChanged;
//当外部为该事件添加了响应函数时,事件就会生效,否则为OnValueChanged的值为null
public event EventHandler ValueChanged
{
add
{
if (OnValueChanged != null)
{
foreach (Delegate d in OnValueChanged.GetInvocationList())
{
if (object.ReferenceEquals(d, value)) { return; }
}
}
OnValueChanged = (EventHandler)Delegate.Combine(OnValueChanged, value);
}
remove
{
OnValueChanged = (EventHandler)Delegate.Remove(OnValueChanged, value);
}
}
#endregion #region 定义Value属性 //当Value值改变的时候,重新设置矩形的进度,控制块的位置,并且重绘控件
//Value属性内如果对进度条的值进行修改,使用myValue变量,而在其他地方,则用Value属性
[Category("进度条"), Description("进度条的值"), Browsable(true)]
public double Value
{
get { return myValue; }
set
{
if (myValue < Minimum)
throw new ArgumentException("小于最小值");
if (myValue > Maximum)
throw new ArgumentException("超过最大值"); myValue = value;
foreRect.Width = (int)(myValue / maximum * backRect.Width); if ((myValue - maximum) > )
{
foreRect.Width = backRect.Width;
} setRect.X = (int)(foreRect.X + foreRect.Width - setRect.Width / ); //如果添加了响应函数,则执行该函数
if (OnValueChanged != null)
{
OnValueChanged(this, EventArgs.Empty);
} Invalidate();
}
}
#endregion #region 绘制控件
//重载OnPaint方法对控件进行绘制
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
DrawRect(e.Graphics);
if (ShowPercentTag)
{
DrawText(e.Graphics);
}
} //画进度条
private void DrawRect(Graphics e)
{
Pen pen = new Pen(this.foregroundColor); e.FillRectangle(new SolidBrush(this.backgroundColor), backRect);
e.DrawRectangle(new Pen(Color.Black), backRect); e.FillRectangle(new SolidBrush(this.foregroundColor), foreRect);
e.DrawRectangle(new Pen(Color.Black), foreRect); e.FillEllipse(new SolidBrush(this.setRectColor), setRect.X, setRect.Y, setRect.Width, setRect.Height);
e.DrawEllipse(new Pen(Color.Black), setRect.X, setRect.Y, setRect.Width, setRect.Height);
//e.FillRectangle(new SolidBrush(this.setRectColor), setRect);
//e.DrawRectangle(new Pen(Color.Black), setRect);
} //画进度值
private void DrawText(Graphics e)
{
Point point = new Point();
point.X = this.backRect.X + this.backRect.Width * / ;
point.Y = this.backRect.Y + this.backRect.Height / ; SolidBrush brush = new SolidBrush(fontColor);
Font font = new Font(myFontFamily, this.fontSize);
string percent = ((int)this.myValue).ToString() + "%"; StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center; e.DrawString(percent, font, brush, backRect, format);
}
#endregion #region 修改控件的大小
//拖动边缘的箭头改变控件的大小时,控件也要做相应的改变时,就可以重载该方法,如果没有重载,就只有在修改完成后才更新控件
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
this.Width = Width;
this.Height = Height;
Invalidate();
}
#endregion #region 拖动控制条
//添加三个辅助变量,添加响应函数
//当前鼠标位置
Point originPoint;
//当前控制块位置
Point originsetRectPoint;
//控制块是否点下
bool setRectDown = false; //放开鼠标
void MyProgressBar_MouseUp(object sender, MouseEventArgs e)
{
setRectDown = false;
} //移动鼠标
void MyProgressBar_MouseMove(object sender, MouseEventArgs e)
{
if (setRectDown)
{
//移动长度
int dd = e.Location.X - originPoint.X;
//百分比
double percent = (double)(originsetRectPoint.X + dd - this.backRect.X + setRect.Width / ) / this.backRect.Width;
if (percent < )
{
percent = ;
}
else if (percent > )
{ percent = ;
}
this.Value = percent * maximum;
this.foreRect.Width = (int)(percent * this.backRect.Width);
//this.setRect.X = originsetRectPoint.X + dd;
this.setRect.X = (int)(foreRect.X + foreRect.Width - setRect.Width / );
Invalidate();
}
} //点下鼠标
void MyProgressBar_MouseDown(object sender, MouseEventArgs e)
{
if (setRect.Contains(e.Location))
{
this.originPoint = e.Location;
originsetRectPoint = this.setRect.Location;
this.setRectDown = true;
}
}
#endregion
}

MyProgressBar

操作

 clsMCI mp = new clsMCI();
mp.FileName = path;//路径
mp.Duration//总时间
mp.CurrentPosition//当前时间

进度条控制时间

nowtime = mySlider1.Value / mySlider1.Maximum * alltime;
mp.CurrentPosition = (int)nowtime;

音乐播放进度

nowtime = Convert.ToDouble(mp.CurrentPosition);
mySlider1.Value = nowtime / alltime * mySlider1.Maximum;

实现界面

用mciSendString做音乐播放器的更多相关文章

  1. C#做音乐播放器时在自动下一曲中报异常的解决办法

    ---------------------- ASP.Net+Unity开发..Net培训.期待与您交流! ---------------------- 在利用Media Player做音乐播放器的时 ...

  2. 自定义css样式结合js控制audio做音乐播放器

    最近工作需求需要播放预览一些音乐资源,所以自己写了个控制audio的音乐播放器. 实现的原理主要是通过js调整audio的对象属性及对象方法来进行控制: 1.通过play().pause()来控制音乐 ...

  3. vue——一个页面实现音乐播放器

    请忽略下面这段文字年关将至,时间好歹又多出了些许.却不敢过度消遣.岁月未曾饶过我,我亦不想饶过岁月.且将它塞得膨胀,让这一年看似加更充实.不曾料想我一个爱些风花雪月.研墨行歌之人,却做起了碼农这一行当 ...

  4. 用PHP+H5+Boostrap做简单的音乐播放器(进阶版)

    前言:之前做了一个音乐播放器(纯前端),意外的受欢迎,然后有人建议我把后台一起做了,正好也想学习后台,所以学了两天php(不要吐槽我的速度,慢工出细活嘛~)然后在之前的基础上也又完善了一些功能,所以这 ...

  5. [ 原创 ]学习笔记-做一个Android音乐播放器是遇到的一些困难

    最近再做一个安卓的音乐播放器,是实验室里学长派的任务,我是在eclipse上进行开发的,由于没有android的基础,所以做起来困难重重. 首先是布局上的困难 1.layout里的控件属性不熟悉 2. ...

  6. 小菜学习Winform(二)WMPLib实现音乐播放器

    前言 现在网上有很多的音乐播放器,但好像都不是.net平台做的,在.net中实现音乐文件的播放功能很简单,下面就简单实现下. SoundPlayer类 在.net提供了音乐文件的类:SoundPlay ...

  7. 【大结局】《从案例中学习JavaScript》之酷炫音乐播放器(四)

    这是之前写的用H5制作的音乐播放器,前三节其实已经做得差不多了,音轨的制作原理已经在上一节说明,不过一直还没有和音乐对接. 本章作为该系列的一个完结篇,我会专门把动态音轨的实现代码贴出来,demo地址 ...

  8. Andriod小项目——在线音乐播放器

    转载自: http://blog.csdn.net/sunkes/article/details/51189189 Andriod小项目——在线音乐播放器 Android在线音乐播放器 从大一开始就已 ...

  9. Android开发6:Service的使用(简单音乐播放器的实现)

    前言 啦啦啦~各位好久不见啦~博主最近比较忙,而且最近一次实验也是刚刚结束~ 好了不废话了,直接进入我们这次的内容~ 在这篇博文里我们将学习Service(服务)的相关知识,学会使用 Service ...

随机推荐

  1. Thrift在Windows及Linux平台下的安装和使用示例

    本文章也同时发表在个人博客Thrift在Windows及Linux平台下的安装和使用示例上. thrift介绍 Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的R ...

  2. angular中$cacheFactory缓存的使用

    最近在学习使用angular,慢慢从jquery ui转型到用ng开发,发现了很多不同点,继续学习吧: 首先创建一个服务,以便在项目中的controller中引用,服务有几种存在形式,factory( ...

  3. JavaScript异常捕获

    理论准备 ★   异常捕获 △ 异常:当JavaScript引擎执行JavaScript代码时,发生了错误,导致程序停止运行: △ 异常抛出:当异常产生,并且这个异常生成一个错误信息: △ 异常捕获: ...

  4. MVC视图展现模式之移动布局

    参考:http://www.cnblogs.com/dunitian/p/5218140.html 简单点,直接上用法 新建MVC项目,在golbal.asax中添加如下代码 //添加一个自定义后缀 ...

  5. SQL Server中@@ROWCOUNT的用法

    SQL Server中@@ROWCOUNT返回受上一语句影响的行数,返回值类型为 int 整型. 如果行数大于 20 亿,则需要使用 ROWCOUNT_BIG. @@ROWCOUNT和@@ERROR变 ...

  6. C#语法糖

    首先需要声明的是“语法糖”这个词绝非贬义词,它可以给我带来方便,是一种便捷的写法,编译器会帮我们做转换:而且可以提高开发编码的效率,在性能上也不会带来损失.这让java开发人员羡慕不已,呵呵. 1.  ...

  7. python中的__init__ 、__new__、__call__小结

    这篇文章主要介绍了python中的__init__ .__new__.__call__小结,需要的朋友可以参考下 1.__new__(cls, *args, **kwargs)  创建对象时调用,返回 ...

  8. yii2.0 图片上传(摘录)

    文章来源:http://blog.sina.com.cn/s/blog_88a65c1b0101izmn.html 下面小伙就带领大学学习一下 Yii2.0 的图片上传类的使用,还是老样子,如果代码样 ...

  9. maven oracle jdbc jar

    1.problem describe: when your dependency jar about oracle use code like this: <!-- oracle-connect ...

  10. php学习笔记:对文件的增删查改等操作

    文件的创建: 采用touch()函数,当文件不存在会被创建 例如: <?php header("Content-type: text/html; charset=utf-8" ...