业务咨询
业务咨询
业务咨询
售后服务

解决方案 当前位置: 首页 >> 解决方案

C# 声音播放类
发布者:巨石软件工作室  发布时间:2014-04-11  阅读次数:5454 次

  /// <summary>
/// 声音播放辅助类
/// </summary>
/// <remarks>也可以参考System.Media.SoundPlayer来实现声音文件的播放</remarks>
public class MySoundPlayer
{
Thread thread = null;
[Flags]
public enum PlaySoundFlags : int
{
/// <summary>
/// play synchronously 同步
/// </summary>
SND_SYNC = 0x0000,
/// <summary>
/// play asynchronously 异步
/// </summary>
SND_ASYNC = 0x0001,
/// <summary>
/// silence (!default) if sound not found
/// </summary>
SND_NODEFAULT = 0x0002,
/// <summary>
/// pszSound points to a memory file
/// </summary>
SND_MEMORY = 0x0004,
/// <summary>
/// loop the sound until next sndPlaySound
/// </summary>
SND_LOOP = 0x0008,
/// <summary>
/// don't stop any currently playing sound
/// </summary>
SND_NOSTOP = 0x0010,
/// <summary>
/// don't wait if the driver is busy
/// </summary>
SND_NOWAIT = 0x00002000,
/// <summary>
/// name is a registry alias
/// </summary>
SND_ALIAS = 0x00010000,
/// <summary>
/// alias is a predefined ID
/// </summary>
SND_ALIAS_ID = 0x00110000,
/// <summary>
/// name is file name
/// </summary>
SND_FILENAME = 0x00020000,
/// <summary>
/// name is resource name or atom
/// </summary>
SND_RESOURCE = 0x00040004,
}
/// <summary>
/// plays a sound specified by a file name, resource, or system event.
/// </summary>
/// <param name="szSound">
/// Pointer to a null-terminated string that specifies the sound to play. If this parameter is NULL,
/// any currently playing waveform sound is stopped. To stop a non-waveform sound, specify SND_PURGE
/// in the fdwSound parameter.
/// Three flags in fdwSound (SND_ALIAS, SND_FILENAME, and SND_RESOURCE) determine whether the name is
/// interpreted as an alias for a system event, a file name, or a resource identifier. If none of these
/// flags are specified, PlaySound searches the registry or the WIN.INI file for an association with the
/// specified sound name. If an association is found, the sound event is played. If no association is found
/// in the registry, the name is interpreted as a file name.
/// </param>
/// <param name="hMod">Handle to the executable file that contains the resource to be loaded. This parameter
/// must be NULL unless SND_RESOURCE is specified in fdwSound. </param>
/// <param name="flags">Flags for playing the sound. The following table shows the possible values. </param>
/// <returns>TRUE indicates success. FALSE indicates failure.</returns>
[DllImport("winmm")]
private static extern bool PlaySound(string szSound, IntPtr hMod, PlaySoundFlags flags);
/// <summary>
/// plays a sound specified by a file name, resource, or system event.
/// </summary>
/// <param name="szSound">
/// Pointer to a null-terminated string that specifies the sound to play. If this parameter is NULL,
/// any currently playing waveform sound is stopped. To stop a non-waveform sound, specify SND_PURGE
/// in the fdwSound parameter.
/// Three flags in fdwSound (SND_ALIAS, SND_FILENAME, and SND_RESOURCE) determine whether the name is
/// interpreted as an alias for a system event, a file name, or a resource identifier. If none of these
/// flags are specified, PlaySound searches the registry or the WIN.INI file for an association with the
/// specified sound name. If an association is found, the sound event is played. If no association is found
/// in the registry, the name is interpreted as a file name.
/// </param>
/// <param name="flags">Flags for playing the sound. The following table shows the possible values. </param>
/// <returns>TRUE indicates success. FALSE indicates failure.</returns>

public static void PlaySound(string filename)
{

PlaySound(filename, IntPtr.Zero, PlaySoundFlags.SND_ASYNC | PlaySoundFlags.SND_FILENAME);
}
}

版权所有:成都巨石软件工作室-成都私人软件开发,专注软件、网站定制开发 蜀ICP备13008633号-1
地址:成都市武侯区五大花园中央花园二期 手机:13308099279 QQ:273038992