pygame.mixer.Sound()语句提示错误:pygame.error: Unable to open file 'test.wav'
環境:Win10、python3.6.5、pygame( version 1.9.3)
在使用pygame創建一個wav文件時,代碼如下:
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)在使用時提示如下錯誤:
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.
可知,用法應該是沒有過時的,并且是正確的。
那么問題出在哪呢?查一下錯誤提示,找到網頁:
You cannot use pygames’ library’s unless you initialize either the modules your using or all of pygame.
這個說是沒有初始化,但我的代碼中將初始化放在__init__() 函數中了,不應該提示錯誤,然后還有的同學說是
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支持不了那么高頻的數據,然后我就將我的代碼中的頻率降為128,結果依然提示錯誤。說明問題還不在這。那么問題出在哪呢?
繼續找,終于在這個網頁(https://blog.csdn.net/ltime/article/details/72473138)上找到了。
原因是我用的公司的臺式電腦,上面沒有音頻設備,硬件不支持,拿一個耳機連接上電腦,再次運行,成功,但是和網頁上那位作者遇到的問題不一樣的是我的錯誤始終提示的是
而那位作者提示的卻是
pygame.error: DirectSoundCreate: No audio device found這樣的錯誤真是簡單明了。
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的pygame.mixer.Sound()语句提示错误:pygame.error: Unable to open file 'test.wav'的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对标记点进行三角化
- 下一篇: python3环境下用matplotli