DirectX编程:[初级]C#中利用DirectSound播放WAV格式声音[最少只要4句话]

网上已经有很多朋友介绍过如何在C#中利用DirectSound来播放声音。今天自己试了下,发现真得很简单,对于初学者来说最简单不过了。只需要短短几句代码。其中关键的只要4句左右代码就OK了。

      效果图如下 :

      DirectX编程:[初级]C#中利用DirectSound播放WAV格式声音[最少只要4句话]

      平台:VS.NET 2005 ,DirectX SDK(June 2008)

      需要引用的外部DLL:Microsoft.DirectX.dll 和 Microsoft.DirectX.DirectSound.dll。

      需要引用的命名空间:using Microsoft.DirectX.DirectSound。

      要实现播放效果的大致步骤:1 建立播放设备对象;2 建立缓冲区对象;3 设置缓冲区协作级别;4.播放缓冲区。

      因为比较简单,所以大家直接看代码吧。其中"Play"按钮主要的就四句话,实现播放效果,但它的缓冲区信息是默认的。"GlobalPlay"按钮通过设置缓冲区信息来对缓冲区做调整,让播放可以在失去焦点的时候继续播放。除了播放功能外,还可以控制音量和声道。

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;



//引用的命名空间

using Microsoft.DirectX.DirectSound;



namespace MyVoice

{

    public partial class Form4 : Form

    {

        public Form4()

        {

            InitializeComponent();

        }



        private SecondaryBuffer secBuffer;//缓冲区对象

        private Device secDev;//设备对象



        private void button1_Click(object sender, EventArgs e)

        {

            openFileDialog1.Filter = "(*.*)|*.*|(.wav)|*.wav";

            DialogResult dlgResult = openFileDialog1.ShowDialog();

            if (dlgResult == DialogResult.OK)

            {

                textBox1.Text = openFileDialog1.FileName;

            }



        }



        private void button2_Click(object sender, EventArgs e)

        {

            if (textBox1.Text.Length > 0)

            {

                secDev = new Device();

                secDev.SetCooperativeLevel(this, CooperativeLevel.Normal);//设置设备协作级别

                secBuffer = new SecondaryBuffer(textBox1.Text, secDev);//创建辅助缓冲区

                secBuffer.Play(0, BufferPlayFlags.Looping);//设置缓冲区为循环播放

            }

        }



        private void button3_Click(object sender, EventArgs e)

        {

            if (textBox1.Text.Length>0)

            {

                secDev = new Device();

                secDev.SetCooperativeLevel(this, CooperativeLevel.Normal);//设置设备协作级别

                BufferDescription buffDes = new BufferDescription();

                buffDes.GlobalFocus = true;//设置缓冲区全局获取焦点

                buffDes.ControlVolume = true;//指明缓冲区可以控制声音

                buffDes.ControlPan = true;//指明缓冲区可以控制声道平衡

                secBuffer = new SecondaryBuffer(textBox1.Text, buffDes, secDev);//创建辅助缓冲区

                secBuffer.Play(0, BufferPlayFlags.Looping);//设置缓冲区为循环播放

            }

        }



        private void button4_Click(object sender, EventArgs e)

        {

            if (secBuffer != null)

            {

                secBuffer.Stop();

            }

        }



        private void trackBar1_Scroll(object sender, EventArgs e)

        {

            if (secBuffer != null)

            {

                secBuffer.Volume = -trackBar1.Value * 400;//音量为0表示最大的音量,因此设置时必须为负。

            }

        }



        private void trackBar2_Scroll(object sender, EventArgs e)

        {

            if (secBuffer != null)

            {

                if (trackBar2.Value == 0)

                {

                    secBuffer.Pan = Convert.ToInt32(Pan.Left);//左声道

                }

                else if (trackBar2.Value == 2)

                {

                    secBuffer.Pan = Convert.ToInt32(Pan.Right);//右声道

                }

                else

                {

                    secBuffer.Pan = Convert.ToInt32(Pan.Center);

                }

            }

        }



    }

}

 

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