using System; using System.ComponentModel; using System.IO; using System.Media; using System.Security; using System.Security.Permissions; namespace Znyc.Admin.Commons.Device { /// /// WAV声音格式文件播放辅助类 /// [HostProtection(SecurityAction.LinkDemand, Resources = HostProtectionResource.ExternalProcessMgmt)] [Browsable(false)] [EditorBrowsable(EditorBrowsableState.Advanced)] public class AudioHelper { /// /// 播放 .wav 格式的声音文件 /// /// 声音文件路径 public static void Play(string location) { Play(location, AudioPlayMode.Background); } /// /// 播放 .wav 格式的声音文件 /// /// 播放声音的枚举模式。默认为AudioPlayMode.Background。 /// 声音文件路径 public static void Play(string location, AudioPlayMode playMode) { ValidateAudioPlayModeEnum(playMode, "playMode"); string fileName = ValidateFilename(location); SoundPlayer player1 = new SoundPlayer(fileName); Play(player1, playMode); } /// /// 播放 .wav 格式的声音文件 /// /// 声音文件的流对象 /// 播放声音的枚举模式。默认为AudioPlayMode.Background。 public static void Play(Stream stream, AudioPlayMode playMode) { ValidateAudioPlayModeEnum(playMode, "playMode"); if (stream == null) { throw new ArgumentNullException("stream"); } Play(new SoundPlayer(stream), playMode); } /// /// 播放 .wav 格式的声音文件 /// /// 声音文件的字节数组 /// 播放声音的枚举模式。默认为AudioPlayMode.Background。 public static void Play(byte[] data, AudioPlayMode playMode) { if (data == null) { throw new ArgumentNullException("data"); } ValidateAudioPlayModeEnum(playMode, "playMode"); MemoryStream stream1 = new MemoryStream(data); Play(stream1, playMode); stream1.Close(); } /// /// 播放系统声音 /// public static void PlaySystemSound(SystemSound systemSound) { if (systemSound == null) { throw new ArgumentNullException("systemSound"); } systemSound.Play(); } /// /// 停止正在后台播放的声音 /// public static void Stop() { SoundPlayer player1 = new SoundPlayer(); InternalStop(player1); } #region 辅助方法 private static SoundPlayer _SoundPlayer; private static void InternalStop(SoundPlayer sound) { new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert(); try { sound.Stop(); } finally { CodeAccessPermission.RevertAssert(); } } /// /// 播放声音函数 /// /// 声音文件 /// 播放模式 private static void Play(SoundPlayer sound, AudioPlayMode mode) { if (_SoundPlayer != null) { InternalStop(_SoundPlayer); } _SoundPlayer = sound; switch (mode) { case AudioPlayMode.WaitToComplete: _SoundPlayer.PlaySync(); return; case AudioPlayMode.Background: _SoundPlayer.Play(); return; case AudioPlayMode.BackgroundLoop: _SoundPlayer.PlayLooping(); return; } } private static void ValidateAudioPlayModeEnum(AudioPlayMode value, string paramName) { if (value < AudioPlayMode.WaitToComplete || value > AudioPlayMode.BackgroundLoop) { throw new InvalidEnumArgumentException(paramName, (int)value, typeof(AudioPlayMode)); } } private static string ValidateFilename(string location) { if (string.IsNullOrEmpty(location)) { throw new ArgumentNullException("location"); } return location; } #endregion 辅助方法 } /// /// 声音播放的方式 /// public enum AudioPlayMode { /// /// 播放声音,并等待,直到它完成 /// WaitToComplete, /// /// 在后台播放声音。调用代码继续执行。 /// Background, /// /// 在后台播放声音,直到调用stop方法。调用代码继续执行。 /// BackgroundLoop } }