Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to play same sound repeatedly with XAudio2?
    primarykey
    data
    text
    <p>I need to play single sound repeatedly in my app, for example, a gunshot, using XAudio2.</p> <p>This is the part of the code I wrote for that purpose:</p> <pre><code>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(); } } </code></pre> <p>The code above is actually based on SlimDX sample.</p> <p>What it does now is, when I call Play() repeatedly, the sound plays like:</p> <blockquote> <p>sound -> sound -> sound</p> </blockquote> <p>So it just fills the buffer and plays it.</p> <p>But, I need to be able to play the same sound <strong>while</strong> the current one is playing, so effectively these two or more should mix and play at the same time.</p> <p>Is there something here that I've missed, or it's not possible with my current solution (perhaps SubmixVoices could help)? </p> <p>I'm trying to find something related in docs, but I've had no success, and there are not many examples online I could reference.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload