Search code examples
pythonqtqt5pysidepyside2

Processing signal from QAudioProbe


I am trying to receive the signal from QAudioProbe's audioBufferProbed signal. I have tried the connect function but I am not using it properly. I want to print the signal values to the console while the media plays.

I am using Python3 and PySide2 (Qt 5.15).

#!/bin/python3

from PySide2.QtMultimedia import QMediaPlayer, QMediaContent, QAudioProbe
from PySide2.QtCore import QUrl, QCoreApplication, QObject, Signal
import sys


def main():

    app = QCoreApplication()
    player = QMediaPlayer()
    url = QUrl.fromLocalFile("/home/ubuntu/Downloads/sample2.mp3")
    content = QMediaContent(url)
    player.setMedia(content)
    player.setVolume(50)

    # probe = QAudioProbe()
    # probe.setSource(player)
    # QObject.connect(probe, Signal(audioBufferProbed(QAudioBuffer)), processProbe)

    player.play()
    ret = app.exec_()
    sys.exit(ret)

def processProbe(probe):
    print(probe)


if __name__ == "__main__":
    main()

Solution

  • You have to use the syntax of PySide2 (and also PyQt5) is sender.signal.connect(receiver.slot):

    #!/bin/python3
    
    from PySide2.QtMultimedia import QMediaPlayer, QMediaContent, QAudioProbe
    from PySide2.QtCore import QUrl, QCoreApplication, QObject, Signal
    import sys
    
    
    def main():
    
        app = QCoreApplication()
        player = QMediaPlayer()
        url = QUrl.fromLocalFile("/home/ubuntu/Downloads/sample2.mp3")
        content = QMediaContent(url)
        player.setMedia(content)
        player.setVolume(0)
    
        probe = QAudioProbe()
        probe.setSource(player)
        probe.audioBufferProbed.connect(processProbe)
    
        player.play()
        ret = app.exec_()
        sys.exit(ret)
    
    def processProbe(buff):
        print(buff.startTime())
    
    
    if __name__ == "__main__":
        main()