亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術文章
文章詳情頁

python - pyaudio stream.close()導致程序崩潰且不能捕獲到異常

瀏覽:151日期:2022-06-29 11:05:15

問題描述

我想要頻繁地使用pyaudio來播放音頻,雖然他不是多線程模塊,但是只使用一個子線程播放,主線程控制播放的開關應該是沒有問題的。但是當我使用它播放時遇到了不能解決的麻煩。

import threadingimport waveimport pyaudioimport timepa=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_datai=0stream1=’’def test(): while True:global i,pa,stream1try: 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 iexcept IOError,e:print eexcept Exception,e:print ebreakt0=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 ebreak print ’789’ time.sleep(3)

輸出是這樣的,然后崩了

0#子線程循環第一次is_active#not stopped #True #stream1._is_running<_portaudio.Stream object at 0x7efd71e00cb0> #stream1._stream123 #調用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、也就是說在調用stream1.stop_stream()后調用stream1.close()導致程序崩潰了,而且子線程和主線程都沒有捕獲到異常!!!!2、如果將stop_stream()后的close()注釋掉短時間內沒有任何問題,但是持續運行大約10分鐘左右雖然不會崩潰但是再也不能繼續播放。pa.open()一直拋出異常[Errno Illegal combination of I/O devices] -9993,當我關閉程序重啟時pa.open()會拋出異常(’Invalid sample rate’, -9997),播放用不了了!!!

這個問題該如何解決?

暫時使用每次播放均重開一個pyaudio.Pyaudio()對象的方式解決,這大概額外使用了cpu時間10ms。持續跑了將近20個小時沒出問題。

...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: passfinally: self.stream.close() self.pa.terminate()...

問題解答

回答1:

這裡給了一個可能解決方法(workaround),ghost commented on 30 Jul 2016

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 视频一区久久 | 在线观看一二三区 | 欧美精品久久久久久久久大尺度 | 成人免费高清视频 | 欧美一级视 | 国产精品18久久久久久小说 | 国产成人在线视频播放 | 99精品福利视频在线一区 | xxxwww在线播放| 请看一下欧美一级毛片 | www欧美com| 欧美aaa级 | 久久久精品影院 | 农村寡妇特一级毛片 | 亚洲美女视频网站 | 久久久久免费视频 | 国产jk福利视频在线观看 | 国产午夜精品免费一二区 | 亚洲毛片一级巨乳 | 一极毛片 | 免费一区二区三区在线视频 | 狼人总合狼人综合 | 久久狠狠一本精品综合网 | 国产欧美日韩三级 | 热99re久久精品香蕉 | 久草视频国产 | 综合国产 | 萌白酱香蕉白丝护士服喷浆 | 精品国产午夜久久久久九九 | 一区二区三区免费看 | 久久香焦 | 在线一区国产 | 在线欧美自拍 | 99精品久久久久久久免费看蜜月 | 老司机免费福利午夜入口ae58 | 免费毛片视频网站 | 怡红院宜春院 | 亚洲欧洲久久久精品 | 91av小视频 | 九九热视频在线免费观看 | 日韩 欧美 中文 亚洲 高清 在线 |