C#播放声音

最近在写winform程序,其中有用到播放背景音乐,经过调研,使用System.Media.SoundPlayer实现了播放功能。

class SoundHelper
    {
        [DllImport("winmm.dll")]
        public static extern bool PlaySound(String Filename, int Mod, int Flags);
        //
        private static string invalidSoundPath = @"music\invalid.wav";
        private static string verifyOkSoundPath = @"music\ok.wav";
        private static string verifyFailSoundPath = @"music\fail.wav";
        private static string serviceFailSoundPath = @"music\serviceFail.wav";

        //
        public enum SoundType
        {
            INVALID, VERIFY_OK, VERIFY_FAIL, SERVICE_FAIL
        }
        public static void playSound(SoundType type)
        {
            switch (type)
            {
                case SoundType.INVALID:
                    player(invalidSoundPath);
                    break;
                case SoundType.VERIFY_FAIL:
                    player(verifyFailSoundPath);
                    break;
                case SoundType.VERIFY_OK:
                    player(verifyOkSoundPath);
                    break;
                case SoundType.SERVICE_FAIL:
                    player(serviceFailSoundPath);
                    break;
                default:
                    break;
            }

        }
        private static void player(string path)
        {
            SoundPlayer sPlayer = new SoundPlayer();
            sPlayer.SoundLocation = path;
            sPlayer.Play();            
        }
        
    }


你可能感兴趣的:(C#.NET)