我想用 pygame 模块播放音频,但它对我不起作用。当我运行下面的代码时,它在不到一秒的时间内没有任何错误地给我这条消息:
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
代码:
from pygame import mixer
mixer.init()
mixer.music.load("/home/hermes/Desktop/test/ring_tone.wav")
mixer.music.play()
我使用过其他著名的库,playsound,但它并没有帮助我解决我的问题。如果你知道任何其他用于在 python 中播放歌曲的库,请告诉我。
最佳答案
看起来你忘记了主事件循环:
import pygame
import sys
pygame.init() #<-- initializes video
pygame.mixer.init()
pygame.mixer.music.load("/home/hermes/Desktop/test/ring_tone.wav")
pygame.mixer.music.play()
SONG_END = pygame.USEREVENT + 1
pygame.mixer.music.set_endevent(SONG_END)
while True:
for event in pygame.event.get():
if event.type == SONG_END:
print("the song ended!")
pygame.quit()
sys.exit()
比照。 https://nerdparadise.com/programming/pygame/part3
没有它,pygame 开始播放并立即退出。
关于python - Pygame 模块不播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57204664/