简体   繁体   中英

How to play sound one by one without waiting in while loop in python?

Like the following:(sounds are played if some condition is satisfied, so sounds are played with little interval and it will stop satisfying the condition after playing)

while True:
    ......
    if condition:
        play different mp3 files
        condition = False
    ......

I've tried: 1.using playsound(), but if block=True, it will cost time to finish playing and the program is frozen, else if block=False, all sounds are played at the same time so I can't hear then one by one. 2.using thread with playsound's block=True option. And all sounds still overlapped.

Any solutions? Thanks in advance!

You could use the VLC MediaPlayer library to play your sounds. Uou can use the is_playing() method to check whether the sound you want to play is still playing. If not, you could then start the next sound.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM