0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the examples of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include "videoplayer.h"
|
|
43 |
|
|
44 |
#include "videowidget.h"
|
|
45 |
|
|
46 |
#include <QtMultimedia>
|
|
47 |
|
|
48 |
VideoPlayer::VideoPlayer(QWidget *parent)
|
|
49 |
: QWidget(parent)
|
|
50 |
, surface(0)
|
|
51 |
, playButton(0)
|
|
52 |
, positionSlider(0)
|
|
53 |
{
|
|
54 |
connect(&movie, SIGNAL(stateChanged(QMovie::MovieState)),
|
|
55 |
this, SLOT(movieStateChanged(QMovie::MovieState)));
|
|
56 |
connect(&movie, SIGNAL(frameChanged(int)),
|
|
57 |
this, SLOT(frameChanged(int)));
|
|
58 |
|
|
59 |
VideoWidget *videoWidget = new VideoWidget;
|
|
60 |
surface = videoWidget->videoSurface();
|
|
61 |
|
|
62 |
QAbstractButton *openButton = new QPushButton(tr("Open..."));
|
|
63 |
connect(openButton, SIGNAL(clicked()), this, SLOT(openFile()));
|
|
64 |
|
|
65 |
playButton = new QPushButton;
|
|
66 |
playButton->setEnabled(false);
|
|
67 |
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
|
|
68 |
|
|
69 |
connect(playButton, SIGNAL(clicked()),
|
|
70 |
this, SLOT(play()));
|
|
71 |
|
|
72 |
positionSlider = new QSlider(Qt::Horizontal);
|
|
73 |
positionSlider->setRange(0, 0);
|
|
74 |
|
|
75 |
connect(positionSlider, SIGNAL(sliderMoved(int)),
|
|
76 |
this, SLOT(setPosition(int)));
|
|
77 |
|
|
78 |
connect(&movie, SIGNAL(frameChanged(int)),
|
|
79 |
positionSlider, SLOT(setValue(int)));
|
|
80 |
|
|
81 |
QBoxLayout *controlLayout = new QHBoxLayout;
|
|
82 |
controlLayout->setMargin(0);
|
|
83 |
controlLayout->addWidget(openButton);
|
|
84 |
controlLayout->addWidget(playButton);
|
|
85 |
controlLayout->addWidget(positionSlider);
|
|
86 |
|
|
87 |
QBoxLayout *layout = new QVBoxLayout;
|
|
88 |
layout->addWidget(videoWidget);
|
|
89 |
layout->addLayout(controlLayout);
|
|
90 |
|
|
91 |
setLayout(layout);
|
|
92 |
}
|
|
93 |
|
|
94 |
VideoPlayer::~VideoPlayer()
|
|
95 |
{
|
|
96 |
}
|
|
97 |
|
|
98 |
void VideoPlayer::openFile()
|
|
99 |
{
|
|
100 |
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"));
|
|
101 |
|
|
102 |
if (!fileName.isEmpty()) {
|
|
103 |
if (surface->isStarted())
|
|
104 |
surface->stop();
|
|
105 |
|
|
106 |
movie.setFileName(fileName);
|
|
107 |
|
|
108 |
playButton->setEnabled(true);
|
|
109 |
positionSlider->setMaximum(movie.frameCount());
|
|
110 |
|
|
111 |
movie.jumpToFrame(0);
|
|
112 |
}
|
|
113 |
}
|
|
114 |
|
|
115 |
void VideoPlayer::play()
|
|
116 |
{
|
|
117 |
switch(movie.state()) {
|
|
118 |
case QMovie::NotRunning:
|
|
119 |
movie.start();
|
|
120 |
break;
|
|
121 |
case QMovie::Paused:
|
|
122 |
movie.setPaused(false);
|
|
123 |
break;
|
|
124 |
case QMovie::Running:
|
|
125 |
movie.setPaused(true);
|
|
126 |
break;
|
|
127 |
}
|
|
128 |
}
|
|
129 |
|
|
130 |
void VideoPlayer::movieStateChanged(QMovie::MovieState state)
|
|
131 |
{
|
|
132 |
switch(state) {
|
|
133 |
case QMovie::NotRunning:
|
|
134 |
case QMovie::Paused:
|
|
135 |
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
|
|
136 |
break;
|
|
137 |
case QMovie::Running:
|
|
138 |
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
|
|
139 |
break;
|
|
140 |
}
|
|
141 |
}
|
|
142 |
|
|
143 |
void VideoPlayer::frameChanged(int frame)
|
|
144 |
{
|
|
145 |
if (!presentImage(movie.currentImage())) {
|
|
146 |
movie.stop();
|
|
147 |
playButton->setEnabled(false);
|
|
148 |
positionSlider->setMaximum(0);
|
|
149 |
} else {
|
|
150 |
positionSlider->setValue(frame);
|
|
151 |
}
|
|
152 |
}
|
|
153 |
|
|
154 |
void VideoPlayer::setPosition(int frame)
|
|
155 |
{
|
|
156 |
movie.jumpToFrame(frame);
|
|
157 |
}
|
|
158 |
|
|
159 |
bool VideoPlayer::presentImage(const QImage &image)
|
|
160 |
{
|
|
161 |
QVideoFrame frame(image);
|
|
162 |
|
|
163 |
if (!frame.isValid())
|
|
164 |
return false;
|
|
165 |
|
|
166 |
QVideoSurfaceFormat currentFormat = surface->surfaceFormat();
|
|
167 |
|
|
168 |
if (frame.pixelFormat() != currentFormat.pixelFormat()
|
|
169 |
|| frame.size() != currentFormat.frameSize()) {
|
|
170 |
QVideoSurfaceFormat format(frame.size(), frame.pixelFormat());
|
|
171 |
|
|
172 |
if (!surface->start(format))
|
|
173 |
return false;
|
|
174 |
}
|
|
175 |
|
|
176 |
if (!surface->present(frame)) {
|
|
177 |
surface->stop();
|
|
178 |
|
|
179 |
return false;
|
|
180 |
} else {
|
|
181 |
return true;
|
|
182 |
}
|
|
183 |
}
|