(我是python的新手)
我正在尝试运行一个关于pyaudio的简单代码。我只是复制并粘贴了我在pyaudio网站上找到的代码。
我收到此错误:
OSError Traceback (most recent call last)
<ipython-input-7-3fc52ceecbf3> in <module>()
15 channels=wf.getnchannels(),
16 rate=wf.getframerate(),
---> 17 output=True)
18
19 # read data
/home/gustavolg/anaconda3/lib/python3.5/site-packages/pyaudio.py in open(self, *args, **kwargs)
748 """
749
--> 750 stream = Stream(self, *args, **kwargs)
751 self._streams.add(stream)
752 return stream
/home/gustavolg/anaconda3/lib/python3.5/site-packages/pyaudio.py in __init__(self, PA_manager, rate, channels, format, input, output, input_device_index, output_device_index, frames_per_buffer, start, input_host_api_specific_stream_info, output_host_api_specific_stream_info, stream_callback)
439
440 # calling pa.open returns a stream object
--> 441 self._stream = pa.open(**arguments)
442
443 self._input_latency = self._stream.inputLatency
OSError: [Errno -9996] Invalid output device (no default output device)
我无法弄清楚如何解决这个错误。我不知道这是否与音频驱动程序有关,或者代码是否需要输出声明。我的意思是,如果我必须选择一个输出。
代码:
import pyaudio
import wave
import sys
CHUNK = 1024
wf = wave.open("/home/gustavolg/anaconda3/aPython/file.wav", 'rb')
# instantiate PyAudio (1)
p = pyaudio.PyAudio()
# open stream (2)
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
# read data
data = wf.readframes(CHUNK)
# play stream (3)
while len(data) > 0:
stream.write(data)
data = wf.readframes(CHUNK)
# stop stream (4)
stream.stop_stream()
stream.close()
# close PyAudio (5)
p.terminate()
我在Jupyter笔记本上使用python3。
答案 0 :(得分:1)
检查以下步骤:
>>> import pyaudio
>>> pa = pyaudio.PyAudio()
>>> pa.get_default_input_device_info()
{'defaultLowOutputLatency': 0.008707482993197279, 'maxOutputChannels': 32, 'hostApi': 0, 'defaultSampleRate': 44100.0, 'defaultHighOutputLatency': 0.034829931972789115, 'name': 'default', 'index': 15, 'maxInputChannels': 32, 'defaultHighInputLatency': 0.034829931972789115, 'defaultLowInputLatency': 0.008707482993197279, 'structVersion': 2}
>>> pyaudio.pa.__file__
'/root/.virtualenvs/py3k/lib/python3.4/site-packages/_portaudio.cpython-34m.so'
>>>
确保您有默认输入设备,否则可以refer to here
我希望它对你有用!