110,545
社区成员
发帖
与我相关
我的任务
分享
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;
}
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;
}
[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);
}