0

I want a Python script that I can run in the background on my Windows machine which plays a sound file.

I have two different scripts which only work when I copy and paste the code into the Python interpreter. The sound will play successfully.

If instead I go to cmd and type python run.py, the script will run with the same standard out, but there will be no sound played.

# can only hear sound when this code is run in interpreter
from pygame import mixer
mixer.init()
mixer.music.load("C:\\Sound.mp3")
mixer.music.play()
# can only hear sound when this code is run in interpreter
import winsound
winsound.PlaySound("C:\\Sound.wav", winsound.SND_ASYNC)

How to resolve this?

Jase
  • 933
  • 1
  • 7
  • 25
  • 1
    You will have to wait for the sound to finish playing before exiting the application – Rabbid76 Feb 04 '21 at 22:31
  • @Rabbid76 What do you mean? The sound takes 1 second to play. When I type `python run.py` the sound should play immediately but it doesn't play. I can only hear the sound if I copy & paste the code into my Python interpreter. – Jase Feb 04 '21 at 22:37
  • 1
    It plays immediately, however `mixer.music.play()` doesn't wait till it is finished. Hence the application terminates before the sound has been played – Rabbid76 Feb 04 '21 at 22:38
  • 1
    @Rabbid76 You're right. Thank you. – Jase Feb 04 '21 at 22:39

0 Answers0