Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

playsound - Python sound plays before rest of the code runs

I am trying to write a program that plays the Morse code sound while showing the Morse code.

The problem that I have is sound plays before showing the Morse code in the program.The program has a UI based on tkinter

   for item in self.morse_code:
        self.output_text_area.config(state="normal")
        self.output_text_area.insert(END, item)
        self.output_text_area.config(state="disable")
    play_sound(self.morse_code)

I am using the playsound library and below is the the function in charge of playing the sound:

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')
        elif char == '.':
            playsound('sound/morse_dot.mp3')
        elif char == '|':
            continue
        time.sleep(0.05)
    time.sleep(1)

How can I get the program to show the Morse Code first , then play Morse Code sound ? Currently even though the code for updating the text_area executes first , the sound plays first then after it is done it will show the Morse Code.

question from:https://stackoverflow.com/questions/65641452/python-sound-plays-before-rest-of-the-code-runs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...