C#循环播放音乐

来源:百度知道 编辑:UC知道 时间:2024/09/20 02:56:20
实现单曲循环
不使用控件

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,