OpenShot Library | libopenshot  0.2.5
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 "stdio.h"
32 #include "../../include/QtPlayer.h"
33 #include "../../include/Qt/PlayerDemo.h"
34 #include <QMessageBox>
35 #include <QFileDialog>
36 
37 PlayerDemo::PlayerDemo(QWidget *parent)
38  : QWidget(parent)
39  , vbox(new QVBoxLayout(this))
40  , menu(new QMenuBar(this))
41  , video(new VideoRenderWidget(this))
42  , player(new openshot::QtPlayer(video->GetRenderer()))
43 {
44  setWindowTitle("OpenShot Player");
45 
46  menu->setNativeMenuBar(false);
47 
48  QAction *action = NULL;
49  action = menu->addAction("Choose File");
50  connect(action, SIGNAL(triggered(bool)), this, SLOT(open(bool)));
51 
52  vbox->addWidget(menu, 0);
53  vbox->addWidget(video, 1);
54 
55  vbox->setMargin(0);
56  vbox->setSpacing(0);
57  resize(600, 480);
58 
59  // Accept keyboard event
60  setFocusPolicy(Qt::StrongFocus);
61 
62 }
63 
65 {
66 }
67 
68 void PlayerDemo::closeEvent(QCloseEvent *event)
69 {
70  // Close window, stop player, and quit
71  QWidget *pWin = QApplication::activeWindow();
72  pWin->hide();
73  player->Stop();
74  QApplication::quit();
75 }
76 
77 void PlayerDemo::keyPressEvent(QKeyEvent *event)
78 {
79  if (event->key() == Qt::Key_Space || event->key() == Qt::Key_K) {
80 
81  if (player->Mode() == openshot::PLAYBACK_PAUSED)
82  {
83  // paused, so start playing again
84  player->Play();
85 
86  }
87  else if (player->Mode() == openshot::PLAYBACK_PLAY)
88  {
89 
90  if (player->Speed() == 0)
91  // already playing, but speed is zero... so just speed up to normal
92  player->Speed(1);
93  else
94  // already playing... so pause
95  player->Pause();
96 
97  }
98 
99  }
100  else if (event->key() == Qt::Key_J) {
101  std::cout << "BACKWARD" << player->Speed() - 1 << std::endl;
102  if (player->Speed() - 1 != 0)
103  player->Speed(player->Speed() - 1);
104  else
105  player->Speed(player->Speed() - 2);
106 
107  if (player->Mode() == openshot::PLAYBACK_PAUSED)
108  player->Play();
109  }
110  else if (event->key() == Qt::Key_L) {
111  std::cout << "FORWARD" << player->Speed() + 1 << std::endl;
112  if (player->Speed() + 1 != 0)
113  player->Speed(player->Speed() + 1);
114  else
115  player->Speed(player->Speed() + 2);
116 
117  if (player->Mode() == openshot::PLAYBACK_PAUSED)
118  player->Play();
119 
120  }
121  else if (event->key() == Qt::Key_Left) {
122  std::cout << "FRAME STEP -1" << std::endl;
123  if (player->Speed() != 0)
124  player->Speed(0);
125  player->Seek(player->Position() - 1);
126  }
127  else if (event->key() == Qt::Key_Right) {
128  std::cout << "FRAME STEP +1" << std::endl;
129  if (player->Speed() != 0)
130  player->Speed(0);
131  player->Seek(player->Position() + 1);
132  }
133  else if (event->key() == Qt::Key_Escape) {
134  std::cout << "QUIT PLAYER" << std::endl;
135  QWidget *pWin = QApplication::activeWindow();
136  pWin->hide();
137 
138  player->Stop();
139 
140  QApplication::quit();
141  }
142 
143  event->accept();
144  QWidget::keyPressEvent(event);
145 }
146 
147 void PlayerDemo::open(bool checked)
148 {
149  // Get filename of media files
150  const QString filename = QFileDialog::getOpenFileName(this, "Open Video File");
151  if (filename.isEmpty()) return;
152 
153  // Create FFmpegReader and open file
154  player->SetSource(filename.toStdString());
155 
156  // Set aspect ratio of widget
157  video->SetAspectRatio(player->Reader()->info.display_ratio, player->Reader()->info.pixel_ratio);
158 
159  // Play video
160  player->Play();
161 }
PlayerDemo::keyPressEvent
void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE
Definition: PlayerDemo.cpp:77
openshot::PLAYBACK_PLAY
@ PLAYBACK_PLAY
Play the video normally.
Definition: PlayerBase.h:46
openshot::QtPlayer::Seek
void Seek(int64_t new_frame)
Seek to a specific frame in the player.
Definition: QtPlayer.cpp:139
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AudioBufferSource.h:39
VideoRenderWidget
Definition: VideoRenderWidget.h:41
openshot::QtPlayer::Reader
void Reader(openshot::ReaderBase *new_reader)
Set the current reader.
Definition: QtPlayer.cpp:176
openshot::QtPlayer::Speed
float Speed()
Get the Playback speed.
Definition: QtPlayer.cpp:202
PlayerDemo::~PlayerDemo
~PlayerDemo()
Definition: PlayerDemo.cpp:64
openshot::QtPlayer::Mode
openshot::PlaybackMode Mode()
Get the current mode.
Definition: QtPlayer.cpp:123
openshot::QtPlayer::Position
int64_t Position()
Get the current frame number being played.
Definition: QtPlayer.cpp:134
openshot::QtPlayer::Pause
void Pause()
Pause the video.
Definition: QtPlayer.cpp:128
PlayerDemo::PlayerDemo
PlayerDemo(QWidget *parent=0)
Definition: PlayerDemo.cpp:37
VideoRenderWidget::SetAspectRatio
void SetAspectRatio(openshot::Fraction new_aspect_ratio, openshot::Fraction new_pixel_ratio)
Definition: VideoRenderWidget.cpp:61
openshot::PLAYBACK_PAUSED
@ PLAYBACK_PAUSED
Pause the video (holding the last displayed frame)
Definition: PlayerBase.h:47
openshot::QtPlayer::Play
void Play()
Play the video.
Definition: QtPlayer.cpp:104
PlayerDemo::closeEvent
void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE
Definition: PlayerDemo.cpp:68
openshot::QtPlayer::Stop
void Stop()
Stop the video player and clear the cached frames.
Definition: QtPlayer.cpp:157
openshot::QtPlayer::SetSource
void SetSource(const std::string &source)
Set the source URL/path of this player (which will create an internal Reader)
Definition: QtPlayer.cpp:88