您的位置:首页 > 编程语言 > C#

DirectX初级编程:C#利用DirectSound播放WAV文件[最少只要4句话]

2017-02-22 20:26 591 查看
1.注意:

      a.DirectSound引用方法:http://blog.csdn.net/woaixiaozhe/article/details/7861340

      b.Mixed mode assembly is built against version 'v1.1.4322' of the runtime and...问题:http://blog.csdn.net/woaixiaozhe/article/details/7864391

2.原文如下:

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

        效果图如下 :

      


      平台: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"按钮通过设置缓冲区信息来对缓冲区做调整,让播放可以在失去焦点的时候继续播放。除了播放功能外,还可以控制音量和声道。

      代码如下:

[csharp] view
plain copy

<span style="font-size:14px;">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)  

12fe1

         {  

            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);  

                 }  

             }  

         }  

  

     }  

}</span>  



 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: