'Audio working only when trying to kill program in python using pygame [duplicate]
I made a program just for fun and the audio didn't work, and when I tried to kill the program the audio suddenly started working and when I pressed cancel it stopped working again. I did this for a couple of times and figured out the audio works only during trying to kill the program, But why? Here is the code
import time
current_time = time.localtime()
hour = current_time.tm_hour
minute = current_time.tm_min
if (hour>5):
x=1
while x==1 :
from pygame import *
print("TIME TO WAKE UP!!!")
mixer.init()
mixer.music.load('Air-raid-siren.ogg')
mixer.music.play()
Solution 1:[1]
Your code doesn't seem to be apt. Firstly' you are checking if hour > 5, then defining a not required variable x for a while loop. Here's what you should do:
import time
from pygame import mixer
current_time = time.localtime()
hour = current_time.tm_hour
minute = current_time.tm_min
while hour>5:
print("TIME TO WAKE UP!!!")
mixer.init()
mixer.music.load(location_of_music)
mixer.music.play()
hour=5
What I've basically done is eliminating your if statement and variable x. I've define while > hour 5 and below the last line I've made hour = 5. This is becase, without that your code will be running forever and it is just a system lag that you get the output when you are killing the program. In reality, the output is there, but it is such a big spam that your device is unable to show it. That's why I suggest using an incrementation or totally removing out while loop cause it is not needed (you wouldn't want a spam). I don't even think pygame is needed to play a music file. Visit this link for a better understanding of playing sounds in pyhton
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | The Myth |
