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

这是给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事件里面写入:

 1// 建立声音设备
 2Microsoft.DirectX.DirectSound.Device dev =
 3    new Microsoft.DirectX.DirectSound.Device();
 4
 5dev.SetCooperativeLevel(this,
 6  Microsoft.DirectX.DirectSound.CooperativeLevel.Normal);
 7
 8// 为声音建立二级缓冲区
 9try
10{
11    Microsoft.DirectX.DirectSound.SecondaryBuffer snd =
12    new Microsoft.DirectX.DirectSound.SecondaryBuffer(
13      "../../NewDrums.wav", dev);
14
15    // 播放声音
16    snd.Play(0, Microsoft.DirectX.DirectSound.BufferPlayFlags.Default);
17}

18catch (Exception ex)
19{
20    label1.Text = ex.ToString();
21}


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:

 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel;
 4using System.Data;
 5using System.Drawing;
 6using System.Text;
 7using System.Windows.Forms;
 8
 9using Microsoft.DirectX;
10using Microsoft.DirectX.DirectSound;
11
12
13namespace mydirectXtest
14{
15    public partial class Form1 : Form
16    {
17        public Form1()
18        {
19            InitializeComponent();
20        }

21
22        private void button1_Click(object sender, EventArgs e)
23        {
24
25            // 建立声音设备
26            Microsoft.DirectX.DirectSound.Device dev =
27                new Microsoft.DirectX.DirectSound.Device();
28
29            dev.SetCooperativeLevel(this,
30              Microsoft.DirectX.DirectSound.CooperativeLevel.Normal);
31
32            // 为声音建立二级缓冲区
33            try
34            {
35                Microsoft.DirectX.DirectSound.SecondaryBuffer snd =
36                new Microsoft.DirectX.DirectSound.SecondaryBuffer(
37                  "../../NewDrums.wav", dev);
38
39                // 播放声音
40                snd.Play(0, Microsoft.DirectX.DirectSound.BufferPlayFlags.Default);
41            }

42            catch (Exception ex)
43            {
44                label1.Text = ex.ToString();
45            }

46
47        }

48    }

49}


 

posted on 2006-08-30 11:53 zenith 阅读(11994) 评论(11) 编辑 收藏

评论

#1楼 2006-09-09 11:16 san[匿名]

觉得好玩。。。做了一下
支持
 回复 引用   

#2楼 2006-09-09 11:33 san[匿名]

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;

namespace myDirectXTest
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnOk;
private System.Windows.Forms.Label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnOk = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btnOk
//
this.btnOk.Location = new System.Drawing.Point(184, 64);
this.btnOk.Name = "btnOk";
this.btnOk.Size = new System.Drawing.Size(72, 24);
this.btnOk.TabIndex = 0;
this.btnOk.Text = "OK";
this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(64, 56);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 32);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(304, 277);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnOk);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void btnOk_Click(object sender, System.EventArgs e)
{
//建立声音设备
Microsoft.DirectX.DirectSound.Device dv = new Device();
dv.SetCooperativeLevel(this, Microsoft.DirectX.DirectSound.CooperativeLevel.Normal);

//为声音建立二级缓冲区
try
{
Microsoft.DirectX.DirectSound.SecondaryBuffer snd = new SecondaryBuffer("E:\\san\\C#\\520show_Blogs_Z-CH\\myDirectXTest\\myDirectXTest\\bin\\Debug魔比斯环.wmv", dv);

//播放声音
snd.Play(0, Microsoft.DirectX.DirectSound.BufferPlayFlags.Default);
}
catch(Exception ee)
{
this.label1.Text = ee.Message.ToString();
}
}
}
}
我写的怎么不能放啊?
 回复 引用   

#3楼[楼主] 2006-09-13 08:40 zenith      

是wmv的格式啊,你先换wav看行不?  回复 引用 查看   

#4楼 2006-10-22 09:01 露雨城市.NET2.0和Sql Server 2005开发研究      

楼主会不会在Form上DrawText啊?想学不DirectDraw。谢谢。  回复 引用 查看   

#5楼[楼主] 2006-10-22 14:41 zenith      

听说DirectDraw是过时的,微软不推荐用它。为什么不用Microsoft.DirectX.Direct3D来实现DrawText功能呢?  回复 引用 查看   

#6楼 2007-09-17 12:03 叶峰      

第一次接触DX, 还以为DX只能在C++和VB下用, 看到这个例子,真是一文惊醒梦中人,这个例子非常好,写的很详细,支持楼主,期待更好的教程  回复 引用 查看   

#7楼 2008-07-02 22:28 jomshuo[未注册用户]

不知道楼主 能不能写一篇如何安装和配置directX的文章 谢谢  回复 引用   

#8楼 2008-07-05 20:15 James1233123123[未注册用户]

请问可以支持MP3格式吗?都支持什么格式的音乐文件啊,加我QQ22550450,给我发邮件也可以,谢谢  回复 引用   

#9楼 2009-06-19 14:09 WCJ      

请问可以支持那些格式的音乐文件  回复 引用 查看   

#10楼 2009-11-04 19:00 步路      

好像只能支持wav格式的。  回复 引用 查看   

#11楼 2010-06-25 11:56 _ascu_      

您好,想问您一个问题,是关于c#写directx的。具体来说我的主程序是一个windows窗口,上面有一个按钮,当我按下按钮就会出现全屏界面,界面上有一个图片。注意,后面是全屏的显示图片。先谢谢了。  回复 引用 查看   

<2006年8月>
303112345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

统计

公告

昵称:zenith
园龄:5年5个月
粉丝:2
关注:0

搜索

 
 

常用链接

最新随笔

随笔分类(24)

随笔档案(23)

积分与排名

最新评论

阅读排行榜

评论排行榜

推荐排行榜