无法在 Pygame 中加载音乐 [英] Cannot load music in Pygame

查看:137
本文介绍了无法在 Pygame 中加载音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的代码是这样的:

导入pygame断言 pygame.init()==(6,0)pygame.mixer.init()从 pygame.locals 导入 *从时间导入睡眠导入操作系统pygame.mixer.music.load('timer_end.mp3')

不幸的是,我收到此错误:

回溯(最近一次调用最后一次):文件C:\Python37\Lib\timer.py",第 60 行,在 <module> 中pygame.mixer.music.load('timer_end.mp3')pygame.error: 无法打开timer_end.mp3"

我还查看了这个问题及其答案.不幸的是,它们没有帮助,因为 .mp3 一开始与我的 Python 脚本在同一个文件中.如何让 PyGame 音乐正常工作?

解决方案

mp3 无法识别

不确定这是否是问题,因为在我看来您使用的是 Windows,但请查看 pygame 音乐文档.他们说:

<块引用>

请注意 MP3 支持是有限的.在某些系统上,不受支持的格式可能会导致程序崩溃,例如Debian Linux.考虑改用 OGG.

因此尝试将文件转换为其他格式,例如 WAV 或 OGG.也许它有效.

使用完整路径

您发布的错误是 pygame 在找不到文件时给出的错误.所以也许@Matt 在他的评论中是对的.
尝试使用完整路径,并在 os.path 函数的帮助下构建它,它们比手动编写完整路径更安全且不易出错.
如果您确定音乐文件位于 python 脚本所在的同一目录中,则可以通过执行以下操作获取其完整路径:os.path.abspath("timer_end.mp3")

作为最后一个资源,以下代码将推断python文件的路径并假设它在同一目录中构建文件music的完整路径.即使您从另一个目录启动 python 脚本,它也应该可以工作.

filepath = os.path.abspath(__file__)filedir = os.path.dirname(filepath)音乐路径 = os.path.join(filedir, timer_end.mp3")pygame.mixer.music.load(音乐路径)

The code I am using is this:

import pygame
assert pygame.init()==(6,0)
pygame.mixer.init()
from pygame.locals import *
from time import sleep
import os
pygame.mixer.music.load('timer_end.mp3')

Unfortunately, I get this error:

Traceback (most recent call last):
  File "C:\Python37\Lib\timer.py", line 60, in <module>
    pygame.mixer.music.load('timer_end.mp3')
pygame.error: Couldn't open 'timer_end.mp3'

I have also looked at this question and its answer. They are, unfortunately, not helpful because the .mp3 was, to begin with, in the same file as my Python script. How can I get PyGame music to work correctly?

解决方案

mp3 not recognized

Not sure if this is the problem, since it seems to me you are using windows, but check the pygame music docs. They say:

Be aware that MP3 support is limited. On some systems an unsupported format can crash the program, e.g. Debian Linux. Consider using OGG instead.

So try to convert the file to another format, like WAV or OGG. Maybe it works.

use full path

The error you posted is the error pygame gives when the file is not found. So maybe @Matt in his comment is right.
Try to use the full path, and build it with the help of the os.path functions, they are safer and less error prone than writing the full path by hand.
If you are sure that the music file is in the same directory where is the python script, you can get its full path by doing: os.path.abspath("timer_end.mp3")

As last resource, the following code will extrapolate the path of the python file and build the full path of the file music assuming that it is in the same directory. It should work even if you launch the python script from another directory.

filepath = os.path.abspath(__file__)
filedir = os.path.dirname(filepath)
musicpath = os.path.join(filedir, "timer_end.mp3")
pygame.mixer.music.load(musicpath)

这篇关于无法在 Pygame 中加载音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆