我想使用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播放歌曲,请告诉我。
答案 0 :(得分:1)
您似乎忘记了主事件循环:
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()
cf。 https://nerdparadise.com/programming/pygame/part3
没有它,pygame开始播放并立即退出。
答案 1 :(得分:0)
也许您可以使用pyaudio lib。 我在github上使用python播放音频文件 源代码:
和格式传输音频/视频文件可以使用ffmepg