如何在Python中播放背景音乐并执行其他功能?

问题描述 投票:0回答:1

我正在使用playsound模块来播放background_loop.mp3文件,但我无法调用其他函数或运行脚本的其他部分,直到它完成播放。

有没有一种方法可以在音乐播放时调用函数并运行脚本?

python
1个回答
1
投票

您可以使用线程来实现这一点。

from threading import Thread

# Create a new thread
# The target is the function to run
# You can also pass arguments to the function
t = Thread(target=playsound, args=["background_loop.mp3"])

# Start the thread
t.start()

# You can run more code here, that will keep going even while the background music is playing.
© www.soinside.com 2019 - 2024. All rights reserved.