调用声音函数时会播放每种声音,但我无法使其起作用
答案 0 :(得分:1)
pygame.mixer.music一次只能加载一个文件。
您可能想创建一个声音对象:
#Note only wav and ogg will be supported
snd_left = pygame.mixer.Sound('/media/pi/1278-1B75/Audio/left.wav')
然后玩
snd_left.play()
此外,您应该在启动时加载声音文件,并且仅加载一次,
pygame.init()/ pygame.mixer.init()也是如此
尽管多次初始化不会导致错误。
所以结果将是这样
pygame.init()
pygame.mixer.init()
#Load sound files after mixer initalized.
sound()
def update_display():
global sensors
global snd_left #You may want to load more files here
#Read the sensors
sensors = sensorVals()
if sensors:
if detect_something():
snd_left.play()
def sound():
global snd_left #remember to use global
snd_left = pygame.mixer.Sound('/media/pi/1278-1B75/Audio/left.wav')