PyAudio 尝试播放 wav 文件时'unknown format: %r'错误



我正在尝试运行以下代码(取自如何使用pyaudio播放audifile ?):

import pyaudio
import wave
import sys
class AudioFile:
chunk = 1024
def __init__(self, file):
""" Init audio stream """ 
self.wf = wave.open(file, 'rb')
self.p = pyaudio.PyAudio()
self.stream = self.p.open(
format = self.p.get_format_from_width(self.wf.getsampwidth()),
channels = self.wf.getnchannels(),
rate = self.wf.getframerate(),
output = True
)
def play(self):
""" Play entire file """
data = self.wf.readframes(self.chunk)
while data != '':
self.stream.write(data)
data = self.wf.readframes(self.chunk)
def close(self):
""" Graceful shutdown """ 
self.stream.close()
self.wf.close()
self.p.terminate()
# Usage example for pyaudio
a = AudioFile("Filename.wav")
a.play()
a.close()

但是我一直得到以下错误:

Traceback (most recent call last):
File "c:UserstanguOneDriveBureauPython 101Python.py", line 33, in <module>
a = AudioFile("DP.wav")
File "c:UserstanguOneDriveBureauPython 101Python.py", line 10, in __init__
self.wf = wave.open(file, 'rb')
File "C:Userstanguanaconda3libwave.py", line 510, in open
return Wave_read(f)
File "C:Userstanguanaconda3libwave.py", line 164, in __init__
self.initfp(f)
File "C:Userstanguanaconda3libwave.py", line 144, in initfp
self._read_fmt_chunk(chunk)
File "C:Userstanguanaconda3libwave.py", line 269, in _read_fmt_chunk
raise Error('unknown format: %r' % (wFormatTag,))
wave.Error: unknown format: 3

有人知道如何解决这个问题吗?

这是因为我使用32位WAV文件…一旦我将它们更改为16位,我就没有问题了。

相关内容

  • 没有找到相关文章