0

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()
R550MAGIC
  • 1
  • 1

1 Answers1

0

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

The Myth
  • 1,090
  • 1
  • 4
  • 16
  • thank you very much for your answer, it helped me a lot. I made the loop go forever because I would like to leave it open and use it as a alarm clock( though very unpractical) – R550MAGIC Mar 15 '22 at 03:19