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