处理来自 QAudioProbe 的信号

Processing signal from QAudioProbe

我正在尝试接收来自 QAudioProbe 的 audioBufferProbed 信号的信号。我已经尝试过连接功能,但我没有正确使用它。我想在媒体播放时将信号值打印到控制台。

我正在使用 Python3 和 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()

你必须使用 PySide2(还有 PyQt5)的语法是 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()