如何在Python脚本的背景中播放音频(playsound)? [英] How do play audio (playsound) in background of Python script?
问题描述
我只是在写一个小型的python游戏,很有趣,我有一个函数可以开始叙述.
I am just writing a small python game for fun and I have a function that does the beginning narrative.
我正在尝试让音频在后台播放,但是很遗憾,在该功能继续之前,首先播放mp3文件.
I am trying to get the audio to play in the background but unfortunately the mp3 file plays first before the function continues.
我如何使其在后台运行?
How do I get it to run in the background?
import playsound
def displayIntro():
playsound.playsound('storm.mp3',True)
print('')
print('')
print_slow('The year is 1845, you have just arrived home...')
还有,有什么方法可以控制播放声音模块的音量?
Also, is there any way of controlling the volume of the play sound module?
我应该补充一点,我使用的是Mac,但我不喜欢使用playsound,它似乎是我可以使用的唯一模块.
I should add that I am using a Mac, and I am not wedded to using playsound, it just seems to be the only module that I can get working.
推荐答案
在Windows中:
使用 winsound.SND_ASYNC 异步播放
import winsound
winsound.PlaySound("filename", winsound.SND_ASYNC | winsound.SND_ALIAS )
停止播放
winsound.PlaySound(None, winsound.SND_ASYNC)
在Mac或其他平台上: 您可以尝试 Pygame/SDL
In mac or other platforms: You can try this Pygame/SDL
pygame.mixer.init()
pygame.mixer.music.load("file.mp3")
pygame.mixer.music.play()
这篇关于如何在Python脚本的背景中播放音频(playsound)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!