OpenShot Library | libopenshot 0.2.7
PlayerDemo.cpp
Go to the documentation of this file.
1/**
2 * @file
3 * @brief Source file for Demo QtPlayer application
4 * @author Jonathan Thomas <jonathan@openshot.org>
5 *
6 * @ref License
7 */
8
9/* LICENSE
10 *
11 * Copyright (c) 2008-2019 OpenShot Studios, LLC
12 * <http://www.openshotstudios.com/>. This file is part of
13 * OpenShot Library (libopenshot), an open-source project dedicated to
14 * delivering high quality video editing and animation solutions to the
15 * world. For more information visit <http://www.openshot.org/>.
16 *
17 * OpenShot Library (libopenshot) is free software: you can redistribute it
18 * and/or modify it under the terms of the GNU Lesser General Public License
19 * as published by the Free Software Foundation, either version 3 of the
20 * License, or (at your option) any later version.
21 *
22 * OpenShot Library (libopenshot) is distributed in the hope that it will be
23 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public License
28 * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29 */
30
31#include <string>
32
33#include "PlayerDemo.h"
34#include "../QtPlayer.h"
35
36#include <QMessageBox>
37#include <QFileDialog>
38#include <QWidget>
39#include <QBoxLayout>
40#include <QMenuBar>
41#include <QMenu>
42#include <QKeyEvent>
43#include <QCloseEvent>
44#include <QApplication>
45
46PlayerDemo::PlayerDemo(QWidget *parent)
47 : QWidget(parent)
48 , vbox(new QVBoxLayout(this))
49 , menu(new QMenuBar(this))
50 , video(new VideoRenderWidget(this))
51 , player(new openshot::QtPlayer(video->GetRenderer()))
52{
53 setWindowTitle("OpenShot Player");
54
55 menu->setNativeMenuBar(false);
56
57 QAction *action = NULL;
58 action = menu->addAction("Choose File");
59 connect(action, SIGNAL(triggered(bool)), this, SLOT(open(bool)));
60
61 vbox->addWidget(menu, 0);
62 vbox->addWidget(video, 1);
63
64 vbox->setMargin(0);
65 vbox->setSpacing(0);
66 resize(600, 480);
67
68 // Accept keyboard event
69 setFocusPolicy(Qt::StrongFocus);
70
71}
72
74{
75}
76
77void PlayerDemo::closeEvent(QCloseEvent *event)
78{
79 // Close window, stop player, and quit
80 QWidget *pWin = QApplication::activeWindow();
81 pWin->hide();
82 player->Stop();
83 QApplication::quit();
84}
85
86void PlayerDemo::keyPressEvent(QKeyEvent *event)
87{
88 if (event->key() == Qt::Key_Space || event->key() == Qt::Key_K) {
89
90 if (player->Mode() == openshot::PLAYBACK_PAUSED)
91 {
92 // paused, so start playing again
93 player->Play();
94
95 }
96 else if (player->Mode() == openshot::PLAYBACK_PLAY)
97 {
98
99 if (player->Speed() == 0)
100 // already playing, but speed is zero... so just speed up to normal
101 player->Speed(1);
102 else
103 // already playing... so pause
104 player->Pause();
105
106 }
107
108 }
109 else if (event->key() == Qt::Key_J) {
110 if (player->Speed() - 1 != 0)
111 player->Speed(player->Speed() - 1);
112 else
113 player->Speed(player->Speed() - 2);
114
115 if (player->Mode() == openshot::PLAYBACK_PAUSED)
116 player->Play();
117 }
118 else if (event->key() == Qt::Key_L) {
119 if (player->Speed() + 1 != 0)
120 player->Speed(player->Speed() + 1);
121 else
122 player->Speed(player->Speed() + 2);
123
124 if (player->Mode() == openshot::PLAYBACK_PAUSED)
125 player->Play();
126
127 }
128 else if (event->key() == Qt::Key_Left) {
129 if (player->Speed() != 0)
130 player->Speed(0);
131 player->Seek(player->Position() - 1);
132 }
133 else if (event->key() == Qt::Key_Right) {
134 if (player->Speed() != 0)
135 player->Speed(0);
136 player->Seek(player->Position() + 1);
137 }
138 else if (event->key() == Qt::Key_Escape) {
139 QWidget *pWin = QApplication::activeWindow();
140 pWin->hide();
141
142 player->Stop();
143
144 QApplication::quit();
145 }
146
147 event->accept();
148 QWidget::keyPressEvent(event);
149}
150
151void PlayerDemo::open(bool checked)
152{
153 // Get filename of media files
154 const QString filename = QFileDialog::getOpenFileName(this, "Open Video File");
155 if (filename.isEmpty()) return;
156
157 // Create FFmpegReader and open file
158 player->SetSource(filename.toStdString());
159
160 // Set aspect ratio of widget
161 video->SetAspectRatio(player->Reader()->info.display_ratio, player->Reader()->info.pixel_ratio);
162
163 // Play video
164 player->Play();
165}
Header file for demo application for QtPlayer class.
void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE
Definition: PlayerDemo.cpp:86
void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE
Definition: PlayerDemo.cpp:77
PlayerDemo(QWidget *parent=0)
Definition: PlayerDemo.cpp:46
void SetAspectRatio(openshot::Fraction new_aspect_ratio, openshot::Fraction new_pixel_ratio)
void Seek(int64_t new_frame)
Seek to a specific frame in the player.
Definition: QtPlayer.cpp:155
void SetSource(const std::string &source)
Set the source URL/path of this player (which will create an internal Reader)
Definition: QtPlayer.cpp:88
void Reader(openshot::ReaderBase *new_reader)
Set the current reader.
Definition: QtPlayer.cpp:192
int64_t Position()
Get the current frame number being played.
Definition: QtPlayer.cpp:150
float Speed()
Get the Playback speed.
Definition: QtPlayer.cpp:218
void Play()
Play the video.
Definition: QtPlayer.cpp:120
openshot::PlaybackMode Mode()
Get the current mode.
Definition: QtPlayer.cpp:139
void Pause()
Pause the video.
Definition: QtPlayer.cpp:144
void Stop()
Stop the video player and clear the cached frames.
Definition: QtPlayer.cpp:173
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:47
@ PLAYBACK_PAUSED
Pause the video (holding the last displayed frame)
Definition: PlayerBase.h:47
@ PLAYBACK_PLAY
Play the video normally.
Definition: PlayerBase.h:46