python - 无法在 pygame.mixer 中打开任何声音文件

标签 python pygame

我一直在使用 pygame.mixer 开发一个与声音相关的 Pygame 项目,但我遇到了一个似乎无法解决的问题。我无法打开我尝试过的任何声音文件(.mp3 和 .midi)。

我在 PyCharm 2018.3 上使用 Python 3。我的 Pygame 大部分是最新的(版本 1.9.3)。我尝试过使用完整路径,我已经完成了 pygame.init()mixer.init(),但我完全卡住了。

这是我的代码:

import pygame
from pygame import mixer

pygame.init()
mixer.init(44100, 16, 2, 4096)

f = mixer.Sound("output.midi")
f.play()

print(bool(mixer.get_busy()))
while mixer.get_busy():
    pass

这是错误(文件中的 ... 覆盖了实际的回溯):

Traceback (most recent call last):
  File "/home/.../note.py", line 27, in <module>
    f = mixer.Sound("output.midi")
pygame.error: Unable to open file 'output.midi'

该程序应该打开在程序的另一部分(我已注释掉)中创建的.midi 文件并播放它直到完成。相反,我只是收到错误并且没有声音播放。

最佳答案

MIDI 文件不是“声音文件”;它是“声音文件”。它基本上是数字乐谱。您需要一个 MIDI 合成器来从中生成声音。

并且MP3是用算法压缩的;它不是声音样本序列。

来自the PyGame.Mixer documentation :

The Sound can be loaded from an OGG audio file or from an uncompressed WAV.

简而言之,您使用了错误的工具来完成这项工作。

关于python - 无法在 pygame.mixer 中打开任何声音文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54165678/

相关文章:

Python 列表反转:[::-1]?

python - nx.draw() 将边绘制为双向,即使它们是单向的

python - 调试缓慢的 Django 管理 View

python - Pygame:当 Sprite 遇到它的踪迹时如何 "lose"游戏

Python/Pygame 使用类/函数创建开始屏幕

python - 通过单击面向对象的 PyGame 按钮来调用 PyGame 函数

python - 如何获取所有文档类型的列表

python - 相当于numpy的cv::MatIterator

python - MPI 意外执行

python - 当前房间内 Sprite 的碰撞检测