[C#]使用mciSendString播放背景音乐并控制音量(wav等)

 C# 播放音乐有很多方法,比如API的PlaySound,微软的TTS(Text To Speech)也可以,但是这两个没法控制音量(TTS在读Text的时候可以控制音量大小,但是在播放wav等文件时控制音量就无效),还有DirectSound可以调节音量(需要引用Microsoft.DirectX.dll和Microsoft.DirectX.DirectSound.dll,有需要的朋友可点这里>>>下载),但是用这个在64位机器下会报错,虽然可以改成x86编译后使用,但有时项目必须要用64位编译,没辙!只能另辟蹊径

 后面还是回归到API,当然不是PlaySound,而是mciSendString,搜一下mciSendString用法有很多,我这里就只记录基本的几个方法(我所需要的)

    public class MCIMediaHelper
    {
        [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
        private static extern int mciSendString(
            string lpstrCommand,
            string lpstrReturnString,
            int uReturnLength,
            int hwndCallback);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        private static extern int GetShortPathName(
            string lpszLongPath,
            string shortFile,
            int cchBuffer);
        /// 
        /// 音量大小
        /// 
        public static int Volume;
        /// 
        /// 播放状态
        /// 
        public enum Playstate : byte
        {
            Stopped = 1,
            Playing = 2,
            Pause = 3
        }
        ///    
        /// 打开MCI设备
        ///    
        /// 要打开的文件名   
        /// 传值代表成功与否   
        private static bool OpenMusic(string FileName)
        {
            CloseMusic();

            string ShortPathName = "";
            ShortPathName = ShortPathName.PadLeft(260, Convert.ToChar(" "));
            int RefInt = GetShortPathName(FileName, ShortPathName, ShortPathName.Length);
            ShortPathName = GetCurrPath(ShortPathName);

            //不知道播放wav文件用waveaudio为什么不能单曲循环播放?
            string MciCommand = string.Format("open {0} type MPEGVideo alias NOWMUSIC ", ShortPathName);//"open " & RefShortName & " type " & DriverID & " alias NOWMUSIC"   

            RefInt = mciSendString(MciCommand, null, 0, 0);

            return RefInt == 0;
        }
        ///    
        /// 循环播放音乐   
        ///    
        ///    
        public static bool PlayMusic(string musicPath)
        {
            if (OpenMusic(musicPath))
            {
                int RefInt = mciSendString("play NOWMUSIC repeat", null, 0, 0);
                if (RefInt == 0)
                {
                    SetVolume();
                }
                return RefInt == 0;
            }
            return false;
        }
        ///    
        /// 暂停播放
        ///    
        ///    
        public static bool PauseMusic()
        {
            int RefInt = mciSendString("pause NOWMUSIC", null, 0, 0);
            return RefInt == 0;
        }
        ///    
        /// 继续播放
        ///    
        ///    
        public static bool ResumeMusic()
        {
            int RefInt = mciSendString("resume NOWMUSIC", null, 0, 0);
            return RefInt == 0;
        }
        ///    
        /// 停止播放
        ///    
        ///    
        public static bool StopMusic()
        {
            int RefInt = mciSendString("stop NOWMUSIC", null, 0, 0);
            return RefInt == 0;
        }
        ///    
        /// 设置声音大小(1-1000)
        ///   
        ///    
        private static bool SetVolume()
        {
            string MciCommand = string.Format("setaudio NOWMUSIC volume to {0}", Volume);
            int RefInt = mciSendString(MciCommand, null, 0, 0);
            return RefInt == 0;
        }
        ///    
        /// 设置播放速度   
        ///    
        ///    
        ///    
        private static bool SetSpeed(int Speed)
        {
            string MciCommand = string.Format("set NOWMUSIC speed to {0}", Speed);
            int RefInt = mciSendString(MciCommand, null, 0, 0);
            return RefInt == 0;
        }
        ///    
        /// 关闭媒体   
        ///    
        ///    
        public static bool CloseMusic()
        {
            int RefInt = mciSendString("close NOWMUSIC", null, 0, 0);
            return RefInt == 0;
        }
        /// 
        /// 获取播放状态
        /// 
        /// 
        public static Playstate IsPlaying()
        {
            Playstate isPlaying = Playstate.Stopped;

            string durLength = "";
            durLength = durLength.PadLeft(128, Convert.ToChar(" "));
            int RefInt = mciSendString("status NOWMUSIC mode", durLength, durLength.Length, 0);
            durLength = durLength.Trim();
            if (durLength.Substring(0, 7) == "playing" || durLength.Substring(0, 2) == "播放")
            {
                isPlaying = Playstate.Playing;
            }
            else if (durLength.Substring(0, 7) == "stopped" || durLength.Substring(0, 2) == "停止")
            {
                isPlaying = Playstate.Stopped;
            }
            else
            {
                isPlaying = Playstate.Pause;
            }
            return isPlaying;
        }

        private static string GetCurrPath(string name)
        {
            if (name.Length < 1)
            {
                return "";
            }
            name = name.Trim();
            name = name.Substring(0, name.Length - 1);
            return name;
        }


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