Thread: How to play wav file without blocking application with xaudio2

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    1

    How to play wav file without blocking application with xaudio2

    Hi!
    I am using xaudio2 to trigger very simple 100 ms .wav files. The timing is very important in this application, and when the mouse is clicked, the application sends a trigger to the parallel port, and 40 ms later, plays the sound. To play sound, I used MSDN's sample code Xaudio2BasicSound (find it here: https://code.msdn.microsoft.com/wind...mples-024b3933). In that code, the PlayWave() function is defined, which reads the wave file, creates a source voice, submits the data to a buffer, and plays the sound. However, the big problem is that it blocks the application while the sound is playing. I need to send additional triggers to my parallel port while the sound is playing, for example 10 ms after the beginning of the sound. What is it in this playwave function which is blocking the rest of the application? How can I modify that so that it effectively plays my sound in the background and still processes my parallel port triggers?

    I am attaching my code, which contains all the xaudio2 functionalities as well as the timing for the different events, in the RunTrials() function.
    tactile2ifc_piezo script.txt
    Thank you so much.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You could use a thread to just do the playback and wait around for the audio to complete.

    Or you could use PlaySound function (Windows) and pass the SND_ASYNC flag to do all the work for you in the background.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pros and Cons of blocking versus non-blocking sockets?
    By abachler in forum Networking/Device Communication
    Replies: 4
    Last Post: 05-08-2008, 06:52 AM
  2. USB File Blocking
    By peckitt99 in forum C++ Programming
    Replies: 6
    Last Post: 11-14-2007, 07:09 AM
  3. Application(GUI) and sockets(other blocking things)
    By LMZ in forum Windows Programming
    Replies: 2
    Last Post: 03-29-2007, 02:27 AM
  4. Play avi file.
    By barraluca73 in forum Windows Programming
    Replies: 5
    Last Post: 02-13-2006, 05:55 AM

Tags for this Thread