OpenShot Library | libopenshot  0.3.3
PlayerDemo.cpp
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2019 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #include <string>
14 
15 #include "PlayerDemo.h"
16 #include "../QtPlayer.h"
17 
18 #include <QMessageBox>
19 #include <QFileDialog>
20 #include <QWidget>
21 #include <QBoxLayout>
22 #include <QMenuBar>
23 #include <QMenu>
24 #include <QKeyEvent>
25 #include <QCloseEvent>
26 #include <QApplication>
27 
28 PlayerDemo::PlayerDemo(QWidget *parent)
29  : QWidget(parent)
30  , vbox(new QVBoxLayout(this))
31  , menu(new QMenuBar(this))
32  , video(new VideoRenderWidget(this))
33  , player(new openshot::QtPlayer(video->GetRenderer()))
34 {
35  setWindowTitle("OpenShot Player");
36 
37  menu->setNativeMenuBar(false);
38 
39  QAction *action = NULL;
40  action = menu->addAction("Choose File");
41  connect(action, SIGNAL(triggered(bool)), this, SLOT(open(bool)));
42 
43  vbox->addWidget(menu, 0);
44  vbox->addWidget(video, 1);
45 
46  vbox->setMargin(0);
47  vbox->setSpacing(0);
48  resize(600, 480);
49 
50  // Accept keyboard event
51  setFocusPolicy(Qt::StrongFocus);
52 
53 }
54 
56 {
57 }
58 
59 void PlayerDemo::closeEvent(QCloseEvent *event)
60 {
61  // Close window, stop player, and quit
62  QWidget *pWin = QApplication::activeWindow();
63  pWin->hide();
64  player->Stop();
65  QApplication::quit();
66 }
67 
68 void PlayerDemo::keyPressEvent(QKeyEvent *event)
69 {
70  if (event->key() == Qt::Key_Space || event->key() == Qt::Key_K) {
71 
72  if (player->Mode() == openshot::PLAYBACK_PAUSED)
73  {
74  // paused, so start playing again
75  player->Play();
76 
77  }
78  else if (player->Mode() == openshot::PLAYBACK_PLAY)
79  {
80 
81  if (player->Speed() == 0)
82  // already playing, but speed is zero... so just speed up to normal
83  player->Speed(1);
84  else
85  // already playing... so pause
86  player->Pause();
87 
88  }
89 
90  }
91  else if (event->key() == Qt::Key_J) {
92  if (player->Speed() - 1 != 0)
93  player->Speed(player->Speed() - 1);
94  else
95  player->Speed(player->Speed() - 2);
96 
97  if (player->Mode() == openshot::PLAYBACK_PAUSED)
98  player->Play();
99  }
100  else if (event->key() == Qt::Key_L) {
101  if (player->Speed() + 1 != 0)
102  player->Speed(player->Speed() + 1);
103  else
104  player->Speed(player->Speed() + 2);
105 
106  if (player->Mode() == openshot::PLAYBACK_PAUSED)
107  player->Play();
108 
109  }
110  else if (event->key() == Qt::Key_Left) {
111  if (player->Speed() != 0)
112  player->Speed(0);
113  player->Seek(player->Position() - 1);
114  }
115  else if (event->key() == Qt::Key_Right) {
116  if (player->Speed() != 0)
117  player->Speed(0);
118  player->Seek(player->Position() + 1);
119  }
120  else if (event->key() == Qt::Key_Escape) {
121  QWidget *pWin = QApplication::activeWindow();
122  pWin->hide();
123 
124  player->Stop();
125 
126  QApplication::quit();
127  }
128 
129  event->accept();
130  QWidget::keyPressEvent(event);
131 }
132 
133 void PlayerDemo::open(bool checked)
134 {
135  // Get filename of media files
136  const QString filename = QFileDialog::getOpenFileName(this, "Open Video File");
137  if (filename.isEmpty()) return;
138 
139  // Open *.osp file (read JSON into variable)
140  QString project_json = "";
141  if (filename.endsWith(".osp")) {
142  QFile file(filename);
143  if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
144  return;
145  while (!file.atEnd()) {
146  QByteArray line = file.readLine();
147  project_json += line;
148  }
149 
150  // Set source from project JSON
151  player->SetTimelineSource(project_json.toStdString());
152  } else {
153  // Set source from filepath
154  player->SetSource(filename.toStdString());
155  }
156 
157  // Set aspect ratio of widget
158  video->SetAspectRatio(player->Reader()->info.display_ratio, player->Reader()->info.pixel_ratio);
159 
160  // Play video
161  player->Play();
162 }
PlayerDemo::keyPressEvent
void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE
Definition: PlayerDemo.cpp:68
openshot::PLAYBACK_PLAY
@ PLAYBACK_PLAY
Play the video normally.
Definition: PlayerBase.h:28
openshot::QtPlayer::Seek
void Seek(int64_t new_frame)
Seek to a specific frame in the player.
Definition: QtPlayer.cpp:166
PlayerDemo.h
Header file for demo application for QtPlayer class.
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
VideoRenderWidget
Definition: VideoRenderWidget.h:24
openshot::QtPlayer::Reader
void Reader(openshot::ReaderBase *new_reader)
Set the current reader.
Definition: QtPlayer.cpp:200
openshot::QtPlayer::Speed
float Speed()
Get the Playback speed.
Definition: QtPlayer.cpp:226
PlayerDemo::~PlayerDemo
~PlayerDemo()
Definition: PlayerDemo.cpp:55
openshot::QtPlayer::Mode
openshot::PlaybackMode Mode()
Get the current mode.
Definition: QtPlayer.cpp:150
openshot::QtPlayer::Position
int64_t Position()
Get the current frame number being played.
Definition: QtPlayer.cpp:161
openshot::QtPlayer::Pause
void Pause()
Pause the video.
Definition: QtPlayer.cpp:155
openshot::QtPlayer::SetTimelineSource
void SetTimelineSource(const std::string &json)
Set the source JSON of an openshot::Timelime.
Definition: QtPlayer.cpp:86
PlayerDemo::PlayerDemo
PlayerDemo(QWidget *parent=0)
Definition: PlayerDemo.cpp:28
VideoRenderWidget::SetAspectRatio
void SetAspectRatio(openshot::Fraction new_aspect_ratio, openshot::Fraction new_pixel_ratio)
Definition: VideoRenderWidget.cpp:49
openshot::PLAYBACK_PAUSED
@ PLAYBACK_PAUSED
Pause the video (holding the last displayed frame)
Definition: PlayerBase.h:29
openshot::QtPlayer::Play
void Play()
Play the video.
Definition: QtPlayer.cpp:131
PlayerDemo::closeEvent
void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE
Definition: PlayerDemo.cpp:59
openshot::QtPlayer::Stop
void Stop()
Stop the video player and clear the cached frames.
Definition: QtPlayer.cpp:181
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:99