117 lines
4.2 KiB
Python
117 lines
4.2 KiB
Python
from PyQt6.QtCore import Qt, QUrl, pyqtSignal
|
|
from PyQt6.QtWidgets import QWidget, QVBoxLayout, QPushButton, QHBoxLayout
|
|
from PyQt6.QtMultimedia import QMediaPlayer
|
|
from PyQt6.QtMultimediaWidgets import QVideoWidget
|
|
import os
|
|
|
|
class WelcomeVideoPlayer(QWidget):
|
|
"""欢迎视频播放器,作为可嵌入组件"""
|
|
videoFinished = pyqtSignal()
|
|
|
|
def __init__(self, parent=None):
|
|
super().__init__(parent)
|
|
|
|
# 创建视频播放组件
|
|
self.video_widget = QVideoWidget()
|
|
self.media_player = QMediaPlayer()
|
|
self.media_player.setVideoOutput(self.video_widget)
|
|
|
|
# 设置跳过按钮
|
|
self.skip_button = QPushButton("跳过")
|
|
# 增强按钮可见性,使用更不透明的背景和更粗的边框
|
|
self.skip_button.setStyleSheet("""
|
|
QPushButton {
|
|
background-color: rgba(0, 0, 0, 0.6);
|
|
color: white;
|
|
border: 2px solid white;
|
|
border-radius: 8px;
|
|
padding: 8px 20px;
|
|
font-size: 16px;
|
|
font-weight: bold;
|
|
}
|
|
QPushButton:hover {
|
|
background-color: rgba(0, 0, 0, 0.8);
|
|
}
|
|
""")
|
|
self.skip_button.clicked.connect(self.skip_video)
|
|
# 确保按钮始终在最上层
|
|
self.skip_button.raise_()
|
|
|
|
# 创建布局
|
|
self.main_layout = QVBoxLayout(self)
|
|
self.main_layout.setContentsMargins(0, 0, 0, 0)
|
|
self.main_layout.setSpacing(0)
|
|
|
|
# 将视频组件添加到主布局
|
|
self.main_layout.addWidget(self.video_widget, 1)
|
|
|
|
# 创建底部布局用于放置跳过按钮
|
|
self.bottom_layout = QHBoxLayout()
|
|
self.bottom_layout.addStretch()
|
|
self.bottom_layout.addWidget(self.skip_button)
|
|
self.bottom_layout.setContentsMargins(0, 0, 20, 30) # 增加底部边距
|
|
self.bottom_layout.setAlignment(Qt.AlignmentFlag.AlignBottom | Qt.AlignmentFlag.AlignRight)
|
|
|
|
# 将底部布局添加到主布局
|
|
self.main_layout.addLayout(self.bottom_layout, 0)
|
|
self.setLayout(self.main_layout) # 明确设置布局
|
|
|
|
# 连接信号
|
|
self.media_player.positionChanged.connect(self.check_position)
|
|
self.media_player.mediaStatusChanged.connect(self.media_status_changed)
|
|
|
|
# 初始隐藏
|
|
self.hide()
|
|
|
|
def load_video(self):
|
|
"""加载欢迎视频"""
|
|
video_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "_internal", "start.mp4")
|
|
if os.path.exists(video_path):
|
|
self.media_player.setSource(QUrl.fromLocalFile(video_path))
|
|
self.media_player.play()
|
|
else:
|
|
print(f"视频文件不存在: {video_path}")
|
|
self.videoFinished.emit()
|
|
|
|
def load_video(self):
|
|
"""加载欢迎视频"""
|
|
video_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "_internal", "start.mp4")
|
|
if os.path.exists(video_path):
|
|
self.media_player.setSource(QUrl.fromLocalFile(video_path))
|
|
self.media_player.play()
|
|
else:
|
|
print(f"视频文件不存在: {video_path}")
|
|
self.videoFinished.emit()
|
|
|
|
def check_position(self, position):
|
|
"""检查视频播放进度"""
|
|
if self.media_player.duration() > 0 and position >= self.media_player.duration() - 1000: # 接近结束时
|
|
self.videoFinished.emit()
|
|
|
|
def media_status_changed(self, status):
|
|
"""媒体状态变化处理"""
|
|
from PyQt6.QtMultimedia import QMediaPlayer
|
|
if status == QMediaPlayer.MediaStatus.EndOfMedia:
|
|
self.videoFinished.emit()
|
|
|
|
def skip_video(self):
|
|
"""跳过视频"""
|
|
self.media_player.stop()
|
|
self.videoFinished.emit()
|
|
|
|
def start_playback(self):
|
|
"""开始播放视频"""
|
|
self.load_video()
|
|
self.show()
|
|
# 确保按钮在播放时也在顶层
|
|
self.skip_button.raise_()
|
|
|
|
def stop_playback(self):
|
|
"""停止播放视频"""
|
|
self.media_player.stop()
|
|
self.hide()
|
|
|
|
def closeEvent(self, event):
|
|
"""关闭事件处理"""
|
|
self.media_player.stop()
|
|
event.accept() |