作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要使用 XAudio2 在我的应用中重复播放单个声音,例如枪声。
这是我为此目的编写的部分代码:
public sealed class X2SoundPlayer : IDisposable
{
private readonly WaveStream _stream;
private readonly AudioBuffer _buffer;
private readonly SourceVoice _voice;
public X2SoundPlayer(XAudio2 device, string pcmFile)
{
var fileStream = File.OpenRead(pcmFile);
_stream = new WaveStream(fileStream);
fileStream.Close();
_buffer = new AudioBuffer
{
AudioData = _stream,
AudioBytes = (int) _stream.Length,
Flags = BufferFlags.EndOfStream
};
_voice = new SourceVoice(device, _stream.Format);
}
public void Play()
{
_voice.SubmitSourceBuffer(_buffer);
_voice.Start();
}
public void Dispose()
{
_stream.Close();
_stream.Dispose();
_buffer.Dispose();
_voice.Dispose();
}
}
上面的代码实际上是基于SlimDX示例。
它现在所做的是,当我重复调用 Play() 时,声音播放如下:
sound -> sound -> sound
所以它只是填充缓冲区并播放它。
但是,我需要能够在当前正在播放的声音时播放相同的声音,因此有效地,这两个或更多应该同时混合和播放。
这里是否有我遗漏的东西,或者我当前的解决方案无法实现(也许 SubmixVoices 可以提供帮助)?
我试图在文档中找到相关的东西,但我没有成功,而且我可以引用的在线示例也不多。
谢谢。
最佳答案
虽然为此目的使用 XACT 是更好的选择,因为它支持声音提示(正是我所需要的),但我确实设法让它以这种方式工作。
我已经更改了代码,因此它总是会从流中创建新的 SourceVoice 对象并播放它。
// ------ code piece
/// <summary>
/// Gets the available voice.
/// </summary>
/// <returns>New SourceVoice object is always returned. </returns>
private SourceVoice GetAvailableVoice()
{
return new SourceVoice(_player.GetDevice(), _stream.Format);
}
/// <summary>
/// Plays this sound asynchronously.
/// </summary>
public void Play()
{
// get the next available voice
var voice = GetAvailableVoice();
if (voice != null)
{
// submit new buffer and start playing.
voice.FlushSourceBuffers();
voice.SubmitSourceBuffer(_buffer);
voice.Start();
}
}
关于c# - 如何使用 XAudio2 重复播放相同的声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8512413/
我最近一直在深入研究 xAudio,但未能找到我需要的一些答案。 {不要浪费你的时间 - 我需要找到一种方法来获得主语音的当前进度 事情是,- SourceVoice 确实提供了一种方法来给出它的状态
我正在开发一个游戏引擎,我发现了一些困难。过去,我尝试使用 DirectX SDK(2010 年 6 月),但我与 Windows 8 Kit 及其自带的 Direct 冲突太多。我决定只使用来自 W
我是一名优秀的程序员,十分优秀!