1 /******************************************************************************
2     Simple Player:  this file is part of QtAV examples
3     Copyright (C) 2012-2016 Wang Bin <wbsecg1@gmail.com>
4 
5 *   This file is part of QtAV
6 
7     This program is free software: you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation, either version 3 of the License, or
10     (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 ******************************************************************************/
20 
21 #include "playerwindow.h"
22 #include <QPushButton>
23 #include <QSlider>
24 #include <QLayout>
25 #include <QMessageBox>
26 #include <QFileDialog>
27 
28 using namespace QtAV;
29 
PlayerWindow(QWidget * parent)30 PlayerWindow::PlayerWindow(QWidget *parent) : QWidget(parent)
31 {
32     m_unit = 1000;
33     setWindowTitle(QString::fromLatin1("QtAV simple player example"));
34     m_player = new AVPlayer(this);
35     QVBoxLayout *vl = new QVBoxLayout();
36     setLayout(vl);
37     m_vo = new VideoOutput(this);
38     if (!m_vo->widget()) {
39         QMessageBox::warning(0, QString::fromLatin1("QtAV error"), tr("Can not create video renderer"));
40         return;
41     }
42     m_player->setRenderer(m_vo);
43     vl->addWidget(m_vo->widget());
44     m_slider = new QSlider();
45     m_slider->setOrientation(Qt::Horizontal);
46     connect(m_slider, SIGNAL(sliderMoved(int)), SLOT(seekBySlider(int)));
47     connect(m_slider, SIGNAL(sliderPressed()), SLOT(seekBySlider()));
48     connect(m_player, SIGNAL(positionChanged(qint64)), SLOT(updateSlider(qint64)));
49     connect(m_player, SIGNAL(started()), SLOT(updateSlider()));
50     connect(m_player, SIGNAL(notifyIntervalChanged()), SLOT(updateSliderUnit()));
51 
52     vl->addWidget(m_slider);
53     QHBoxLayout *hb = new QHBoxLayout();
54     vl->addLayout(hb);
55     m_openBtn = new QPushButton(tr("Open"));
56     m_playBtn = new QPushButton(tr("Play/Pause"));
57     m_stopBtn = new QPushButton(tr("Stop"));
58     hb->addWidget(m_openBtn);
59     hb->addWidget(m_playBtn);
60     hb->addWidget(m_stopBtn);
61     connect(m_openBtn, SIGNAL(clicked()), SLOT(openMedia()));
62     connect(m_playBtn, SIGNAL(clicked()), SLOT(playPause()));
63     connect(m_stopBtn, SIGNAL(clicked()), m_player, SLOT(stop()));
64 }
65 
openMedia()66 void PlayerWindow::openMedia()
67 {
68     QString file = QFileDialog::getOpenFileName(0, tr("Open a video"));
69     if (file.isEmpty())
70         return;
71     m_player->play(file);
72 }
73 
seekBySlider(int value)74 void PlayerWindow::seekBySlider(int value)
75 {
76     if (!m_player->isPlaying())
77         return;
78     m_player->seek(qint64(value*m_unit));
79 }
80 
seekBySlider()81 void PlayerWindow::seekBySlider()
82 {
83     seekBySlider(m_slider->value());
84 }
85 
playPause()86 void PlayerWindow::playPause()
87 {
88     if (!m_player->isPlaying()) {
89         m_player->play();
90         return;
91     }
92     m_player->pause(!m_player->isPaused());
93 }
94 
updateSlider(qint64 value)95 void PlayerWindow::updateSlider(qint64 value)
96 {
97     m_slider->setRange(0, int(m_player->duration()/m_unit));
98     m_slider->setValue(int(value/m_unit));
99 }
100 
updateSlider()101 void PlayerWindow::updateSlider()
102 {
103     updateSlider(m_player->position());
104 }
105 
updateSliderUnit()106 void PlayerWindow::updateSliderUnit()
107 {
108     m_unit = m_player->notifyInterval();
109     updateSlider();
110 }
111