Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 游戏天堂 > 游戏开发 > 建立第一个directX程序——在C#下利用DirectSound实现声音播放
【标  题】:建立第一个directX程序——在C#下利用DirectSound实现声音播放
【关键字】:directX,DirectSound
【来  源】:http://blog.csdn.net/cutebab0888/archive/2006/08/30/1143310.aspx

建立第一个directX程序——在C#下利用DirectSound实现声音播放

Your Ad Here

这是给directX初学者的教程,如果你是大虾,大可不必理会本文。

第1步:下载并安装DirectX 9 SDK

DirectX 9 SDK下载地址:http://msdn.microsoft.com/directx/sdk/ ,目前最新版本是2006年8月,下载包500MB多。

第2步:建立C#应用程序

新建一个C#的windows应用程序,名称这里输mydirectXtest。

解决方案管理器里,右击项目,“添加引用”,选中DirectX和DirectSound,如下图:


在Form1.cs里面添加:

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

往Form1上面拉一个Label和一个Button,在Button onclick事件里面写入:

// 建立声音设备
Microsoft.DirectX.DirectSound.Device dev =
    
new Microsoft.DirectX.DirectSound.Device();

dev.SetCooperativeLevel(
this,
  Microsoft.DirectX.DirectSound.CooperativeLevel.Normal);

// 为声音建立二级缓冲区
try
{
    Microsoft.DirectX.DirectSound.SecondaryBuffer snd 
=
    
new Microsoft.DirectX.DirectSound.SecondaryBuffer(
      
"../../NewDrums.wav", dev);

    
// 播放声音
    snd.Play(0, Microsoft.DirectX.DirectSound.BufferPlayFlags.Default);
}

catch (Exception ex)
{
    label1.Text 
= ex.ToString();
}

 

Microsoft.DirectX.DirectSound.Device dev = new Microsoft.DirectX.DirectSound.Device(); ——建立device的类;

dev.SetCooperativeLevel(this
, Microsoft.DirectX.DirectSound.CooperativeLevel.Normal); ——设置CooperativeLevel。因为Windows是多任务的系统,设备不是独占的,所以在使用设备前要为这个设备设置CooperativeLevel。调用Device的SetCooperativeLevel方法:其中,第一个参数是一个Control;第二个参数是个枚举类型,用来设置优先级的。

SecondaryBuffer snd = new Microsoft.DirectX.DirectSound.SecondaryBuffer("../../NewDrums.wav
", dev); —— 开辟缓冲区。声音设备有个自己的缓冲区,叫主缓冲区。系统中,一个设备有唯一的主缓冲区。由于windows是多任务的,所以可以有几个程序同时利用一个设备播放声音,每个程序都自己开辟一个二级缓冲区,放自己的声音。
 

这里需要注意播放声音的路径,一开始初学者容易把wav声音放到项目里面,在SecondaryBuffer里面直接写“NewDrums.wav”,调试是会显示“应用程序错误”。因为调试的默认文件夹是Debug,需要的声音文件应该放到Debug目录下,用“NewDrums.wav”的格式;或者放在项目下面,用“../../NewDrums.wav”的格式。很傻的错误吧。

这样,调试程序,按button就会播放声音了。全部代码如下:
Form1.cs:

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;
using Microsoft.DirectX.DirectSound;


namespace mydirectXtest
{
    
public partial class Form1 : Form
    
{
        
public Form1()
        
{
            InitializeComponent();
        }


        
private void button1_Click(object sender, EventArgs e)
        
{

            
// 建立声音设备
            Microsoft.DirectX.DirectSound.Device dev =
                
new Microsoft.DirectX.DirectSound.Device();

            dev.SetCooperativeLevel(
this,
              Microsoft.DirectX.DirectSound.CooperativeLevel.Normal);

            
// 为声音建立二级缓冲区
            try
            
{
                Microsoft.DirectX.DirectSound.SecondaryBuffer snd 
=
                
new Microsoft.DirectX.DirectSound.SecondaryBuffer(
                  
"../../NewDrums.wav", dev);

                
// 播放声音
                snd.Play(0, Microsoft.DirectX.DirectSound.BufferPlayFlags.Default);
            }

            
catch (Exception ex)
            
{
                label1.Text 
= ex.ToString();
            }


        }

    }

}

XNA Framework?? waiting~~,waiting~~~:【上一篇】
Using an Input Method Editor in a Game:【下一篇】
【相关文章】
  • DMO(DirectX Media Object)的工程创建过程及其调用方式
  • DirectX是什么
  • 一个DirectX的Sample引发的学习(1)
  • DirectX 编程 第四步: 顶点(Vertex)绘图
  • Game Programming with DirectX -- 10[粒子系统的实现]
  • Game Programming with DirectX -- 11[粒子系统的实际应用]
  • 关于微软的DMO(Directx Media Object)MSDN翻译文章(一)
  • Game Programming with DirectX -- 08[Mesh]
  • 深入Managed DirectX9(一)
  • 深入Managed DirectX9(二)
  • 【随机文章】
  • window.resizeBy()方法使用示例
  • 如何在强制类型中定义NULL
  • MSN群建设完毕,等待CMS开拓者进入
  • TCP/IP基础笔记(1)
  • 如何让DevExpress的DateEdit控件正确显示日期的周名
  • 我的aix基本命令粘贴簿
  • 三招保护局域网中的IP地址
  • 软件日志 | applog:mcool起源
  • 科龙布线:西安第二长途通信大楼
  • JNDI File System Service Provider
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.