SlimDX的DirectSound模块
网上SlimDX的资源很少,搜到了http://www.xukailun.me/article/238/这篇关于《SlimDX的DirectSound模块应用实战》的文章,备份下来以备不时之需。
1.基本的播放功能
void CreateDirectSound(Guid deviceId, IntPtr handle)
{
// 音频设备的Id
_ds = new DirectSound(deviceId);
// 需要使用窗口的句柄
_ds.SetCooperativeLevel(handle, CooperativeLevel.Normal);
}
void Play(string file)
{
SecondarySoundBuffer snd = null;
// 读取wav音频文件
using (WaveStream ws = new WaveStream(file))
{
var desc = new SoundBufferDescription();
desc.Format = ws.Format;
desc.Flags = BufferFlags.GlobalFocus | BufferFlags.ControlVolume | BufferFlags.ControlPositionNotify | BufferFlags.GetCurrentPosition2;
desc.SizeInBytes = (int)ws.Length;
// 为声音建立二级缓冲区
snd = new SecondarySoundBuffer(_ds, desc);
snd.Volume = -;
byte[] buffer = new byte[desc.SizeInBytes];
ws.Read(buffer, , buffer.Length);
snd.Write<byte>(buffer, , LockFlags.None);
}
snd.Play(, PlayFlags.None);
}
2.播放中的回调
AutoResetEvent功能就类似于Java的wait/notify。调用WaitOne方法相当于wait,使当前线程等待;调用Set方法相当于调用notify,通知线程继续执行。
class SoundUnit
{
public int Id { get; private set; }
public SoundBuffer Buffer { get; private set; }
public AutoResetEvent Event { get; private set; }
public SoundUnit(SoundBuffer buffer)
{
this.Id = ++i;
this.Buffer = buffer;
this.Event = new AutoResetEvent(false);
}
}
void Play(string file)
{
SecondarySoundBuffer snd = null;
SoundUnit su = null;
// 读取wav音频文件
using (WaveStream ws = new WaveStream(file))
{
var desc = new SoundBufferDescription();
desc.Format = ws.Format;
desc.Flags = BufferFlags.GlobalFocus | BufferFlags.ControlVolume | BufferFlags.ControlPositionNotify | BufferFlags.GetCurrentPosition2;
desc.SizeInBytes = (int)ws.Length;
// 为声音建立二级缓冲区
snd = new SecondarySoundBuffer(_ds, desc);
snd.Volume = -;
byte[] buffer = new byte[desc.SizeInBytes];
ws.Read(buffer, , buffer.Length);
snd.Write&lt;byte&gt;(buffer, , LockFlags.None);
su = new SoundUnit(snd);
snd.SetNotificationPositions(new[]{
// 设置播放结束后回调
new NotificationPosition{ Event = su.Event, Offset = DSBPN_OFFSETSTOP } });
}
snd.Play(, PlayFlags.None);
Console.WriteLine("播放开始:" + su.Id);
WaitForNotification(su);
}
void WaitForNotification(SoundUnit unit)
{
new Thread(() =>{
// 等待播放完成
unit.Event.WaitOne();
Console.WriteLine("播放结束:" + unit.Id); }).Start();
}
SlimDX的DirectSound模块的更多相关文章
- DirectSound的应用
假设仅仅使用PlaySound()这个API函数来表现声音效果的话,那么就无法表现出声音的混音效果,由于PlaySound在播放还有一个声音时,必定会导致现有声音的停止.因此,使用 PlaySound ...
- DirectSound应用
只是使用的假设PlaySound()这个API函数来显示的声音效果,然后,然后,它不会出现在混合声音,因为PlaySound还有播放期间声音,这将不可避免地导致现有声音停止. 因此,使用 PlaySo ...
- npm 私有模块的管理使用
你可以使用 NPM 命令行工具来管理你在 NPM 仓库的私有模块代码,这使得在项目中使用公共模块变的更加方便. 开始前的工作 你需要一个 2.7.0 以上版本的 npm ,并且需要有一个可以登陆 np ...
- node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理
一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...
- ES6模块import细节
写在前面,目前浏览器对ES6的import支持还不是很好,需要用bable转译. ES6引入外部模块分两种情况: 1.导入外部的变量或函数等: import {firstName, lastName, ...
- Python标准模块--ContextManager
1 模块简介 在数年前,Python 2.5 加入了一个非常特殊的关键字,就是with.with语句允许开发者创建上下文管理器.什么是上下文管理器?上下文管理器就是允许你可以自动地开始和结束一些事情. ...
- Python标准模块--Unicode
1 模块简介 Python 3中最大的变化之一就是删除了Unicode类型.在Python 2中,有str类型和unicode类型,例如, Python 2.7.6 (default, Oct 26 ...
- Python标准模块--Iterators和Generators
1 模块简介 当你开始使用Python编程时,你或许已经使用了iterators(迭代器)和generators(生成器),你当时可能并没有意识到.在本篇博文中,我们将会学习迭代器和生成器是什么.当然 ...
- 自己实现一个javascript事件模块
nodejs中的事件模块 nodejs中有一个events模块,用来给别的函数对象提供绑定事件.触发事件的能力.这个别的函数的对象,我把它叫做事件宿主对象(非权威叫法),其原理是把宿主函数的原型链指向 ...
随机推荐
- [Red5]Red5之Flash流媒体服务器的安装与使用教程完整版(组图)
参看下面链接:http://www.cuplayer.com/player/PlayerCode/Red5/2013/0319/760.html
- ajax + php + Controller 控制所有后台函数调用
转载请注明出处:http://www.cnblogs.com/ghypnus/p/4645873.html 好久没有来这边发布代码了 总共分成3大部分来完成php的ajax调用逻辑,以下是大致的结构 ...
- iptables中规则的关系——以只允许某些IP段访问为例
最近遇到一个问题:服务器被全球的IP频繁试图通过ssh登录. 于是想到通过iptables防火墙限制访问,达到:仅允许指定ip段访问ssh服务(22端口). 关于iptables添加规则的文章有很多, ...
- linux系统文件夹的作用 good
/bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 如:环境变量 /etc/rc.d 启动的配置文件和脚本 /home用户主目录的基点,比如用户user的主目录就是/ho ...
- C# Stopwatch类_性能_时间计时器
在研究性能的时候,完全可以使用Stopwatch计时器计算一项技术的效率.但是有时想知道某想技术的性能的时候,又常常想不起可以运用Stopwatch这个东西,太可悲了. 属性: Elapsed 获取当 ...
- xampp安装时mysql报错
问题描述:以前安装过mysql,后来安装xampp,mysql打不开,出错提示16:04:48 [mysql] MySQL Service detected with wrong path16:0 ...
- Linux系统编程(9)—— 进程之进程控制函数exec系列函数
在Linux中,并不存在exec()函数,exec指的是一组函数,一共有6个,分别是: #include <unistd.h> extern char **environ; int exe ...
- Bucket Sort
(referrence: GeekforGeeks) Bucket sort is mainly useful when input is uniformly distributed over a r ...
- openstack vm image
1,openstack 基于iso生成镜像
- java中a++与++a区别
java中a++与++a区别 a++与++a的区别,如果单独使用没有任何区别,如果在运算中就有区别了,a++是先运算在赋值,而++a是先赋值在运算!! 先看a++的代码哦 class demo1 { ...