简体   繁体   English

音频无法使用 Pygame 模块播放

[英]Audio not Playing with Pygame Module

I want to play audio with pygame module but it doesn't work for me.我想用 pygame 模块播放音频,但它对我不起作用。 When I run code below, it just gives me this message in less than a second without any error:当我运行下面的代码时,它会在不到一秒钟的时间内给我这条消息,没有任何错误:

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

Code:代码:

from pygame import mixer
mixer.init()
mixer.music.load("/home/hermes/Desktop/test/ring_tone.wav")
mixer.music.play()

I have used other famous library,playsound but it didn't help me with my problem.if you know any other library for playing songs in python please tell me.我使用过其他著名的库,playsound,但它没有帮助我解决我的问题。如果你知道任何其他在 python 中播放歌曲的库,请告诉我。

It looks like you forgot the main event loop:看起来您忘记了主事件循环:

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 https://nerdparadise.com/programming/pygame/part3

without it, pygame starts playing and immediately exits.没有它,pygame 开始播放并立即退出。

Maybe you can use pyaudio lib.也许你可以使用 pyaudio lib。 I play audio file with python on my github source code:我在我的github源代码上用python播放音频文件:

I using pyaudio paly wav file 我使用 pyaudio paly wav 文件

and format Transfer audio/vedio file can use ffmepg和格式传输音频/视频文件可以使用 ffmepg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM