Python winsound, ASYNC flag not working?

Viewed 3617

I'm using python 3.5 and I'm trying to play a sound while continuing right away with my script; according to https://docs.python.org/3.5/library/winsound.html the flag "winsound.SND_ASYNC" should be the way to go. However the following doesn't produce any sound:

import winsound
winsound.PlaySound('C:/Users/Bob/Sounds/sound.wav', winsound.SND_ASYNC)

Interesting enough if I change the flag to "winsound.SND_FILENAME the sound is played:

import winsound
winsound.PlaySound('C:/Users/Bob/Sounds/sound.wav', winsound.SND_FILENAME)

Any ideas why the async flag doesn't work?

2 Answers

Just in case somebody will google this with reason, that particularly winsound.SND_ASYNC flag is not working in combination with winsound.SND_FILENAME | winsound.SND_ASYNC, and everything working without it, just for winsound.SND_FILENAME - sometimes the problem is, when calling winsound at the end of your script, it will not be play with async flag, cause script already stopped, so it stops anything else with async flag. Check this out:

import winsound
import time


def wavsound(asynced=True):
    flags = winsound.SND_FILENAME | winsound.SND_ASYNC if asynced else winsound.SND_FILENAME
    winsound.PlaySound(r"path_to_your_wav_file", flags)


# it would or would not play at the end of your python script if:
wavsound(asynced=True)  # play if this line uncommented and wavsound(asynced=False) commented
# wavsound(asynced=False) # not play if this line uncommented and wavsound(asynced=True) commented

# time.sleep(2) # if uncomment this - play first 2 seconds and then cutoff with asynced=True
Related