I can't run a simple code using pyaudio - [Errno -9996] Invalid output device (no default output device)

喜你入骨 提交于 2020-02-25 03:56:20

问题


(I'm new at python)

I'm trying to run a simple code about pyaudio. I just copied and pasted a code that I found on the pyaudio web site.

I get this error:

    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)

I can not figure out how to solve this error. I don't know if this has something to do with audio driver or if the code needs an output declaration. I mean, if I have to select an output.

The code:

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()

I'm using python3 on Jupyter notebook.


回答1:


check the following steps:

>>> 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'
>>>

make sure you have a default input device,if not you can refer to here

I want it's useful for you!



来源:https://stackoverflow.com/questions/37063540/i-cant-run-a-simple-code-using-pyaudio-errno-9996-invalid-output-device-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!