簡體   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