C#播放WAV声音文件

 

 

        //WAV声音播放
        //-------------------------------------
        [DllImport("winmm.dll")]
        public static extern int mciSendString(string m_strCmd, string m_strReceive, int m_v1, int m_v2);

        [DllImport("winmm.dll")]
        public static extern int mciSendString(string m_strCmd, StringBuilder m_strReceive, int m_v1, int m_v2);


        [DllImport("Kernel32", CharSet = CharSet.Auto)]
        static extern Int32 GetShortPathName(String path, StringBuilder shortPath, Int32 shortPathLength);

        //-------------------------------------
 

 

        //播放WAV声音
        public static void playMusic(string name, string Vol)
        {
            try
            {
            StringBuilder shortpath = new StringBuilder(100);
                int result = GetShortPathName(name, shortpath, shortpath.Capacity);
                name = shortpath.ToString();
                string buf = string.Empty;

                mciSendString("play " + name, buf, buf.Length, 0); //播放
                mciSendString("SETAUDIO " + name + " VOLUME TO " + Vol, buf, 0, 0); //音量1-1000
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show("playMusic:" + ex.Message);
                return;
            }
        }

        //检测声音的长度
        public static void LoadMusicTime(string name)
        {
            try
            {
                StringBuilder shortpath = new StringBuilder(100);
                int result = GetShortPathName(name, shortpath, shortpath.Capacity);
                name = shortpath.ToString();
                StringBuilder buf = new StringBuilder(80);


                mciSendString("close all", buf, buf.Capacity, 0);
                mciSendString("open " + name + " alias media", buf, buf.Capacity, 0);
                mciSendString("status media length", buf, buf.Capacity, 0);
                TimeSpan ts = new TimeSpan(0, 0, 0, 0, (int)Convert.ToDouble(buf.ToString().Trim()));
                MessageBox.Show( ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds + "." + ts.Milliseconds);
                mciSendString("close all", buf, buf.Capacity, 0);
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show("LoadMusicTime:" + ex.Message);
                return;
            }
        }

        //停止播放
        public static void StopMusic()
        {
            try
            {
                StringBuilder buf = new StringBuilder(80);
                mciSendString("close all", buf, buf.Capacity, 0);
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show("StopMusic:" + ex.Message);
                return;
            }
        }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值