音乐操作类

 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. Scrum 3.1 多鱼点餐系统开发进度(第三阶段项目构思与任务规划)

    Scrum 3.1 多鱼点餐系统开发进度(第三阶段项目构思与任务规划) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅到 ...

  2. Logger.getLogger和LogFactory.getLog的区别

    Logger来自log4j自己的包.如果用Logger.getLogger,需要一个log4j的jar包,用此方式你只能依靠log4j: LogFactory来自common-logging包.如果用 ...

  3. ok6410 android driver(3)

    This article discusses the Makefile and how to port the module to different platform (localhost and ...

  4. 三分 --- POJ 3301 Texas Trip

    Texas Trip Problem's Link:   http://poj.org/problem?id=3301 Mean: 给定n(n <= 30)个点,求出包含这些点的面积最小的正方形 ...

  5. WCF小结

    WCF总结 1.WCF(原代号为Indigo)是一个用于创建和运行分布式系统的技术集合,使用它能创建安全的.可靠的.跨平台的的分布式解决方案.它的面向服务的编程模型,整合了.Net平台下以往全部的分布 ...

  6. 怎样用C#代码屏蔽任务管理器?

    这是我在网上找的并多加了一些我自己需要的代码,经过我的测试,可以屏蔽任务管理器,但还有一些瑕疵. 首先,我在vs2012中新建一个项目,选择window下的window窗体应用程序,把窗体form1拉 ...

  7. HTML5 Audio and JavaScript Control

    IE8 以下无效 <!DOCTYPE html> <html> <head> <meta content="text/html; charset=u ...

  8. [TypeScript] JSON对象转TypeScript对象范例

    [TypeScript] JSON对象转TypeScript对象范例 Playground http://tinyurl.com/nv4x9ak Samples class DataTable { p ...

  9. Postman的使用

    在我们平时开发中,特别是需要与接口打交道时,无论是写接口还是用接口,拿到接口后肯定都得提前测试一下,这样的话就非常需要有一个比较给力的Http请求模拟工具,现在流行的这种工具也挺多的,像火狐浏览器插件 ...

  10. 自己写方法处理WP(RT)后退键事件处理

    不用微软的NavigationHelper,自己写方法处理WP后退键事件 在WP8.1(RT)程序中,你会发现按下后退键时,应用会直接退出,变为后台运行,这是因为RT与Silverlight对后退键的 ...