简体   繁体   English

pygame.mixer.music 播放音频很好,但 pygame.mixer.Sound 给出错误“无法打开文件”

[英]pygame.mixer.music plays audio fine but pygame.mixer.Sound gives error 'Unable to open file'

I have a simple code in which i want to play an audio file using pygame's mixer class.我有一个简单的代码,我想在其中使用 pygame 的混音器类播放音频文件。 When I do:当我做:

import pygame

dir_path = os.path.dirname(os.path.realpath(__file__))
path = os.path.join(dir_path, "mp3s", 'info.mp3')

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

it works just fine.它工作得很好。 But when I try the same with:但是当我尝试使用以下方法时:

import pygame

dir_path = os.path.dirname(os.path.realpath(__file__))
path = os.path.join(dir_path, "mp3s", 'info.mp3')

pygame.mixer.init()
mysound = pygame.mixer.Sound(file=path)
pygame.mixer.Sound.play(mysound)

It gives error Unable to open file '/home/pi/myproject/mp3s/info.mp3'它给出错误Unable to open file '/home/pi/myproject/mp3s/info.mp3'

I've tried googling and only thing I could find was either to try pygame.init() or pygame.display.set_mode((400, 300)) neither of which worked for me... Any ideas on what might be wrong?我试过谷歌搜索,唯一能找到的就是尝试pygame.init()pygame.display.set_mode((400, 300))这两个都不适合我......关于可能出什么问题的任何想法? I'm using Raspberry Pi with Raspbian installed and python 3.7 with pygame 1.9.6我正在使用安装了 Raspbian 的 Raspberry Pi 和带有 pygame 1.9.6 的 python 3.7

You can't use the Sound class for mp3 playback, as it only supports ogg and wav .您不能将Sound类用于mp3播放,因为它仅支持oggwav

See the docs of pygame.mixer.Sound :请参阅pygame.mixer.Sound文档

The Sound can be loaded from an OGG audio file or from an uncompressed WAV.声音可以从 OGG 音频文件或未压缩的 WAV 加载。

The only way to play a mp3 file with pygame is to use pygame.mixer.music .使用 pygame 播放mp3文件的唯一方法是使用pygame.mixer.music

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

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