pygame.mixer.Sound()。play()没有给出错误,但实际上没有播放

时间:2020-05-11 19:24:01

标签: python-3.x pygame python-3.6

我正在尝试使用pygame播放.wav文件

import pygame
pygame.mixer.init()
s = pygame.mixer.Sound('absolute path to file')
s.play()

它没有错误,但似乎没有任何作用。
这是完整的输出

pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html

在conda python 3.6环境中通过pip安装了pygame。具有macOS Mojave的macbook pro 2019

2 个答案:

答案 0 :(得分:2)

正如bashBediam所说的那样,问题在于代码结束了,没有给音频播放时间。我这样修复。

import pygame
pygame.mixer.init()
s = pygame.mixer.Sound(path)
channel = s.play()
while channel.get_busy():
     pygame.time.wait(100)

答案 1 :(得分:1)

在末尾添加一行:

,您会听到。您的脚本在有机会播放声音之前就结束了。