forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayerwindow.cpp
109 lines (94 loc) · 3.64 KB
/
playerwindow.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
/******************************************************************************
Simple Player: this file is part of QtAV examples
Copyright (C) 2014-2015 Wang Bin <[email protected]>
* This file is part of QtAV
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "playerwindow.h"
#include <QPushButton>
#include <QSlider>
#include <QLabel>
#include <QLayout>
#include <QMessageBox>
#include <QFileDialog>
#include <QtAVWidgets>
using namespace QtAV;
PlayerWindow::PlayerWindow(QWidget *parent) : QWidget(parent)
{
QtAV::Widgets::registerRenderers();
setWindowTitle(QString::fromLatin1("QtAV simple player example"));
m_player = new AVPlayer(this);
QVBoxLayout *vl = new QVBoxLayout();
setLayout(vl);
m_vo = new VideoOutput(this);
if (!m_vo->widget()) {
QMessageBox::warning(0, QString::fromLatin1("QtAV error"), QString::fromLatin1("Can not create video renderer"));
return;
}
m_player->setRenderer(m_vo);
vl->addWidget(m_vo->widget());
m_slider = new QSlider();
m_slider->setOrientation(Qt::Horizontal);
connect(m_slider, SIGNAL(sliderMoved(int)), SLOT(seek(int)));
connect(m_player, SIGNAL(positionChanged(qint64)), SLOT(updateSlider()));
connect(m_player, SIGNAL(started()), SLOT(updateSlider()));
vl->addWidget(m_slider);
QHBoxLayout *hb = new QHBoxLayout();
vl->addLayout(hb);
m_openBtn = new QPushButton(tr("Open"));
m_captureBtn = new QPushButton(tr("Capture video frame"));
hb->addWidget(m_openBtn);
hb->addWidget(m_captureBtn);
m_preview = new QLabel(tr("Capture preview"));
m_preview->setFixedSize(200, 150);
hb->addWidget(m_preview);
connect(m_openBtn, SIGNAL(clicked()), SLOT(openMedia()));
connect(m_captureBtn, SIGNAL(clicked()), SLOT(capture()));
connect(m_player->videoCapture(), SIGNAL(imageCaptured(QImage)), SLOT(updatePreview(QImage)));
connect(m_player->videoCapture(), SIGNAL(saved(QString)), SLOT(onCaptureSaved(QString)));
connect(m_player->videoCapture(), SIGNAL(failed()), SLOT(onCaptureError()));
}
void PlayerWindow::openMedia()
{
QString file = QFileDialog::getOpenFileName(0, tr( "Open a video"));
if (file.isEmpty())
return;
m_player->play(file);
}
void PlayerWindow::seek(int pos)
{
if (!m_player->isPlaying())
return;
m_player->seek(pos*1000LL); // to msecs
}
void PlayerWindow::updateSlider()
{
m_slider->setRange(0, int(m_player->duration()/1000LL));
m_slider->setValue(int(m_player->position()/1000LL));
}
void PlayerWindow::updatePreview(const QImage &image)
{
m_preview->setPixmap(QPixmap::fromImage(image).scaled(m_preview->size()));
}
void PlayerWindow::capture()
{
//m_player->captureVideo();
m_player->videoCapture()->capture();
}
void PlayerWindow::onCaptureSaved(const QString &path)
{
setWindowTitle(tr("saved to: ") + path);
}
void PlayerWindow::onCaptureError()
{
QMessageBox::warning(0, QString::fromLatin1("QtAV video capture"), tr("Failed to capture video frame"));
}