C#循环播放音乐

实现单曲循环
不使用控件

第1个回答  推荐于2016-12-01
2种方法:

[DllImport("winmm.dll")]
private static extern bool PlaySound(string pszSound, int hmod, int fdwSound);
public const int SND_FILENAME = 0x00020000;
public const int SND_ASYNC = 0x0001;
public const int SND_LOOP = 8;

/// <summary>
/// 调用API播放声音
/// </summary>
/// <param name="name">声音文件名</param>
public static void PlayMusic(string name)
{
try
{
PlaySound(name, 0, SND_ASYNC | SND_FILENAME | SND_LOOP);
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}

方法2:

[DllImport("winmm.dll")]
public extern static int mciSendString(string command,IntPtr responseBuffer, int bufferLength, int nothing);

WinAPI.mciSendString("open D:\\GM01.mid alias song ", new IntPtr(0), 0, 0);
WinAPI.mciSendString("play song", new IntPtr(0), 0, 0);本回答被提问者采纳