现在的位置: 首页 > 综合 > 正文

[C#] DirectX 播放声音

2014年01月27日 ⁄ 综合 ⁄ 共 1076字 ⁄ 字号 评论关闭

  妈的的巴子 , 看到一个程序 要安装DirectX来处理图像和声音  我顺道研究了使用它来放音乐  
1: 首先引用 MicroSoft.DirectX.dll (在C:\Windows\Microsoft.NET\Framework)
2:添加命名空间 :

using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound

3: Microsoft.DirectX.DirectSound空间中,有个Device的类。这个是表示系统中的声音设备。实例化成员

Device device=new Device();

4: 设置CooperativeLevel。因为windows是多任务的系统,设备不是独占的,所以在使用设备前要为这个设备设置CooperativeLevel。调用Device的SetCooperativeLevel方法:其中,第一个参数是一个Control,第二个参数是个枚举类型,第二个参数就是优先级别,这里表示优先播放。

device.SetCooperativeLevel((new UF()),CooperativeLevel.Priority);

5: 开辟缓冲区。他有个自己的缓冲区,叫主缓冲区。第一参数文件名,第二个就是需要使用的设备。

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

6:这哈就是播放了卅 ,第一个参数表示优先级别,0是最低的。第2个参数是播放方式,这里是循环播放

buf.Play(0,BufferPlayFlags.Looping);

7:完整代码:

using System;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;
using System.Windows.Forms;
namespace test1
{
 class test
 {
  public static void Main(string [] args)
  {
   Device device=new Device();
   dv.SetCooperativeLevel((new UF()),CooperativeLevel.Priority);
   SecondaryBuffer buf=new SecondaryBuffer(@"snd.wav",device);
   buf.Play(0,BufferPlayFlags.Looping);
   Console.ReadLine();
  }
  class UF:Form{} 
 }
}





抱歉!评论已关闭.