0

I've copied and pasted some example code to play a wav file using pyaudio, but I get the error: IOError: [Errno Invalid output device (no default output device)] -9996.

Here's the code:

import pyaudio
import wave
import sys

CHUNK = 1024

if len(sys.argv) < 2:
    print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
    sys.exit(-1)

wf = wave.open(sys.argv[1], 'rb')

p = pyaudio.PyAudio()

stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                channels=wf.getnchannels(),
                rate=wf.getframerate(),
                output=True)

data = wf.readframes(CHUNK)

while data != '':
    stream.write(data)
    data = wf.readframes(CHUNK)

stream.stop_stream()
stream.close()

p.terminate()

To run the program I open up terminal and just type python playwavexample mywavfile.wav.

I thought this may of been a permission issue so I tried throwing a sudo in there but it didn't do anything. I'm running Ubuntu 16.04, and audio output seems to be working fine...

Null Salad
  • 765
  • 2
  • 16
  • 31

1 Answers1

-1

It seems PyAudio can't find your default device. One solution would be to add the parameter output_device_index to the initialization of your stream variable.

For example, with a device index of 0

stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                channels=wf.getnchannels(), 
                rate=wf.getframerate(), 
                output=True,
                output_device_index=0)

You can find the device indices for all of the devices accessible (input and output) to PyAudio via the following code.

import pyaudio

p = pyaudio.PyAudio()
device_count = p.get_device_count()
for i in range(0, device_count):
    print("Name: " + p.get_device_info_by_index(i)["name"])
    print("Index: " + p.get_device_info_by_index(i)["index"])
    print("\n")
Jonas
  • 1
  • 2