Currently doing some experimenting with PyAudio. My current goal is to import a wav file and get PyAudio to play it for me. Playing it is actually the easy part - the hard part is getting the stream to close once the wav file is complete. Right now my code never exits the while loop. I hear the wav file and then silence as the code continues to write nothing to the stream.
import wave
import pyaudio
wav = wave.open('./5secondbeat.wav')
p = pyaudio.PyAudio()
chunk = 1024
stream = p.open(format = p.get_format_from_width(wav.getsampwidth()),
channels = wav.getnchannels(),
rate = wav.getframerate(),
frames_per_buffer = chunk,
output = True)
data = wav.readframes(chunk)
while data != '': //enters this loop
stream.write(data) //I hear my short 5 second wave file
data = wav.readframes(chunk)
print('hello') //this never gets printed and my code continues running in the loop
stream.close()
p.terminate()
What am I missing?
The data types in your conditional don't correspond. You could try one of these to cast them to the same type before checking:
while data != b'':
or
while data.decode("utf-8") != '':