-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverlayvideoplayer.cpp
120 lines (105 loc) · 3.16 KB
/
overlayvideoplayer.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
#include "overlayvideoplayer.h"
#include <QVBoxLayout>
OverlayVideoPlayer::OverlayVideoPlayer(QWidget *parent)
: QWidget{parent}
{
scene = new QGraphicsScene(this);
view = new QGraphicsView(scene, this);
videoItem = new QGraphicsVideoItem;
audio = new QAudioOutput;
player = new QMediaPlayer;
player->setVideoOutput(videoItem);
player->setAudioOutput(audio);
audio->setVolume(1);
connect(player, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), this, SLOT(onMediaStatusChanged(QMediaPlayer::MediaStatus)));
connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(onPositionChanged(qint64)));
videoItem->setAspectRatioMode(Qt::KeepAspectRatio);
videoItem->setSize(QSize(400, 300));
scene->addItem(videoItem);
QImage img;
img.load(":/images/logo.png");
auto logoPixmap = QPixmap::fromImage(img);
logoPixmap = logoPixmap.scaled(560, 200, Qt::KeepAspectRatio);
logoItem = scene->addPixmap(logoPixmap);
logoItem->setOffset(-280, -100);
setLayout(new QVBoxLayout());
this->layout()->addWidget(view);
}
void OverlayVideoPlayer::resizeEvent(QResizeEvent *event)
{
auto rect = this->contentsRect();
int diff = 25;
rect.setSize(QSize(rect.size().width() - diff, rect.size().height() - diff));
scene->setSceneRect(rect);
videoItem->setSize(rect.size());
videoItem->setOffset(QPointF(-rect.width() / 2, -rect.height() / 2));
auto logoPos = QPointF(rect.width() / 2, rect.height() / 2);
logoItem->setPos(logoPos);
videoItem->setPos(logoPos);
view->centerOn(logoPos);
}
void OverlayVideoPlayer::mouseDoubleClickEvent(QMouseEvent *event)
{
if (player->mediaStatus() != QMediaPlayer::NoMedia){
if (player->isPlaying()) {
player->pause();
logoItem->setVisible(true);
emit playStatusChanged(false);
} else {
player->play();
logoItem->setVisible(false);
emit playStatusChanged(true);
}
}
}
void OverlayVideoPlayer::setVideoUrl(QUrl url)
{
player->setSource(url);
player->play();
logoItem->setVisible(false);
}
void OverlayVideoPlayer::closeVideo()
{
player->setSource(QUrl());
logoItem->setVisible(true);
}
void OverlayVideoPlayer::setVolume(int v)
{
if (v == 0) {
audio->setMuted(true);
emit muteChanged(true);
} else if (audio->isMuted()) {
audio->setMuted(false);
emit muteChanged(false);
}
float target = 0.01 * v;
audio->setVolume(target);
}
void OverlayVideoPlayer::toggleMuted()
{
if (audio->isMuted()) {
audio->setMuted(false);
emit muteChanged(false);
} else {
audio->setMuted(true);
emit muteChanged(true);
}
}
void OverlayVideoPlayer::setPosition(qint64 pos)
{
if (pos <= player->duration()) {
player->setPosition(pos);
}
}
void OverlayVideoPlayer::onMediaStatusChanged(QMediaPlayer::MediaStatus status)
{
if (status == QMediaPlayer::MediaStatus::EndOfMedia)
{
logoItem->setVisible(true);
emit playbackEnded();
}
}
void OverlayVideoPlayer::onPositionChanged(qint64 pos)
{
emit videoProgressChanged(pos, player->duration());
}