4

我在 2.7 中的 PyGame 混音器无法使用声音选项。我可以使用mixer.music,但不能使用mixer.sound,使用mixer.sound它会发出很小的滴答声然后停止。代码:

import pygame
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
song = pygame.mixer.Sound("song.mp3")
pygame.mixer.Sound.play(song)

没有错误,它只是不会播放,并且会发出很小的滴答声。在 Windows 7-x64 顺便说一句。

4

4 回答 4

9

通常,Pygame 不会播放 mp3 文件。您可以测试看看 .wav 和 .ogg 文件是否会首先播放,以确保您的代码是正确的(根据您粘贴的内容,它似乎是正确的)。我建议将您的 mp3 声音转换为 Pygame 的 ogg。

于 2013-09-15T23:28:40.387 回答
1

您刚刚创建了一个名为 song 的对象。

而不是“pygame.mixer.Sound.play(song)”试试这个:

歌曲播放()

于 2015-01-06T22:47:42.437 回答
0

Pygame does play mp3 files. I had the same problem but I found out the solution:

if you saved your mp3 file as 'filename.mp3', and you wrote down the .mp3 file extension yourself, then the filename in pygame's pygame.mixer.music.load() function must be written as 'filename.mp3.mp3', because python expects you to add the .mp3. Sometimes the .mp3 is already included in the filename if you manually saved it as that.

Therefore, try this: pygame.mixer.music.load('filename.mp3.mp3')

于 2014-07-21T03:29:28.483 回答
0

这很容易解决,因为您的歌曲文件应该作为音乐加载,而不是作为普通声音加载。因此,以下代码使其完美运行:

import pygame
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
pygame.mixer.music.load("song.mp3")
pygame.mixer.music.play()
于 2013-10-05T19:48:51.183 回答