Pygame:无法打开.mp3文件 [英] Pygame: Unable to open .mp3 file

查看:273
本文介绍了Pygame:无法打开.mp3文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以...每当我尝试运行此代码块时:

So... Whenever I try to run this block of code:

import pygame, sys, time
from pygame.locals import *

pygame.init()

DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Memes.')

meme = pygame.mixer.Sound('JUST DO IT.mp3')
meme.play()
time.sleep(2)
meme.stop()

while True: # Main Loop
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()

我收到此错误:

Traceback (most recent call last):
  File "C:\Users\Slay-Slay\Desktop\Python\Python Code\play soun.py", line 9, in <module>
    meme = pygame.mixer.Sound('JUST DO IT.mp3')
pygame.error: Unable to open file 'JUST DO IT.mp3'

在您询问之前,代码和声音都在相同的文件夹中.我不知道这可能是什么问题...我认为pygame支持.mp3吗?

Both the code and the sound are in the same exact folder, before you ask. I don't know what the problem could be... I thought pygame supported .mp3?

我只是用pygame.mixer.music.load()函数尝试了一下……而且那也不起作用.相反,它给了我这个错误:

I just tried it with the pygame.mixer.music.load() function... And that didn't work either. It instead gave me this error:

Traceback (most recent call last):
  File "C:\Users\Slay-Slay\Desktop\Python\Python Code\play soun.py", line 9, in <module>
    pygame.mixer.music.load('JUST DO IT.mp3')
pygame.error: Couldn't read from 'JUST DO IT.mp3'

我还尝试了不同的格式,例如WAV或OGG.两者都不起作用.我在2种不同的功能上尝试了所有3种格式.全部返回相同的错误.对于Music.load,无法读取",对于声音,无法打开文件".

I also tried different formats, such as WAV or OGG. Neither worked. I tried all 3 formats on the 2 different functions. All returned the same errors. "Couldn't read from" for music.load, and "Unable to open file" for Sound.

推荐答案

还有另一个混合器方法music,您可能应该使用-music支持mp3,但sound不支持.试试这个.

There's another mixer method, music, that you should probably be using - music supports mp3, but sound doesn't. Try this.

pygame.mixer.music.load('JUST DO IT.mp3')
pygame.mixer.music.play()
time.sleep(2)
pygame.mixer.music.stop()

^顺便说一句,这是一个很可爱的模因

^that's a pretty dank meme by the way

这篇关于Pygame:无法打开.mp3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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