This is because the playsound function has a "block" argument, that blocks executing until the sound has completed execution. By default, this argument is "True".
Change it to "False", and you're good to go:
from playsound import playsound
def play_sound(morse_code: list):
print(morse_code)
for code in morse_code:
print(code)
for char in code:
if char == '-':
playsound('sound/morse_line.mp3', block=False)
elif char == '.':
playsound('sound/morse_dot.mp3', block=False)
elif char == '|':
continue
time.sleep(0.05)
time.sleep(1)
You may, however, want to print and play the sound at the same time, in which case just iterate over each letter of the string, print it individually and play the sound as well, instead of printing it all together at the start of the function.
Source: Playsound Documentation at https://pypi.org/project/playsound/
Relevant part: The playsound module contains only one thing - the function (also named) playsound.
It requires one argument - the path to the file with the sound you’d like to play. This may be a local file, or a URL.
There’s an optional second argument, block, which is set to True by default. Setting it to False makes the function run asynchronously.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…