Home  >  Q&A  >  body text

python - pyaudio stream.close() causes the program to crash and the exception cannot be caught

I want to use pyaudio frequently to play audio. Although it is not a multi-threaded module, it only uses one sub-thread to play. There should be no problem with the main thread controlling the playback switch. But when I used it to play, I ran into trouble that I couldn't solve.

import threading
import wave
import pyaudio
import time
pa=pyaudio.PyAudio()
f=wave.open('/home/dyan/catkin_ws/src/hlf_robot/scripts/hlf_voice/my.wav','rb')
raw_data=f.readframes(f.getnframes())
f.close()
samplewidth,channels,framerate,raw_data=2,1,16000,raw_data

i=0
stream1=''
def test():
    while True:
        global i,pa,stream1
        try:
            print i
            stream1=pa.open(format=pa.get_format_from_width(samplewidth),
                            channels=channels,
                            rate=framerate,
                            output=True)
            stream1.write(raw_data)
    
            i+=1
            print i
        except IOError,e:
                print e
        except Exception,e:
                print e
                break
t0=threading.Thread(target=test)
t0.start()
time.sleep(3)
while True:
    if stream1.is_active():
        print "is_active"
    else:
        print "not active"
    if stream1.is_stopped():
        print "is_stopped"
    else:
        print "not stopped"
    print stream1._is_running
    print stream1._stream
    print '123'
    try:
        stream1.stop_stream()
        print '456'
        stream1.close()
    except Exception,e:
        print e
        break
    print '789'
    time.sleep(3)

The output is like this, and then it crashes

0                                                #子线程循环第一次
is_active                                        #
not stopped                                      #
True                                             #stream1._is_running
<_portaudio.Stream object at 0x7efd71e00cb0>     #stream1._stream
123                                              #调用stop_stream()之前
456                                              #调用stop_stream()之后,close()之前
[Errno Unanticipated host error] -9999           #子线程捕获到主线程调用stream1.stop_stream()时由stream1.write()抛出的异常IOError,忽略这个异常继续执行
0                                                #子线程stream1.write()抛出异常i+=1未执行继续循环在pa.open()之前的print

1. In other words, calling stream1.close() after calling stream1.stop_stream() caused the program to crash, and neither the child thread nor the main thread caught the exception! ! ! !
2. If you comment out the close() after stop_stream(), there will be no problem for a short time. However, if it continues to run for about 10 minutes, it will not crash but will no longer be able to continue playing. pa.open() keeps throwing an exception [Errno Illegal combination of I/O devices] -9993. When I close the program and restart it, pa.open() will throw an exception ('Invalid sample rate', -9997). For playback No more! ! !

How to solve this problem?


Temporarily use the method of reopening a pyaudio.Pyaudio() object for each playback, which uses about 10ms of additional CPU time. I ran it continuously for nearly 20 hours without any problems.

...
try:
    self.pa=pyaudio.PyAudio()
                    
    self.stream=self.pa.open(format=self.pa.get_format_from_width(samplewidth),
                    channels=channels,
                    rate=framerate,
                    output=True)
    self.stream.write(raw_data)
except IOError,e:
    pass
finally:
    self.stream.close()
    self.pa.terminate()
...
我想大声告诉你我想大声告诉你2686 days ago1177

reply all(1)I'll reply

  • 阿神

    阿神2017-06-12 09:27:06

    Here is a possible workaround, ghost commented on 30 Jul 2016

    reply
    0
  • Cancelreply