1

我正在尝试在我的 PyQt 应用程序中使用 sounddevice OutputStream 来播放声音。

我想要类似的东西

import sounddevice as sd

def callback(...):
    #stuff that works with a "with" statement

class MyApp(QtWidgets.QMainWindow):
    def __init__(self):
        #stuff and buttons

    def startSound(self):
        #called by a button
        self.streamInstance = sd.OutputStream(settings)


    def stopSound(self):
        #called by another button
        self.streamInstance.close()

现在这不起作用,但如果我设置为:

with sd.OutputStream(settings):
        #a loop

它可以工作,但我无法停止来自另一个功能的流,并且应用程序停止运行。

如果有人有解决方法或修复的想法,将不胜感激

4

1 回答 1

0

流不会在初始代码示例中开始,因为您需要显式调用streamInstance.start()它来开始处理。当您将它包含在一个块中时(如在超类的方法with中定义的那样),这会自动完成。_StreamBase__enter__

正如 Matthias 所说,如果不包含更多代码,就无法说明为什么您无法从另一个函数中停止流。如果您内部的循环with被阻塞并且您没有使用多线程,则可能是另一个函数永远不会被调用。

于 2019-02-03T15:09:00.113 回答