Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7016515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:47:44+00:00 2026-05-27T22:47:44+00:00

I need to play single sound repeatedly in my app, for example, a gunshot,

  • 0

I need to play single sound repeatedly in my app, for example, a gunshot, using XAudio2.

This is the part of the code I wrote for that purpose:

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();
        }
    }

The code above is actually based on SlimDX sample.

What it does now is, when I call Play() repeatedly, the sound plays like:

sound -> sound -> sound

So it just fills the buffer and plays it.

But, I need to be able to play the same sound while the current one is playing, so effectively these two or more should mix and play at the same time.

Is there something here that I’ve missed, or it’s not possible with my current solution (perhaps SubmixVoices could help)?

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.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:47:44+00:00Added an answer on May 27, 2026 at 10:47 pm

    Although it’s better option to use XACT for this purpose because it supports sound cues (exactly what I needed), I did managed to get it working this way.

    I’ve changed the code so it would always create new SourceVoice object from the stream and play it.

            // ------ 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();
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to play Music Library files using file URL, that I will set
I need to play a part of an MP3 file in my java code.
I have a single page web app that shows real time data using just
We need to play MP3 files in jPlayer without using Flash as a fallback.
I need to play a background sound all the time as long as my
I need to play and record at the same time. The problem is that
What I'm doing : I need to play audio and video files that are
I have an aes encrypted mp3 file that I need to play in iOS;
I need to play video file (using installed codecs) and get some file info
I'm analyzing leaks in my Objective-C++ code using the instrumentation, and I'm getting this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.