繁体   English   中英

pygame 混合器 pygame 错误:无法打开文件 'file_location.mp3'

[英]pygame mixer pygame error: Unable to open file 'file_location.mp3'

我想使用 pygame.mixer 模块在 python 脚本中播放我的 mp3 文件。 我有一个如下所示的脚本:

import pygame
from time import sleep
import os

# print(pygame.version.ver)

pygame.init()
pygame.mixer.init()

my_sound = pygame.mixer.Sound(os.path.join(os.path.dirname(os.path.abspath(__file__)),"sounds","my_sound.mp3"))
while True:
    my_sound.play()
    sleep(0.5)

使用 powershell 和 python 3.10 在我的 windows10 计算机上运行它会导致错误

pygame.error: Unable to open file 'C:\\path\\to\\my\\file\\sounds\\my_sound.mp3'

我已尝试安装所有最新的 >2.0.0 版本,但错误仍然存​​在。 是否需要安装任何类型的软件驱动器才能使其正常工作? 在我的旧计算机上,像这样使用 pygame.mixer 没有问题,而无需在我的脚本中进行某种额外的初始化。

使用任何类型的在线工具将 .mp3 文件转换为 .wav 文件,然后它就可以工作了。 PyGame 对音频中的文件类型非常挑剔。

我也有这个问题,并做了一个功能来帮助它。 这可能看起来不专业,但它会起作用。

def backslashChange(text):
    ans = ''
    temp = text.split("\\\\")
    for items in temp:
        if items == 'C:':
            ans += 'C:\\\\'
        else:
            ans += items 
            ans +='\\'
    ans = ans [:-1]
    return ans

在找到更专业的解决方案之前,我一直在使用它。 所以把你的代码改成这个

def backslashChange(text):
    ans = ''
    temp = text.split("\\\\")
    for items in temp:
        if items == 'C:':
            ans += 'C:\\\\'
        else:
            ans += items 
            ans +='\\'
    ans = ans [:-1]
    return ans
import pygame
from time import sleep
import os

# print(pygame.version.ver)

pygame.init()
pygame.mixer.init()

my_sound = pygame.mixer.Sound(backslashChange(os.path.join(os.path.dirname(os.path.abspath(__file__)),"sounds","my_sound.mp3")))
while True:
    my_sound.play()
    sleep(0.5)

暂无
暂无

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

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