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模块,用来给别的函数对象提供绑定事件.触发事件的能力.这个别的函数的对象,我把它叫做事件宿主对象(非权威叫法),其原理是把宿主函数的原型链指向 ...
随机推荐
- android 利用重力感应监听 来电时翻转手机后静音。
在CallNotifier.java中 加入如下代码: public void GetSensorManager(Context context) { sm = (SensorManager) ...
- Android SDK 更新时修改hosts文件仍然无法更新,可试试这个方法……
Android SDK 更新时修改hosts文件仍然无法更新,此时必定万分蛋疼.在hosts文件中更换了各种ip,仍然解决不了!!!!!!!!!!!!!!? 第一步: 打开此软件,等待服务器连接 第二 ...
- vmware9安装centos和Mac经验总结
其实以前安装过,但是这次安装发现还是很陌生 主要是因为以下几点vm11下载的最新版本结果不支持用vm-Mac激活包一直激活不成功,所以只好用vm9来进行,中间的细节操作就是要停止的那几项VMware服 ...
- 梯田(dfs)
梯田 Time Limit: 2000 ms Memory Limit: 256 MBTotal Submission: 26 Submission Accepted: 5 Descrip ...
- Html辅助方法 之 Form表单标签
一.Html.BeginForm <form>标签 //视图代码@using (Html.BeginForm("search", "home", F ...
- jQuery之Jcrop
头像裁剪是一个经常用到的功能,实现原理也较为简单,就是在本地选择好所需裁剪图片的坐标,将坐标发送到服务器,由服务器执行图片裁剪操作. jQuery插件Jcrop提供了强大的图片裁剪坐标选择插件.一下来 ...
- jQuery开发经验实例笔记
jQuery获取文件选择输入框的扩展名 var file=$("input[name='file']").val() var filename=file.replace(/.*(\ ...
- Fragment与Activity相互传递数据:
Activity向Fragment传递数据:在Activity中创建Bundle数据包,并调用Fragment的setArguments(Bundle bundle)方法即可将Bundle数据包传给F ...
- html模块一些方法
<pre name="code" class="python"> find_by_tag_name: @elements = $h->find ...
- ###Android 断点调试和高级调试###
转自:http://www.2cto.com/kf/201506/408358.html 有人说Android 的调试是最坑的,那我只能说是你不会用而已,我可以说Android Studio的调试是我 ...