0

python-vlc我使用和创建了一个媒体播放器应用程序,与此 qtvlc 代码PyQt5非常相似。一个最小的可重现代码如下所示:

class Player(QMainWindow):
    """A simple Media Player using VLC and Qt
    """
    def __init__(self, master=None):
        QMainWindow.__init__(self, master)
        self.setWindowTitle("Media Player")

        # creating a basic vlc instance
        self.instance = vlc.Instance()
        # creating an empty vlc media player
        self.mediaplayer = self.instance.media_player_new()

        self.createUI()
        self.isPaused = False

    def createUI(self):
        """Set up the user interface, signals & slots
        """
        self.widget = QWidget(self)
        self.setCentralWidget(self.widget)

        self.videoframe = QFrame()
        self.videoframe.setAutoFillBackground(True)

        pixmap = QtGui.QPixmap()
        pixmap.load("path to transparent_image.png")
        self.my_label = QLabel(self)
        self.my_label.setPixmap(pixmap)
        self.my_label.setGeometry(300, 250, 100, 100)
        self.my_label.setFrameStyle(QFrame.NoFrame)
        self.my_label.setAttribute(Qt.WA_TranslucentBackground)

        self.vboxlayout = QVBoxLayout()
        self.vboxlayout.addWidget(self.videoframe)

        self.widget.setLayout(self.vboxlayout)

        self.media = self.instance.media_new("path to video file")
        # put the media in the media player
        self.mediaplayer.set_media(self.media)

        # the media player has to be 'connected' to the QFrame
        # (otherwise a video would be displayed in it's own window)
        # this is platform specific!
        # you have to give the id of the QFrame (or similar object) to
        # vlc, different platforms have different functions for this
        if sys.platform.startswith('linux'): # for Linux using the X Server
            self.mediaplayer.set_xwindow(self.videoframe.winId())
        elif sys.platform == "win32": # for Windows
            self.mediaplayer.set_hwnd(self.videoframe.winId())
        elif sys.platform == "darwin": # for MacOS
            self.mediaplayer.set_nsobject(self.videoframe.winId())
        self.mediaplayer.play()

    
if __name__ == "__main__":
    app = QApplication(sys.argv)
    player = Player()
    player.show()
    player.resize(640, 480)
    sys.exit(app.exec_())

运行代码后,我可以看到透明图像绘制在视频之上,但图像而不是通过它显示背景视频继续显示QFrame其背景的一部分。谁能帮我解决这个问题?

4

0 回答 0