在不同的功能启动和停止SoundDevice流

问题描述 投票:0回答: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

它的工作原理,但我再不能从另一个功能停止流和应用程序停止运行。

如果任何人有一种变通方法或修正的想法,这将不胜感激

python pyqt python-sounddevice
1个回答
0
投票

因为你需要显式调用streamInstance.start()就可以开始处理该流不最初的代码示例中开始。这是当你将其括在一个with块(作为_StreamBase超类的__enter__方法中定义)自动完成。

由于马蒂亚斯说,这是不可能告诉你为什么不能阻止其他功能流不包括更多的代码。如果您with内环路阻塞和你没有使用多线程,则可能是其他功能不会被调用。

© www.soinside.com 2019 - 2024. All rights reserved.