简体   繁体   English

pygame.mixer.Sound只会发出爆裂的声音并退出。 为什么?

[英]pygame.mixer.Sound only plays a popping noise and exits. Why?

I am trying to play sound using Python via pygame but all I hear is a popping noise and then the program quits. 我正在尝试通过pygame使用Python播放声音,但我听到的只是一声突然的声音,然后程序退出了。 The length of the track is at least 20 seconds long but my program quits almost immediately after the pop. 曲目的长度至少为20秒,但是我的程序在弹出后几乎立即退出。

I've tried solutions other than pygame and I hear popping on those as well. 除了pygame之外,我还尝试了其他解决方案,而且我也听到了弹出的声音。 Just not sure why it just pops and quits and it seems like it's related to my machine (Mac). 只是不确定为什么它会突然弹出并退出,并且似乎与我的机器(Mac)有关。

import pygame
pygame.init()
pygame.mixer.init()
sound = pygame.mixer.Sound("SampleAudio_0.4mb.wav")
sound.play()
while pygame.mixer.get_busy():
    pygame.time.delay(1000000)

I downloaded this wav file: http://www.anzwad.com/dods/sound/ambient/thunder2.wav 我下载了这个wav文件: http : //www.anzwad.com/dods/sound/ambient/thunder2.wav

I ran 我跑了

import pygame
pygame.init()
pygame.mixer.init()
sound = pygame.mixer.Sound("thunder2.wav")
sound.play()
print(pygame.mixer.get_busy())
while pygame.mixer.get_busy():
    print(pygame.mixer.get_busy())
    pygame.time.delay(1000)
print(pygame.mixer.get_busy())

It printed this: 它打印了这个:

pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
1
1
1
1
1
1
1
1
1
1
1
0

Does this work for you? 这对您有用吗? For me the 1's (the 1's mean that get_busy is returning true) are printed during the playing of the sound file then, right when the sound is complete and the delay finishes, the program closes. 对我来说,在播放声音文件期间会打印1(1表示get_busy返回true),然后在声音完成并且延迟结束后立即关闭程序。

Is that the functionality that you'd like? 那是您想要的功能吗?

Note my python version is 3.7.1 注意我的python版本是3.7.1

If this python code does not have the same functionality on your machine it may enable us to pinpoint the issue. 如果此python代码在您的计算机上没有相同的功能,则可能使我们能够查明问题。

If the sound file that you are using publicly available and you post a link, I can test my program with that file as well. 如果您正在使用的声音文件公开可用并且您发布了链接,那么我也可以使用该文件测试程序。

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

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