UI设计
代码展示
video.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>563</width>
<height>335</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>563</width>
<height>335</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>563</width>
<height>335</height>
</size>
</property>
<property name="windowTitle">
<string>PyVideo</string>
</property>
<property name="windowIcon">
<iconset>
<normaloff>video.png</normaloff>video.png</iconset>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QVideoWidget" name="wgt_player" native="true">
<property name="geometry">
<rect>
<x>30</x>
<y>30</y>
<width>501</width>
<height>241</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">border-radius:7px;
background-color: rgb(197, 197, 197);</string>
</property>
</widget>
<widget class="QPushButton" name="btn_play_pause">
<property name="geometry">
<rect>
<x>30</x>
<y>292</y>
<width>21</width>
<height>21</height>
</rect>
</property>
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="styleSheet">
<string notr="true">border:none;
image: url(:/imge/暂停.png);</string>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="QSlider" name="sld_duration">
<property name="geometry">
<rect>
<x>30</x>
<y>275</y>
<width>501</width>
<height>16</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">QSlider::groove:horizontal {
height: 2.5px;
margin: 0px 0;
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgb(203, 203, 203), stop: 1.0 rgb(203, 203, 203));
}
QSlider::handle:horizontal {
width: 9px;
height: 5px;
background-color: rgb(44, 44, 44);
margin: -5px 0px -5px 0px;
border-radius:4px;
}
QSlider::add-page:Horizontal
{
height:4px;
}
QSlider::sub-page:Horizontal
{
background-color: rgb(0, 170, 255);
height:4px;
}\
QSlider::groove:Horizontal
{\
background:transparent;
height:6px;
}\
QSlider::handle:Horizontal
{
height: 30px;
width:8px;
margin: -8 0px;
}
</string>
</property>
<property name="maximum">
<number>1000</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QLabel" name="lab_duration">
<property name="geometry">
<rect>
<x>60</x>
<y>292</y>
<width>41</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<family>微软雅黑</family>
</font>
</property>
<property name="text">
<string>--:--</string>
</property>
</widget>
<widget class="QPushButton" name="btn_select">
<property name="geometry">
<rect>
<x>470</x>
<y>290</y>
<width>61</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<family>微软雅黑</family>
</font>
</property>
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="styleSheet">
<string notr="true">border:none;
background-color:rgb(153, 153, 153);
color: rgb(255, 255, 255);
border-radius:4px;</string>
</property>
<property name="text">
<string>选择视频</string>
</property>
</widget>
<widget class="QSlider" name="volumeSlider">
<property name="geometry">
<rect>
<x>390</x>
<y>290</y>
<width>71</width>
<height>20</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">QSlider::groove:horizontal {
height: 2.5px;
margin: 0px 0;
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgb(203, 203, 203), stop: 1.0 rgb(203, 203, 203));
}
QSlider::handle:horizontal {
width: 9px;
height: 5px;
background-color: rgb(44, 44, 44);
margin: -5px 0px -5px 0px;
border-radius:4px;
}
QSlider::add-page:Horizontal
{
height:4px;
}
QSlider::sub-page:Horizontal
{
background-color: rgb(0, 170, 255);
height:4px;
}\
QSlider::groove:Horizontal
{\
background:transparent;
height:6px;
}\
QSlider::handle:Horizontal
{
height: 30px;
width:8px;
margin: -8 0px;
}
</string>
</property>
<property name="maximum">
<number>10</number>
</property>
<property name="value">
<number>8</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QLabel" name="imge">
<property name="geometry">
<rect>
<x>360</x>
<y>290</y>
<width>31</width>
<height>21</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">image: url(:/imge/volume.png);</string>
</property>
<property name="text">
<string/>
</property>
</widget>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<customwidgets>
<customwidget>
<class>QVideoWidget</class>
<extends>QWidget</extends>
<header location="global">PyQt5.QtMultimediaWidgets</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources>
<include location="Resources.qrc"/>
</resources>
<connections/>
</ui>
直接复制进video.ui就行了
资源部分
把以下图片保存到本地重命名即可
播放.png
暂停.png
volume.png
video.png
主文件
video.py
import os
from PyQt5.QtCore import Qt
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtWidgets import QFileDialog, QApplication
from PyQt5 import uic
class videoPlayer:
def __init__(self):
# 初始化
self.ui = uic.loadUi('./video.ui') # 加载designer设计的ui程序
# 播放器
self.player = QMediaPlayer()
self.player.setVideoOutput(self.ui.wgt_player)
# 按钮
self.ui.btn_select.clicked.connect(self.open)
self.ui.btn_play_pause.clicked.connect(self.playPause)
self.ui.imge.setStyleSheet("image: url(./volume.png)")
self.ui.btn_play_pause.setStyleSheet("border:none;"
"image: url(./暂停.png)")
# 进度条
self.player.durationChanged.connect(self.getDuration)
self.player.positionChanged.connect(self.getPosition)
self.ui.sld_duration.sliderMoved.connect(self.updatePosition)
self.ui.volumeSlider.valueChanged.connect(self.set_volume_func)
def keyPressEvent(self, event):
if event.key() == Qt.Key_Space:
self.playPause()
def set_volume_func(self):
self.player.setVolume(self.ui.volumeSlider.value())
# 打开视频文件
def open(self):
self.player.setMedia(QMediaContent(QFileDialog.getOpenFileUrl()[0]))
self.player.play()
# 播放视频
def playPause(self):
if self.player.state()==1:
self.ui.btn_play_pause.setStyleSheet("border:none;"
"image: url(./播放.png)")
self.player.pause()
else:
self.ui.btn_play_pause.setStyleSheet("border:none;"
"image: url(./暂停.png)")
self.player.play()
# 视频总时长获取
def getDuration(self, d):
'''d是获取到的视频总时长(ms)'''
self.ui.sld_duration.setRange(0, d)
self.ui.sld_duration.setEnabled(True)
self.displayTime(d)
# 视频实时位置获取
def getPosition(self, p):
self.ui.sld_duration.setValue(p)
self.displayTime(p)
# 显示剩余时间
def displayTime(self, ms):
minutes = int(ms/60000)
seconds = int((ms-minutes*60000)/1000)
self.ui.lab_duration.setText('{}:{}'.format(minutes, seconds))
# 用进度条更新视频位置
def updatePosition(self, v):
self.player.setPosition(v)
self.displayTime(self.ui.sld_duration.maximum()-v)
# 获取并设置音量
def updatevld(self, v):
self.player.setPosition(v)
self.volumeSlider.setAudioOutput(self.audioOutput)
self.displayTime(self.ui.sld_duration.maximum()-v)
if __name__ == "__main__":
app = QApplication([])
myPlayer = videoPlayer()
myPlayer.ui.show()
app.exec()
保存运行即可
然后就可以随时随地的看罗老师的刑法课了!(awa!)
原文地址:YupenBobの小窝