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

学习笔记,VB.NET使用DirectSound9 (1) 简单播放

2005-11-08 09:06 1096 查看
关键字: DirectX9 DirectSound VB.NET 作者:董含君
来自 http://blog.csdn.net/a11s
网上看的源代码都是C++的,最好的打算也是C#的,去baidu搜索,发现想找的人不少.于是想起了在CSDN还有一个blog.希望能够跟大家分享.3-17就要考试了,估计要写的话也要很久以后才能继续研究了.

关于VB6的爱好者.大家还是尽快放弃VB6吧,我从97年开始用,现在终于也到了.Net了,中间的痛苦可想而知.尤其是服务器编程,.NET会方便很多.客户端随着XP的推广跟windows update的升级,也快了.到时候别没有准备.

我的学习方法

看SDK的文档,了解诸多概念,然后看SDK C#的教程(已经习惯了)

准备工作

VS2003(有2002的,但是不用那个版本)

DXSDK 9.0C 2004 Dec(记住,是for VS2k3的,曾经有过VS2k2的,而且很多地方不通用)

Win2k(这个久不用说了吧)

大体步骤

1 首先要添加引用.否则直接imports没办法找到 Microsoft.Directx....

2 引用完了,为了方便再imports Microsoft.DirectX.directsound

为了播放一个简单的声音文件进行如下操作

创建一个设备,关联到这个窗体
创建一个缓冲,指定来源(这里用文件)
播放...

Dim ad As Device '创建设备
Dim Buf As SecondaryBuffer '创建缓冲
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ad = New Device '实力化设备对象
ad.SetCooperativeLevel(Me.Handle, CooperativeLevel.Normal) '关联
Buf = New SecondaryBuffer("G:\MEDIA\WAV\ff9start.wav", ad) '实力化缓冲区
Buf.Play(0, BufferPlayFlags.Looping) '播放,同时指定开始位置跟方式
End Sub

这是一个最基本最简单的例子,目的是了解步骤

有趣的现象,由于DS关联的是窗体,当窗体失去焦点的时候,他会停止播放

一旦重新获得焦点会继续播放

很方便吧

但是DS用的是缓冲区的,在切换的时候,会丢失声音,少了那么一点

SDK也提到过,但是我没有认真看(1 感觉问题不大(除非录音) 2 英文的,懒~)

实际上没有必要解决丢失问题,因为对buf描述的时候就有这个选项

普通的(就是默认的,我用的这种)
Sticky 看原文吧:Buffers with \"sticky\" focus will continue to play if the user switches to another application not using DirectSound. However, if the user switches to another DirectSound application, all normal-focus and sticky-focus buffers in the previous application are muted.";

全局的:Buffers with global focus will continue to play if the user switches focus to another application, even if the new application uses DirectSound. The one exception is if you switch focus to a DirectSound application that uses the DSSCL_WRITEPRIMARY cooperative level. In this case, the global-focus buffers from other applications will not be audible.";

再就是 默认 硬件加速 软件加速 (用默认就好,有硬件就用硬件,否则模拟)

一下是原文
if (MixHardware)
{
sText = sText + "\n\nWith the hardware mixing flag, the new buffer will be forced to use hardware mixing. If the device does not support hardware mixing or if the required hardware resources are not available, the call to the DirectSound.CreateSoundBuffer method will fail.";
}
else if (MixSoftware)
{
sText = sText + "\n\nWith the software mixing flag, the new buffer will use software mixing, even if hardware resources are available.";
}
else
{
// Default mixing
sText = sText + "\n\nWith default mixing, the new buffer will use hardware mixing if available, otherwise software mixing will be used.";
}

由于内容很简单,没有翻译的必要(主要是因为懒,自己能看懂稍微记录一下就算了)

实际上DirectSound能作的工作很多

包括特效,3D音效跟混音以及声音抓取(录音?)

今天比较晚了,还有音量控制没有研究.是一个BufferDescription

描述缓冲区要使用的功能

设置上之后才能用buf.pan=... 来修改,我是这么理解的,下次再尝试,感觉不是很难.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: