Sorry, the Forum is closed :(

Unfortunately, the forum is now closed.

Please read the following post for further details.

We apologize for the inconvenience.
The SharpDX team.
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC: Play sound in a game - too slow.

Play sound in a game - too slow. 1 year 4 months ago #1311

I have created a simple game for WinRT, and now I want to add sound when the user clicks the buttons.
How can I play the same sound many times? I'm using this code, but it's too slow - sometimes when I click the buttons too fast, the sound is not played.


The class is a Service of MVVM, but if you don't know how to use it, treat it as a static class.
public class SoundService : ISoundService
    {
        #region Variables
        private SourceVoice VoicePlayer;
        private AudioBuffer ClickBuffer;
        uint[] packetInfo;
        #endregion

        #region Public Methods
        public async void PlayClickSound()
        {
            InitializePlayers();

	    	VoicePlayer.SubmitSourceBuffer(ClickBuffer, packetInfo);
            VoicePlayer.Start();
            
        }
        #endregion

        #region Private Methods
        private void InitializePlayers()
        {
            if (this.playersInitialized)
                return;

            XAudio2 xaudio;
            MasteringVoice masteringVoice;

            xaudio = new XAudio2();
            masteringVoice = new MasteringVoice(xaudio);

            NativeFileStream nativefilestream = new NativeFileStream(
                @"Data\Sounds\Click.wav",
                NativeFileMode.Open,
                NativeFileAccess.Read,
                NativeFileShare.Read);

            SoundStream soundstream = new SoundStream(nativefilestream);


            WaveFormat waveFormat = soundstream.Format;
            ClickBuffer = new AudioBuffer
            {
                Stream = soundstream.ToDataStream(),
                AudioBytes = (int)soundstream.Length,
                Flags = BufferFlags.EndOfStream
            };

            VoicePlayer = new SourceVoice(xaudio, waveFormat, true);

            this.packetInfo = soundstream.DecodedPacketsInfo;
            

            this.playersInitialized = true;
        }
        #endregion

        #region Flags
        bool playersInitialized = false;
        #endregion
    }

What shall I do? Please, help.
Last Edit: 1 year 4 months ago by jozefkarton.
The administrator has disabled public write access.

Re: Play sound in a game - too slow. 1 year 4 months ago #1318


  • Posts:843 Thank you received: 1
  • xoofx's Avatar
  • xoofx
  • Administrator
  • OFFLINE
Use a pool of SourceVoice, with BufferEnd event to release or dispose the audio buffer if you need to. The same source voice can only play a single buffer at a time.

Also, read resources from "Learning XAudio2" as it covers all the basic and advanced usages while using XAudio2.
The administrator has disabled public write access.
  • Page:
  • 1
Time to create page: 0.332 seconds