C#播放声音

时间:2021-12-29 09:23:23
   [DllImport("winmm.dll")]
public static extern long PlaySound(String fileName, long a, long b); [DllImport("winmm.dll")]
public static extern long mciSendString(string lpstrCommand, string lpstrReturnString, long length, long hwndcallback); /// <summary>
/// 播放音乐文件(重复)
/// </summary>
/// <param name="p_FileName">音乐文件名称</param>
public static void PlayMusic_Repeat(string p_FileName)
{
try
{
mciSendString(@"close temp_music", " ", , );
mciSendString(@"open " + p_FileName + " alias temp_music", " ", , );
mciSendString(@"play temp_music repeat", " ", , );
}
catch
{ }
} /// <summary>
/// 播放音乐文件
/// </summary>
/// <param name="p_FileName">音乐文件名称</param>
public static void PlayMusic(string p_FileName)
{
try
{
mciSendString(@"close temp_music", " ", , );
mciSendString(@"open " + p_FileName + " alias temp_music", " ", , );
mciSendString(@"play temp_music", " ", , );
}
catch
{ }
} /// <summary>
/// 停止当前音乐播放
/// </summary>
/// <param name="p_FileName">音乐文件名称</param>
public static void StopMusic(string p_FileName)
{
try
{
mciSendString(@"close " + p_FileName, " ", , );
}
catch { }
} private void button18_Click(object sender, EventArgs e)
{
//getMessageMethod();
PlayMusic_Repeat("你有新的消息");
}