教你自己做一个自定义的播放器 支持全屏,不支持窗口最大化、取消全屏

v1: 最简单的视频播放器:

from PyQt5.QtWidgets import QApplication, QFileDialog
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget

if __name__ == '__main__':
    app = QApplication([])
    player = QMediaPlayer()
    wgt_video = QVideoWidget()  # 视频显示的widget
    wgt_video.show()
    player.setVideoOutput(wgt_video)  # 视频输出的widget
    player.setMedia(QMediaContent(QFileDialog.getOpenFileUrl()[0]))  # 选取视频文件
    player.play()
    app.exec_()

v2 自定义界面:

先用designer写一个界面:

 在提升一个类:

        

 提升类名称:QVideoWidget

头文件:PyQt5.QtMultimediaWidgets

 在创建一个随便名字.py:

from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtWidgets import QFileDialog, QApplication,QMainWindow
from gui import Ui_MainWindow     # 设计的ui转成gui.py,方法这里不多说了

class videoPlayer:
    def __init__(self,parent=None):
        super(QMainWindow,self).__init__(parent)
        self.ui = Ui_MainWindow()         # gui里的类
        self.ui.setupUi(self)             # setupUi
        self.player = QMediaPlayer()
        self.player.setVideoOutput(self.ui.widget)
        self.ui.pushButton.clicked.connect(self.openVideoFile)
    # 打开视频文件并播放
    def openVideoFile(self):
        self.player.setMedia(QMediaContent(QFileDialog.getOpenFileUrl()[0]))
        self.player.play()


if __name__ == "__main__":
    app = QApplication([])
    myPlayer = videoPlayer()
    myPlayer.ui.show()
    app.exec()

v3 加入进度和装饰:

gui.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1073</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <property name="styleSheet">
   <string notr="true">#MainWindow {border-image: url(:/image/E:/视频播放/R-C.jpg);}</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QListWidget" name="listWidget">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>10</y>
      <width>281</width>
      <height>571</height>
     </rect>
    </property>
   </widget>
   <widget class="QVideoWidget" name="widget" native="true">
    <property name="geometry">
     <rect>
      <x>310</x>
      <y>10</y>
      <width>751</width>
      <height>511</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton">
    <property name="geometry"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值