pygame.mixer.Sound()语句提示错误:pygame.error: Unable to open file 'test.wav'
環(huán)境:Win10、python3.6.5、pygame( version 1.9.3)
在使用pygame創(chuàng)建一個(gè)wav文件時(shí),代碼如下:
import pygame class Player:# constructordef __init__(self):pygame.mixer.pre_init(44100, -16, 1, 2048)pygame.init()# add a notedef add(self, fileName):self.notes[fileName] = pygame.mixer.Sound(fileName)在使用時(shí)提示如下錯(cuò)誤:
pygame.error: Unable to open file 'test.wav'
查閱資料:
pygame.mixer.Sound
Create a new Sound object from a file or buffer object
Note: The buffer will be copied internally, no data will be shared between it and the Sound object.
可知,用法應(yīng)該是沒有過時(shí)的,并且是正確的。
那么問題出在哪呢?查一下錯(cuò)誤提示,找到網(wǎng)頁(yè):
You cannot use pygames’ library’s unless you initialize either the modules your using or all of pygame.
這個(gè)說是沒有初始化,但我的代碼中將初始化放在__init__() 函數(shù)中了,不應(yīng)該提示錯(cuò)誤,然后還有的同學(xué)說是
It turned out that the frequency rate was too high for pygame, so I used one of the free tools to reduce the sampling rate (to smth like 128 bps) and it worked.
也就是說pygame支持不了那么高頻的數(shù)據(jù),然后我就將我的代碼中的頻率降為128,結(jié)果依然提示錯(cuò)誤。說明問題還不在這。那么問題出在哪呢?
繼續(xù)找,終于在這個(gè)網(wǎng)頁(yè)(https://blog.csdn.net/ltime/article/details/72473138)上找到了。
原因是我用的公司的臺(tái)式電腦,上面沒有音頻設(shè)備,硬件不支持,拿一個(gè)耳機(jī)連接上電腦,再次運(yùn)行,成功,但是和網(wǎng)頁(yè)上那位作者遇到的問題不一樣的是我的錯(cuò)誤始終提示的是
而那位作者提示的卻是
pygame.error: DirectSoundCreate: No audio device found這樣的錯(cuò)誤真是簡(jiǎn)單明了。
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的pygame.mixer.Sound()语句提示错误:pygame.error: Unable to open file 'test.wav'的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。