使用DirectX播放wav声音文件

1.    使用 DirectX ,主要用到 MicroSoft.DirectX.dll 和 Microsoft.Directx.DirectSound.dll。

        a. 可以下载安装DirectX,下载地址https://www.microsoft.com/en-us/download/details.aspx?id=6812

             然后添加引用MicroSoft.DirectX.dll和Microsoft.Directx.DirectSound.dll。

        b. 也可以直接下载dll文件,下载地址https://www.dllme.com/dll/download/7588/Microsoft.DirectX.dll

2.    安装DirectX SDK时,无法安装成功,遇到 Error Code : S1023,  打开文件 C:\Users\用户名\AppData\Local\Temp\Microsoft Visual C++ 2010  x86             Redistributable .html

          

     也就是说: VS已经安装了更高版本的Microsoft Visual C++ 2010 Redistributable ,所以导致无法成功安装DirectX SDK,在控制面板中卸载高版本的 这个文件,重新安装directX SDK 即可。

3.    安装完成后,新建一个winform项目,添加引用MicroSoft.DirectX.dll 和 Microsoft.Directx.DirectSound.dll, 却无法找到这个两个dll文件, 实际上这  两  个dll文件不在安装目录下。在C:\Windows\Microsoft.NET\DirectX for Managed Code\  ..文件下可以找到各个版本的DirectX.dll    


         


          


将这两文件添加引用即可。

 4   开始写代码

          (1).引入DirectX 的DLL文件的名字空间:

using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;

  (2)建立设备。在Microsoft.DirectX.DirectSound空间中,有个Device的类。这个是表示系统中的声音设备。

Device dev=new Device();

  (3).设置CooperativeLevel。因为windows是多任务的系统,设备不是独占的,所以在使用设备前要为这个设备设置CooperativeLevel。调用Device的SetCooperativeLevel方法:其中,第一个参数是一个Control,可以用this代替,第二个参数是个枚举类型,设置优先级。         

dev.SetCooperativeLevel(this,CooperativeLevel.Normal);

  (4).开辟缓冲区。系统中,一个设备有唯一的主缓冲区。由于windows是多任务,所以可以有几个程序同时利用一个设备播放声音,所以每个程序都自己开辟一个二级缓冲区,播放自己的声音。
  系统根据各个程序的优先级别,按照相应的顺序分别去各个二级缓冲区中读取内容到主缓冲区中播放。
  其中,第一个参数表示(要播放的.wav)文件名,第二个就是需要使用的设备。

SecondaryBuffer buf=new SecondaryBuffer("hello.wav",dv);

  (5).接下来就可以播放啦。第一个参数表示优先级别,0是最低的。第2个参数是播放方式,这里是循环播放。

buf.Play(0,BufferPlayFlags.Looping);

  代码完成,可以编译运行了。

  5.     运行项目时,debug模式下报出异常 

       Mixed mode assembly is built against version 'v1.1.4322' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

解决方案:
        https://stackoverflow.com/questions/14508627/mixed-mode-assembly-is-built-against-version-2-0-50727-of-the-runtime-and-cann

项目--->右键------>add new Item----->App config file  即添加App.config文件,文件中输入下面内容。
<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
  <startup useLegacyV2RuntimeActivationPolicy="true"> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> 
  </startup> 
</configuration> 

  

  再次运行项目,就可以听到.wav文件播放出的声音了吐舌头

代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;
namespace directXTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Device dev = new Device();//系统中的声音设备。
            dev.SetCooperativeLevel(this, CooperativeLevel.Normal);// 设置设备优先级。
            SecondaryBuffer buf = new SecondaryBuffer("music.wav", dev);//wav文件可以放在debug文件下。
            buf.Play(0, BufferPlayFlags.Looping);
        }
    }
}







   



猜你喜欢

转载自blog.csdn.net/sophiemantela/article/details/78564560
今日推荐