终止时PyAudio崩溃

2024-05-28 22:56:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用pyaudio进行音频播放,但无法在命令下停止它。 当我让我的播放列表从头到尾播放时,它工作得很好,但是如果我试图预先中断播放,它确实会停止,但它也会使python崩溃,错误报告显示错误模块是_门户网站.pyd. 我使用的是Anaconda2.4.1和Python2.7.1164位。 以下是控制播放的功能:

    def play_stop(self):
        self.is_playing=self.toggle_flag(self.is_playing)
        t=Create_thread(self.snd_play)
        t.start()

    def snd_play(self):
        global stream
        global p
        self.plist=list()
        p=pyaudio.PyAudio()
        self.ind=self.buffersize-1
        self.currind=1
        for i,f in enumerate(self.file_list[:self.buffersize]):
            self.plist.append(wave.open(f,"rb"))
        while len(self.plist)>0 and self.is_playing:

            if len(self.plist)<self.buffersize:

                self.ind+=1
                try:
                    self.plist.append(wave.open(self.file_list[self.ind],"rb"))
                except IndexError:
                    pass
        self.indexcounter.setText("Zvuk: %d/%d" %(self.currind,len(self.file_list)))
        stream=p.open(format=p.get_format_from_width(self.plist[0].getsampwidth()),
                      channels=self.plist[0].getnchannels(),
                                rate=self.plist[0].getframerate(),
                                output=True)
            data=self.plist[0].readframes(self.plist[0].getnframes())
            stream.write(data)
            stream.stop_stream()
            #stream.close()
            self.plist.pop(0)
            self.currind+=1

            try:
                sleep(abs(float(self.playList.item(self.currind-1,1).text())))
            except TypeError:
                QtGui.QMessageBox.critical(self,u"Greška", "Interval nije cijeli ni decimalni broj")
                break
            except:
                    pass

        else:
            self.is_playing=False
            stream.stop_stream()
            stream.close()
            p.terminate()

python本身在崩溃之前不会抛出任何错误,只有在我尝试停止流、关闭它并在pyaudio实例结束之前终止它时才会崩溃。我用pip安装了pyaudio 正如您所看到的,snd_play()方法总是在一个新线程中执行,这就是为什么我在实例化之前切换playing标志,因为我假设所有线程在函数完成后都会停止


Tags: selfplaystreamisopenlistfileplist

热门问题