简体   繁体   English

Pygame Mixer 不播放音频

[英]Pygame Mixer not playing audio

I've been working on this for a while but everything I find seems to be a dead end so I could use some help troubleshooting.我已经为此工作了一段时间,但我发现的一切似乎都是死胡同,所以我可以使用一些帮助进行故障排除。

import pygame

file = open("C:\\Users\\MyName\\Music\\oggTest.ogg")

pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(file)
pygame.mixer.music.play()

while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)

This code is supposed to grab an audio file and play it using the mixer from pygame.这段代码应该抓取一个音频文件并使用 pygame 的混音器播放它。 Very simple, and I've seen other questions here asking about how to do it but mine's still not working for some reason.非常简单,我在这里看到了其他问题,询问如何做,但由于某种原因我的仍然无法正常工作。 Right now, I have no errors when I'm running the code.现在,我在运行代码时没有错误。 The only problem is that nothing happens.唯一的问题是什么也没有发生。 No audio gets played.没有音频播放。 I have a hunch that it has something to do with my IDE (pycharm), but I don't know for sure if that's the case.我有一种预感,它与我的 IDE (pycharm) 有关,但我不确定是否是这种情况。 Also, the filepath part is a bit confusing to me.另外,文件路径部分对我来说有点混乱。 I don't see any reason why it wouldn't work but perhaps I'm missing something obvious.我看不出有什么理由让它不起作用,但也许我遗漏了一些明显的东西。 Thanks in advance for helping out.提前感谢您的帮助。

As per the advice of a YT video, I converted the mp3 file I originally had to an ogg file.根据 YT 视频的建议,我将最初拥有的 mp3 文件转换为 ogg 文件。 Not sure if that was necessary, and honestly I'd prefer not to have to do it in the future.不确定这是否有必要,老实说,我希望将来不必这样做。

Okay, I found the answer after a bit more research.好的,我经过一番研究后找到了答案。 Taking out the pygame.init() worked.取出 pygame.init() 工作。 I guess it didn't like doing both pygame.init() and pygame.mixer.init() for some reason.我想由于某种原因它不喜欢同时执行 pygame.init() 和 pygame.mixer.init() 。

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

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