使用DirectSound播放背景音乐

时间:2014-09-14 03:57:26

标签: c# vb.net directx directsound

我正在使用DirecSound在我的应用中播放声音,因为我需要它能够同时播放多个声音。但是,当我在alt-tab或甚至点击任何其他窗口时声音停止播放。即使应用没有聚焦,我可以在后台播放它吗?

我正在使用的代码基本上是这样的:

Dim soundDevice As New Device
soundDevice.SetCooperativeLevel(Me.Handle, CooperativeLevel.Normal)
Dim sb As New SecondaryBuffer(My.Resources.ResourceManager.GetStream("MyFile.wav"), soundDevice)
sb.play(0, BufferPlayFlags.Default)

1 个答案:

答案 0 :(得分:0)

哦,非常感谢@ user3560941用另一个问题回答我的问题:)(https://stackoverflow.com/questions/23223028/change-directsound-behavior

答案是创建一个BufferDescription并将其标志设置为DSBCAPS_GLOBALFOCUS:

Dim soundDevice As New Device
soundDevice.SetCooperativeLevel(Me.Handle, CooperativeLevel.Normal)
Dim bd As New BufferDescription
bd.Flags = BufferDescriptionFlags.GlobalFocus
Dim sb As New SecondaryBuffer(My.Resources.ResourceManager.GetStream("MyFile.wav"), bd, soundDevice)
sb.play(0, BufferPlayFlags.Default)