Unsolved MacOS - The video widget is not compatible with QtWebEngine
-
I have a program embedded with both media player and QtWebEngine, which is used to access specific web pages when playing video.
It worked perfectly on Windows and Linux, and when I ran it on MacOS, the media player would become invisible, and QtWebEngine had some display problems.
I tested the VLC MediaPlayer and QMediaPlayer on PySide5.9.6 and 5.11, and they all had the same problems.
VLC test code
# -*- coding: utf-8 -*- import sys import os import vlc from PySide2.QtGui import * from PySide2.QtWidgets import * from PySide2.QtCore import * from PySide2.QtWebEngineWidgets import * class test_widget(QWidget): def __init__(self, parent=None): super(test_widget, self).__init__(parent) self.__ui__() def __ui__(self): t_lay_parent = QVBoxLayout() t_lay_content = QHBoxLayout() t_lay_tool = QHBoxLayout() self.label_media = QLabel() self.webengine = QWebEngineView() t_lay_content.addWidget(self.label_media, 1) t_lay_content.addWidget(self.webengine, 1) self.lineedit_url = QLineEdit("http://baidu.com") self.button_go = QPushButton("Go") self.button_play = QPushButton("Play") t_lay_tool.addWidget(self.lineedit_url) t_lay_tool.addWidget(self.button_go) t_lay_tool.addWidget(self.button_play) self.button_go.clicked.connect(self.slt_go) self.button_play.clicked.connect(self.slt_play) t_lay_parent.addLayout(t_lay_content) t_lay_parent.addLayout(t_lay_tool) self.setLayout(t_lay_parent) self.instance = vlc.Instance("-q") self.m_player = self.instance.media_player_new() t_wid = self.label_media.winId() if sys.platform.startswith('linux'): # linux self.m_player.set_xwindow(t_wid) elif sys.platform == "win32": # windows self.m_player.set_hwnd(t_wid) elif sys.platform == "darwin": # mac self.m_player.set_nsobject(t_wid) def slt_go(self): t_url = self.lineedit_url.text() self.webengine.setUrl(QUrl(t_url)) def slt_play(self): t_media = self.instance.media_new("1.mp4") self.m_player.set_media(t_media) self.m_player.play() if __name__ == '__main__': app = QApplication(sys.argv) win = test_widget() win.show() sys.exit(app.exec_())
QMediaPlayer test code
# -*- coding: utf-8 -*- import os import sys from PySide2.QtGui import * from PySide2.QtCore import * from PySide2.QtWidgets import * from PySide2.QtMultimedia import * from PySide2.QtWebEngineWidgets import * from PySide2.QtMultimediaWidgets import * class demo_widget(QWidget): def __init__(self, parent=None): super(demo_widget, self).__init__(parent) self.__ui__() def __ui__(self): t_lay_parnet = QVBoxLayout() t_lay_content = QHBoxLayout() self.video_widget = QVideoWidget() self.webView = QWebEngineView() t_lay_content.addWidget(self.video_widget, 1) t_lay_content.addWidget(self.webView, 1) t_lay_bottom = QHBoxLayout() self.lineEdit = QLineEdit("https://Google.com") self.pushButton = QPushButton("Go") self.pushButton1 = QPushButton("Play") t_lay_bottom.addWidget(self.lineEdit) t_lay_bottom.addWidget(self.pushButton) t_lay_bottom.addWidget(self.pushButton1) t_lay_parnet.addLayout(t_lay_content) t_lay_parnet.addLayout(t_lay_bottom) self.setLayout(t_lay_parnet) self.pushButton.clicked.connect(self.slt_jump) self.pushButton1.clicked.connect(self.slt_play) self.player = QMediaPlayer() self.player.setVideoOutput(self.video_widget) def slt_jump(self): t_text = self.lineEdit.text() if t_text.strip() != "": self.webView.load(QUrl(t_text)) def slt_play(self): self.player.setMedia(QMediaContent(QUrl.fromLocalFile("1.mp4"))) self.player.play() if __name__ == "__main__": app = QApplication(sys.argv) win = demo_widget() win.show() sys.exit(app.exec_())