网上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&amp;lt;byte&amp;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();
}

最新文章

  1. zen Code 支持的代码样式
  2. Oracle Jdbc demo
  3. 配置FileZilla Ftp服务器
  4. VHDL----基础知识1
  5. 关于Navicat Premium导入xlsx的问题
  6. paip.索引优化---sql distict—order by 法
  7. mac终端 使用摘要
  8. kgdb接收一个数据包详解
  9. HTML5入门总结 HTML5API
  10. JS 生成唯一数字
  11. css :root 选择器
  12. Git 使用vi或vim命令打开、关闭、保存文件
  13. Web Penetration Testing w3af fierce
  14. CSS魔法堂:稍稍深入伪类选择器
  15. 内部排序-&gt;交换排序-&gt;快速排序
  16. CentOS 7 :Failed to start IPv4 firewall with iptables.
  17. (转)MVC一个页面多个submit
  18. eclipse 64和32位切换
  19. BZOJ1907 树的路径覆盖
  20. Socket 之 传值方式

热门文章

  1. jquery插件编写
  2. 用Python写的简单脚本更新本地hosts
  3. Hopscotch(POJ 3050 DFS)
  4. C++----练习--string 从文件中一个一个单词的读直到文件尾
  5. Planner – 项目管理软件
  6. Linux APP源码级编译安装
  7. 剑指offer-面试题2.实例Singleton模式
  8. hdu 4619 Warm up 2 ( 二分图最大匹配 )
  9. java面试题集3
  10. iOS 字体设置