I am making a timed beep test as a skit for an event with my cadet corps and I can’t get the timer to run at the same time as the beep test audio.
import os, playsound, multiprocessing, time
cwd = os.getcwd()
def beep_test():
audio= "".join((cwd+"\\BTsound.mp3"))
playsound.playsound(audio,True)
def multitasking():
sound_process = multiprocessing.Process(target=beep_test())
sound_process.start()
timer = 10
while timer > 0 and sound_process.is_alive():
time.sleep(1)
print(f"..{timer}")
timer -= 1
if timer == 0:
sound_process.terminate()
print("Time's up, runners must grab a water bottle and walk around. Do not sit down.")
if __name__ == "__main__":
message= input("Type 'begin beep test' to launch the timed beep test.\n")
if "begin beep test" in message:
multitasking()
Specifics:
I use playsound 1.2.2, because 1.3.0 is bugged
It’s designed to be used with a speech recognition feature, but for simplicity’s sake, I replaced it with input()
On prod, the timer will be set at 600s, but for efficiency’s sake, I changed it to 10 during testing
I haven’t tried much, but I did move around the ‘if name == "main":’ hoping that it would play both the sound and timer, but to no avail.
I have troubleshot the timer, and it works alone
I’ve removed the ‘if "begin beep test" in message:’ and played the process alone, it works.
The problem seems to be in the first if, which I depend on to activate it, as I have other functionalities on the main program (I.E music).
>Solution :
The target argument needs to take a function name, not call the function.
What’s happening at the minute is that you are calling beep_test yourself, rather than letting the other process handle it.
Change it to multiprocessing.Process(target=beep_test).