-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
140 lines (129 loc) · 4.23 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "mainwindow.h"
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
this->setWindowTitle(QString::fromLocal8Bit("Media Player"));
// this->setFixedSize(1280, 720);
// 获取屏幕宽度和高度
QList<QScreen*> list_screen = QGuiApplication::screens();
QRect rect = list_screen.at(0)->geometry();
this->setGeometry((rect.width() - 1280) / 2, (rect.height() - 760) / 2, 1280, 760);
videoPlayWidget = new VideoPlayWidget();
// videoPlayWidget->setFixedSize(1280, 720);
// videoPlayWidget->setGeometry(0, 0, 1280, 720);
this->setCentralWidget(videoPlayWidget);
// 菜单栏部分--------------------------------------------------
QMenu* fileMenu = menuBar()->addMenu(QString::fromLocal8Bit("文件"));
QAction* openAction = new QAction(QString::fromLocal8Bit("打开"), this);
fileMenu->addAction(openAction);
// 状态栏部分--------------------------------------------------
progressBar = new QSlider(this);
progressBar->setOrientation(Qt::Horizontal);
progressBar->setMinimum(0);
progressBar->setMaximum(1000);
progressBar->setSingleStep(1);
openFileButton = new QPushButton(QString::fromLocal8Bit("打开文件"));
playOrPauseButton = new QPushButton(QString::fromLocal8Bit("播放"));
playOrPauseButton->setEnabled(false);
statusBar()->addWidget(progressBar);
statusBar()->addWidget(openFileButton);
statusBar()->addWidget(playOrPauseButton);
connect(openAction, &QAction::triggered, this, &MainWindow::OpenFile);
connect(openFileButton, &QPushButton::clicked, this, &MainWindow::OpenFile);
connect(playOrPauseButton, &QPushButton::clicked, this, &MainWindow::PlayOrPause);
connect(progressBar, &QSlider::sliderPressed, this, &MainWindow::progressBarPress);
connect(progressBar, &QSlider::sliderReleased, this, &MainWindow::progressBarRelease);
// 更新播放进度
startTimer(40);
}
MainWindow::~MainWindow()
{
demuxThreadUtil.Close();
}
// 打开播放目标文件
void MainWindow::OpenFile()
{
QString fileUrl = QFileDialog::getOpenFileName(this, QString::fromLocal8Bit("选择视频文件"));
if (fileUrl.isEmpty())
{
return;
}
demuxThreadUtil.StartThreads();
// 启动音视频线程
this->setWindowTitle(fileUrl);
if (!demuxThreadUtil.Open(fileUrl.toLocal8Bit(), videoPlayWidget))
{
QMessageBox::information(nullptr, "ERROR", QString::fromLocal8Bit("无法打开文件"));
return;
}
// 启用播放暂停按钮
playOrPauseButton->setEnabled(true);
// 根据当前状态判断是否设置暂停(暂时无用)
SetPause(demuxThreadUtil.isPause);
}
// 播放和暂停
void MainWindow::PlayOrPause()
{
bool isPause = !demuxThreadUtil.isPause;
// 更新播放暂停按钮状态
SetPause(isPause);
// 暂停音视频线程
demuxThreadUtil.SetPause(isPause);
}
// 定时刷新播放器进度条
void MainWindow::timerEvent(QTimerEvent* event)
{
if (isProcessBarPress)
{
return;
}
long long totalMilliseconds = demuxThreadUtil.totalMilliseconds;
if (totalMilliseconds > 0)
{
double pos = (double)demuxThreadUtil.pts / (double)totalMilliseconds;
int value = progressBar->maximum() * pos;
progressBar->setValue(value);
}
}
// 界面尺寸改变事件
void MainWindow::resizeEvent(QResizeEvent* event)
{
}
// 进入与退出播放器全屏
void MainWindow::mouseDoubleClickEvent(QMouseEvent* event)
{
if (isFullScreen())
{
this->showNormal();
}
else
{
this->showFullScreen();
}
}
// 设置播放和暂停按钮状态
void MainWindow::SetPause(bool isPause)
{
if (isPause)
{
playOrPauseButton->setText(QString::fromLocal8Bit("播放"));
}
else
{
playOrPauseButton->setText(QString::fromLocal8Bit("暂停"));
}
}
// 点击进度条(不松)
void MainWindow::progressBarPress()
{
isProcessBarPress = true;
}
// 快进 点击进度条(松开)
void MainWindow::progressBarRelease()
{
isProcessBarPress = false;
double pos = 0.0;
pos = (double)progressBar->value() / (double)progressBar->maximum();
qDebug() << QString::fromLocal8Bit("进度条目标位置") << pos;
demuxThreadUtil.Seek(pos);
}