我正在尝试使用Python和PyQt5创建视频播放器。我已经构建了一个可播放视频的小部件,但无法使视频适合该小部件。我已经阅读了Qt的文档,但是我的理解是有限的。
下面的代码是我目前拥有的。
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Connect events (like button presses) to functions
self.ui.button_play.clicked.connect(self.callback_play) # Play button
self.ui.button_pause.clicked.connect(self.callback_pause) # Pause button
self.ui.button_stop.clicked.connect(self.callback_stop) # Stop button
# Configure the video widget
self.video_player = QtMultimedia.QMediaPlayer(None, QtMultimedia.QMediaPlayer.VideoSurface)
# Load in a file to play
file = QtCore.QDir.current().filePath("video.mp4")
self.video_player.setMedia(QtMultimedia.QMediaContent(QtCore.QUrl.fromLocalFile(file)))
self.video_player.setVideoOutput(self.ui.video_widget_input)
我尝试使用self.video_player.QtMultimediaWidgets.QVideoWidget.aspectRatioMode(1)
但返回
AttributeError: 'QMediaPlayer' object has no attribute 'QtMultimediaWidgets'