業務咨詢
業務咨詢
業務咨詢
售後服務

解決方案 當前位置: 首頁 >> 解決方案

C# 聲音(yīn)播放類
發布者:巨石軟件工作室  發布時(shí)間:2014-04-11  閱讀次數:4863 次

  /// <summary>
/// 聲音(yīn)播放輔助類
/// </summary>
/// <remarks>也(yě)可(kě)以參考System.Media.SoundPlayer來(lái)實現聲音(yīn)文件的(de)播放</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);
}
}

版權所有:武侯區創雲軟件開發工作室-成都私人(rén)軟件開發,專注軟件、網站定制開發 京ICP證000000号
地址:成都市武侯區(qū)五大(dà)花園中央花園二期 手機:13308099279 QQ:273038992