我对python很陌生,我对此进行了一些研究,但我发现的答案似乎不起作用。我试图用带有绝对路径的pyglet加载一个mp3文件,但它给出了这个错误:
Traceback (most recent call last):
File "C:\Apps\Python\lib\site-packages\pyglet\resource.py", line 624, in media
location = self._index[name]
KeyError: 'C:/Users/Ruurd/Google Drive/Script/Python/music2.mp3'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/Ruurd/Desktop/playmus.py", line 3, in <module>
music=pyglet.resource.media(path)
File "C:\Apps\Python\lib\site-packages\pyglet\resource.py", line 634, in media
raise ResourceNotFoundException(name)
pyglet.resource.ResourceNotFoundException: Resource "C:/Users/Ruurd/Google Drive/Script/Python/music2.mp3" was not found on the path. Ensure that the filename has the correct captialisation.
当我使用&#39; music2.mp3&#39;作为一条路径,它工作正常。这是无效代码
import pyglet
path='C:/Users/Ruurd/Google Drive/Script/Python/music2.mp3'
music=pyglet.resource.media(path)
music.play()
答案 0 :(得分:2)
尝试使用:
music=pyglet.media.load(path)
而不是:
music=pyglet.resource.media(path)
为我工作。 =)
答案 1 :(得分:0)
import pyglet
from os.path import abspath, isfile
pyglet.options['audio'] = ('directsound', 'alsa', 'openal', 'silent')
path = abspath('C:/Users/Ruurd/Google Drive/Script/Python/music2.mp3')
if isfile(path):
music = pyglet.resource.media(path)
music.play()
由于你不能在任何地方找到你的mp3,我无法对此进行测试
但使用abspath
来确保语法正确并使用isfile
来确定该路径上是否存在该文件通常是个好主意。
要更好地控制所使用的驱动程序,可以使用pyglet.options['audio']
来指定pyglet,例如directsound
。