-
Notifications
You must be signed in to change notification settings - Fork 261
/
PlayerDemo.cpp
170 lines (141 loc) · 4.36 KB
/
PlayerDemo.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* @file
* @brief Source file for Demo QtPlayer application
* @author Jonathan Thomas <jonathan@openshot.org>
*
* @ref License
*/
/* LICENSE
*
* Copyright (c) 2008-2019 OpenShot Studios, LLC
* <http://www.openshotstudios.com/>. This file is part of
* OpenShot Library (libopenshot), an open-source project dedicated to
* delivering high quality video editing and animation solutions to the
* world. For more information visit <http://www.openshot.org/>.
*
* OpenShot Library (libopenshot) is free software: you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* OpenShot Library (libopenshot) is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
#include "PlayerDemo.h"
#include "../QtPlayer.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QWidget>
#include <QBoxLayout>
#include <QMenuBar>
#include <QMenu>
#include <QKeyEvent>
#include <QCloseEvent>
#include <QApplication>
PlayerDemo::PlayerDemo(QWidget *parent)
: QWidget(parent)
, vbox(new QVBoxLayout(this))
, menu(new QMenuBar(this))
, video(new VideoRenderWidget(this))
, player(new openshot::QtPlayer(video->GetRenderer()))
{
setWindowTitle("OpenShot Player");
menu->setNativeMenuBar(false);
QAction *action = NULL;
action = menu->addAction("Choose File");
connect(action, SIGNAL(triggered(bool)), this, SLOT(open(bool)));
vbox->addWidget(menu, 0);
vbox->addWidget(video, 1);
vbox->setMargin(0);
vbox->setSpacing(0);
resize(600, 480);
// Accept keyboard event
setFocusPolicy(Qt::StrongFocus);
}
PlayerDemo::~PlayerDemo()
{
}
void PlayerDemo::closeEvent(QCloseEvent *event)
{
// Close window, stop player, and quit
QWidget *pWin = QApplication::activeWindow();
pWin->hide();
player->Stop();
QApplication::quit();
}
void PlayerDemo::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Space || event->key() == Qt::Key_K) {
if (player->Mode() == openshot::PLAYBACK_PAUSED)
{
// paused, so start playing again
player->Play();
}
else if (player->Mode() == openshot::PLAYBACK_PLAY)
{
if (player->Speed() == 0)
// already playing, but speed is zero... so just speed up to normal
player->Speed(1);
else
// already playing... so pause
player->Pause();
}
}
else if (event->key() == Qt::Key_J) {
std::cout << "BACKWARD" << player->Speed() - 1 << std::endl;
if (player->Speed() - 1 != 0)
player->Speed(player->Speed() - 1);
else
player->Speed(player->Speed() - 2);
if (player->Mode() == openshot::PLAYBACK_PAUSED)
player->Play();
}
else if (event->key() == Qt::Key_L) {
std::cout << "FORWARD" << player->Speed() + 1 << std::endl;
if (player->Speed() + 1 != 0)
player->Speed(player->Speed() + 1);
else
player->Speed(player->Speed() + 2);
if (player->Mode() == openshot::PLAYBACK_PAUSED)
player->Play();
}
else if (event->key() == Qt::Key_Left) {
std::cout << "FRAME STEP -1" << std::endl;
if (player->Speed() != 0)
player->Speed(0);
player->Seek(player->Position() - 1);
}
else if (event->key() == Qt::Key_Right) {
std::cout << "FRAME STEP +1" << std::endl;
if (player->Speed() != 0)
player->Speed(0);
player->Seek(player->Position() + 1);
}
else if (event->key() == Qt::Key_Escape) {
std::cout << "QUIT PLAYER" << std::endl;
QWidget *pWin = QApplication::activeWindow();
pWin->hide();
player->Stop();
QApplication::quit();
}
event->accept();
QWidget::keyPressEvent(event);
}
void PlayerDemo::open(bool checked)
{
// Get filename of media files
const QString filename = QFileDialog::getOpenFileName(this, "Open Video File");
if (filename.isEmpty()) return;
// Create FFmpegReader and open file
player->SetSource(filename.toStdString());
// Set aspect ratio of widget
video->SetAspectRatio(player->Reader()->info.display_ratio, player->Reader()->info.pixel_ratio);
// Play video
player->Play();
}