请教WINFORM里的音乐连续播放问题! 我用WINFORM写了个小程序,界面一打开就播放音乐,我的音乐地址是从数据库里读出来的,因为有多条路径,所以每次打开调用一次播放的方法,但是考虑到页面一直开着音乐会有播放结束的问题,所以想让音乐在播放结束后再自动调用音乐播放的方法,但是不知道怎么判断结束,及怎样重新调用!希望能给个思路及SIMPLE,在此多谢了啊~~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果你要读取数据库中的文件,那就在文件读完的时候重新调用这个方法。加一个while循环试试。或者将音乐文件路径储存在一个arraylist中,然后循环读取,当读到arraylist的尾的时候回到list头,继续循环读取就ok了。不知回答是否满意 我播放音乐是独立的一个方法,然后我读取数据库中的路径也是一个方法,确实我存储路径是使用的ARRAYLIST存储,所以我随机抽出一个INT值,然后给ARRAYLIST的下标这样可以实现每次调用方法取得的音乐路径是不一样的效果。但是我的音乐播放代码只是直接给了文件名之后就能播放,实在不知道该怎样控制它的状态,所以我无法实现播放完毕后重新加载一次音乐播放的方法~~~求高手指教,最好能有完整代码进行学习~~ 给出你播放音乐的代码或者使用Windows Media Player类播放 一般音乐播放有一个播放状态,如果没有那么肯定会有一个事件指示已经播放完,那么用一个timer控件去检测播放状态,如果为停止,那么调用方法播放下一首.不知道楼主是用什么方法播放音乐的. 第一 你用的什么播放winapi?dx? mediaplayer?每种检测方法不一样 我是使用的API,因为我是新手不太懂实现的过程~~但是根据代码意思大致的查看它也只有实现简单播放的功能,没有控制状态的方法,贴出播放部分代码:name=Application.StartupPath+"\\music\\"+name;StringBuilder shortpath=new StringBuilder(80);int result=GetShortPathName(name,shortpath,shortpath.Capacity);name=shortpath.ToString();mciSendString(@"close all",null,0,0);mciSendString(@"open "+name+" alias song",null,0,0);mciSendString("play song",null,0,0);只是调用这个方法就可以播放,但是找不到控制状态的~~~而且NAME是我调用方法时传过来的音乐路径~~~请各位大家指点~~ MCIERROR mciSendString( LPCTSTR lpszCommand, LPTSTR lpszReturnString, UINT cchReturn, HANDLE hwndCallback );你看看第四个参数,回调函数啊,这不正是你所需要的吗 [DllImport("kernel32.dll", CharSet = CharSet.Auto)] private static extern int GetShortPathName(string lpszLongPath, StringBuilder shortFile, int cchBuffer); [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)] private static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback); private const int MM_MCINOTIFY = 0x3B9; private void button1_Click(object sender, EventArgs e) { string name = "c:\\123.MP3"; StringBuilder shortpath = new StringBuilder(80); int result = GetShortPathName(name, shortpath, shortpath.Capacity); mciSendString(@"close all", null, 0, 0); mciSendString(@"open " + name + " alias song", null, 0, 0); mciSendString("play song notify", null, 0, this.Handle.ToInt32()); } protected override void WndProc(ref System.Windows.Forms.Message m) { if (m.Msg == MM_MCINOTIFY) { // 还要判断具体消息,没查到 MessageBox.Show("OK"); } base.WndProc(ref m); } private void button2_Click(object sender, EventArgs e) { mciSendString(@"close all", null, 0, 0); } 能否解释下protected override void WndProc(ref System.Windows.Forms.Message m) { if (m.Msg == MM_MCINOTIFY) { // 还要判断具体消息,没查到 MessageBox.Show("OK"); } base.WndProc(ref m); }这段的具体意义,还有这个方法是自动触发的吗? 原来还要使用mciSendCommand函数发送消息,困难你直接使用下面的方法吧 protected override void WndProc(ref System.Windows.Forms.Message m) { if (m.Msg == MM_MCINOTIFY) { // 播放完一首音乐后会触发MM_MCINOTIFY消息,你在这里接着播放下一首就行了 MessageBox.Show("播放完毕"); } base.WndProc(ref m); } WndProc这个方法该在什么地方调用呢?? 不行的,提示该方法ref System.Windows.Forms.Message m未找到适合的方法。 呵呵,用mcisendstring的话,就自己设置一个属性playstate. public enum Playstate : byte { Stopped = 1, Playing = 2, Pause = 3 } /// <summary> /// 获得当前媒体的状态是不是在播放 /// </summary> /// <returns></returns> public Playstate IsPlaying() { Playstate isPlaying=Playstate .Stopped ; durLength = ""; durLength = durLength.PadLeft(128, Convert.ToChar(" ")); int RefInt= mciSendString("status NOWMUSIC mode", durLength, durLength.Length, 0); durLength = durLength.Trim(); if (durLength.Substring(0, 7) == "playing" || durLength.Substring(0, 2) == "播放") isPlaying = Playstate.Playing; else if (durLength.Substring(0, 7) == "stopped" || durLength.Substring(0, 2) == "停止") isPlaying = Playstate.Stopped; else isPlaying = Playstate.Pause; return isPlaying; } 然后在播放界面放一个timer控件,隔500ms检测一次playstate 请问durLength这是在哪声明的,是做什么用的? [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] private string durLength = ""; 用于mcisendstring返回值. NOWMUSIC 是我为打开的音乐取的一个别名.这样吧,我用mcisendstring写一个循环播放音乐的代码,你有油箱吗? private void timer1_Tick(object sender, EventArgs e) { if (playList.Count == 0)//playlist为我用来保存音乐路径的arraylist return; if (cmci.IsPlaying() == Media.Playstate.Stopped) { playIndex++;//playIndex为现在播放的是第几首音乐 if (playIndex >= playList.Count) playIndex = 0; cmci.OpenMusic((string)playList[playIndex], this.Handle); cmci.PlayMusic(); } } //IsPlaying做了修改. public Playstate IsPlaying() { Playstate isPlaying = Playstate.Stopped; try { durLength = ""; durLength = durLength.PadLeft(128, Convert.ToChar(" ")); int RefInt = mciSendString("status NOWMUSIC mode", durLength, durLength.Length, 0); durLength = durLength.Trim(); if (durLength.Substring(0, 7) == "playing" || durLength.Substring(0, 2) == "播放") isPlaying = Playstate.Playing; else if (durLength.Substring(0, 7) == "stopped" || durLength.Substring(0, 2) == "停止") isPlaying = Playstate.Stopped; else isPlaying = Playstate.Pause; } catch { } return isPlaying; } 道理都一样拉,一首音乐播放完肯定得重新打开下一首啊,那timer中检测 到stopped,播放下一首不就行了!举一反三. 现在的问题是durLength这个提示越界了???实在不知道什么原因? LIST的用法 如何求出字符串中汉字和全角字符的个数,看例子: 怎样调用远程计算机的内存使用情况! 关于DateTimePicker的两个时间相减的问题。。。 高手指条路 请问我已经有一个.reg文件,我想执行这个文件,可以用process总是弹出一个询问对话框,问怎样能不出现对话框呢? 谁来帮我看看这代码哪个地方有误? 非常辣手的问题,有关底层的程序。。。。。。。。。。。:( 终于涨工资了,散分了 C#中的模块到底是怎么一回事? VS中 c#添加什么语句可以使dos窗口停留? 如何构建邮件服务器
或者
使用Windows Media Player类播放
winapi?dx? mediaplayer?
每种检测方法不一样
name=Application.StartupPath+"\\music\\"+name;
StringBuilder shortpath=new StringBuilder(80);
int result=GetShortPathName(name,shortpath,shortpath.Capacity);
name=shortpath.ToString();
mciSendString(@"close all",null,0,0);
mciSendString(@"open "+name+" alias song",null,0,0);
mciSendString("play song",null,0,0);只是调用这个方法就可以播放,但是找不到控制状态的~~~而且NAME是我调用方法时传过来的音乐路径~~~
请各位大家指点~~
LPCTSTR lpszCommand,
LPTSTR lpszReturnString,
UINT cchReturn,
HANDLE hwndCallback
);
你看看第四个参数,回调函数啊,这不正是你所需要的吗
private static extern int GetShortPathName(string lpszLongPath, StringBuilder shortFile, int cchBuffer); [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
private static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback); private const int MM_MCINOTIFY = 0x3B9; private void button1_Click(object sender, EventArgs e)
{
string name = "c:\\123.MP3";
StringBuilder shortpath = new StringBuilder(80);
int result = GetShortPathName(name, shortpath, shortpath.Capacity); mciSendString(@"close all", null, 0, 0);
mciSendString(@"open " + name + " alias song", null, 0, 0);
mciSendString("play song notify", null, 0, this.Handle.ToInt32());
} protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == MM_MCINOTIFY)
{
// 还要判断具体消息,没查到
MessageBox.Show("OK");
}
base.WndProc(ref m);
} private void button2_Click(object sender, EventArgs e)
{
mciSendString(@"close all", null, 0, 0);
}
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == MM_MCINOTIFY)
{
// 还要判断具体消息,没查到
MessageBox.Show("OK");
}
base.WndProc(ref m);
}
这段的具体意义,还有这个方法是自动触发的吗?
你直接使用下面的方法吧
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == MM_MCINOTIFY)
{
// 播放完一首音乐后会触发MM_MCINOTIFY消息,你在这里接着播放下一首就行了
MessageBox.Show("播放完毕");
}
base.WndProc(ref m);
}
ref System.Windows.Forms.Message m
未找到适合的方法。
public enum Playstate : byte
{
Stopped = 1,
Playing = 2,
Pause = 3
}
/// <summary>
/// 获得当前媒体的状态是不是在播放
/// </summary>
/// <returns></returns>
public Playstate IsPlaying()
{
Playstate isPlaying=Playstate .Stopped ;
durLength = "";
durLength = durLength.PadLeft(128, Convert.ToChar(" "));
int RefInt= mciSendString("status NOWMUSIC mode", durLength, durLength.Length, 0);
durLength = durLength.Trim();
if (durLength.Substring(0, 7) == "playing" || durLength.Substring(0, 2) == "播放")
isPlaying = Playstate.Playing;
else if (durLength.Substring(0, 7) == "stopped" || durLength.Substring(0, 2) == "停止")
isPlaying = Playstate.Stopped;
else isPlaying = Playstate.Pause;
return isPlaying;
}
private string durLength = ""; 用于mcisendstring返回值.
private void timer1_Tick(object sender, EventArgs e)
{
if (playList.Count == 0)//playlist为我用来保存音乐路径的arraylist
return;
if (cmci.IsPlaying() == Media.Playstate.Stopped)
{
playIndex++;//playIndex为现在播放的是第几首音乐
if (playIndex >= playList.Count)
playIndex = 0; cmci.OpenMusic((string)playList[playIndex], this.Handle);
cmci.PlayMusic();
}
} //IsPlaying做了修改.
public Playstate IsPlaying()
{ Playstate isPlaying = Playstate.Stopped; try
{ durLength = ""; durLength = durLength.PadLeft(128, Convert.ToChar(" ")); int RefInt = mciSendString("status NOWMUSIC mode", durLength, durLength.Length, 0); durLength = durLength.Trim(); if (durLength.Substring(0, 7) == "playing" || durLength.Substring(0, 2) == "播放") isPlaying = Playstate.Playing; else if (durLength.Substring(0, 7) == "stopped" || durLength.Substring(0, 2) == "停止") isPlaying = Playstate.Stopped; else isPlaying = Playstate.Pause;
}
catch
{
} return isPlaying; }
实在不知道什么原因?