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模块,用来给别的函数对象提供绑定事件.触发事件的能力.这个别的函数的对象,我把它叫做事件宿主对象(非权威叫法),其原理是把宿主函数的原型链指向 ...
随机推荐
- (原)Ubuntu16中卸载并重新安装google的Protocol Buffers
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5782992.html 目前最新的是1.6.1 1. 卸载掉老版本的Protocol: sudo apt ...
- YUI的UA检测
YUI.UA是针对javascript的宿主环境检测的一个检测对象,返回的是一系统关于当前宿主的信息 1.对象相关信息列表及userAgent 检测对象o = { ie: 0, //ie Mozill ...
- PHP基础示例:用PHP+Mysql编写简易新闻管理系统
实现目标:使用php和mysql操作函数实现一个新闻信息的发布.浏览.修改和删除操作 实现步骤: 一.创建数据库和表 1.创建数据库和表:newsdb 2.创建表格:news 字段:新闻id,标题,关 ...
- Django中文无法转换成latin-1编码的解决方案
在Ubuntu上用Django做Web开发的时候遇到了中文保存到Cookie无法解析的问题,经过了下面几个步骤终于把问题解决了: 修改/usr/lib/python3.4/wsgiref/header ...
- java直接打开pdf,doc,xls
jsp页面: <a href=\'#\' onclick=onLine(\''+urls[i]+'\') >在线打开</a> html页面超链接单击打开online函数 var ...
- PHP PSR-3 日志接口规范 (中文版)
日志接口规范 本文制定了日志类库的通用接口规范. 本规范的主要目的,是为了让日志类库以简单通用的方式,通过接收一个 Psr\Log\LoggerInterface 对象,来记录日志信息. 框架以及CM ...
- python3.4+pyspider爬58同城(二)
之前使用python3.4+selenium实现了爬58同城的详细信息,这次用pyspider实现,网上搜了下,目前比较流行的爬虫框架就是pyspider和scrapy,但是scrapy不支持pyth ...
- oc语言--description方法和sel
一.description方法 Description方法包括类方法和对象方法.(NSObject类所包含) (一)基本知识 -description(对象方法) 使用NSLog和@%输出某个对象时, ...
- Dropdownlist的onchange事件应用
function selectDpList(dp) { var sIndex = dp.selectedIndex;//返回选中是第几项 0,1.... var sText = dp.options[ ...
- 8051_asm.uew
/L20"8051 Assembly" AASM_LANG Line Comment = ; Nocase String Chars = ' File Extensions = S ...