-
Notifications
You must be signed in to change notification settings - Fork 4
/
custommediaplayer.cpp
315 lines (254 loc) · 10 KB
/
custommediaplayer.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#if HAVE_QT5
#include <QtWidgets>
#endif
#include <QtGui>
#define SLIDER_RANGE 8
#include "custommediaplayer.h"
#include "mainwindow.h"
CustomMediaPlayer::CustomMediaPlayer(QWidget *parent) :
playButton(0), nextEffect(0),
// m_AudioOutput(Phonon::VideoCategory),
m_videoWidget(new Phonon::VideoWidget(this))
{
QSize buttonSize(34, 28);
rewindButton = new QPushButton(this);
rewindButton->setIcon(style()->standardIcon(QStyle::SP_MediaSkipBackward));
playButton = new QPushButton(this);
playIcon = style()->standardIcon(QStyle::SP_MediaPlay);
pauseIcon = style()->standardIcon(QStyle::SP_MediaPause);
playButton->setIcon(playIcon);
slider = new Phonon::SeekSlider(this);
slider->setMediaObject(&m_MediaObject);
setStyleSheet(" QSlider::handle { image: url(:/resources/images/slider-dot.png)}"
" QSlider::groove { min-height: 80px; };");
// volume = new Phonon::VolumeSlider(&m_AudioOutput);
QVBoxLayout *vLayout = new QVBoxLayout(this);
vLayout->setContentsMargins(8, 8, 8, 8);
QHBoxLayout *layout = new QHBoxLayout();
info = new QLabel(this);
info->setMinimumHeight(70);
info->setAcceptDrops(false);
info->setMargin(2);
info->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
info->setLineWidth(2);
info->setAutoFillBackground(true);
QPalette palette;
palette.setBrush(QPalette::WindowText, Qt::white);
rewindButton->setMinimumSize(buttonSize);
playButton->setMinimumSize(buttonSize);
info->setStyleSheet("border-image:url(:/images/screen.png) ; border-width:3px");
info->setPalette(palette);
info->setText(tr("<center>No media</center>"));
// volume->setFixedWidth(120);
layout->addWidget(rewindButton);
layout->addWidget(playButton);
layout->addStretch();
// layout->addWidget(volume);
vLayout->addWidget(info);
initVideoWindow();
vLayout->addWidget(&m_videoWindow);
m_videoWindow.setMaximumSize(0.6* this->size().width(), 0.9* this->size().height());
QVBoxLayout *buttonPanelLayout = new QVBoxLayout();
m_videoWindow.hide();
buttonPanelLayout->addLayout(layout);
timeLabel = new QLabel(this);
progressLabel = new QLabel(this);
timeLabel->setPalette(palette);
QWidget *sliderPanel = new QWidget(this);
QHBoxLayout *sliderLayout = new QHBoxLayout();
sliderLayout->addWidget(slider);
sliderLayout->addWidget(timeLabel);
sliderLayout->addWidget(progressLabel);
sliderLayout->setContentsMargins(0, 0, 0, 0);
sliderPanel->setLayout(sliderLayout);
buttonPanelLayout->addWidget(sliderPanel);
buttonPanelLayout->setContentsMargins(0, 0, 0, 0);
QWidget *buttonPanelWidget = new QWidget(this);
buttonPanelWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
buttonPanelWidget->setLayout(buttonPanelLayout);
vLayout->addWidget(buttonPanelWidget);
QHBoxLayout *labelLayout = new QHBoxLayout();
vLayout->addLayout(labelLayout);
setLayout(vLayout);
// Setup signal connections:
connect(rewindButton, SIGNAL(clicked()), this, SLOT(rewind()));
//connect(openButton, SIGNAL(clicked()), this, SLOT(openFile()));
// openButton->setMenu(fileMenu);
connect(playButton, SIGNAL(clicked()), this, SLOT(playPause()));
//connect(openButton, SIGNAL(clicked()), this, SLOT(openFile()));
// connect(m_videoWidget, SIGNAL(customContextMenuRequested(const QPoint &)), SLOT(showContextMenu(const QPoint &)));
// connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), SLOT(showContextMenu(const QPoint &)));
// connect(&m_MediaObject, SIGNAL(metaDataChanged()), this, SLOT(updateInfo()));
connect(&m_MediaObject, SIGNAL(totalTimeChanged(qint64)), this, SLOT(updateTime()));
connect(&m_MediaObject, SIGNAL(tick(qint64)), this, SLOT(updateTime()));
connect(&m_MediaObject, SIGNAL(finished()), this, SLOT(finished()));
connect(&m_MediaObject, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(stateChanged(Phonon::State,Phonon::State)));
connect(&m_MediaObject, SIGNAL(bufferStatus(int)), this, SLOT(bufferStatus(int)));
connect(&m_MediaObject, SIGNAL(hasVideoChanged(bool)), this, SLOT(hasVideoChanged(bool)));
rewindButton->setEnabled(false);
playButton->setEnabled(false);
setAcceptDrops(true);
setFile("big_buck_bunny_480x272_stereo.ogg");
// m_audioOutputPath = Phonon::createPath(&m_MediaObject, &m_AudioOutput);
Phonon::createPath(&m_MediaObject, m_videoWidget);
resize(minimumSizeHint());
}
void CustomMediaPlayer::stateChanged(Phonon::State newstate, Phonon::State oldstate)
{
Q_UNUSED(oldstate);
if (oldstate == Phonon::LoadingState) {
QRect videoHintRect = QRect(QPoint(0, 0), m_videoWindow.sizeHint());
QRect newVideoRect = QApplication::desktop()->screenGeometry().intersected(videoHintRect);
if (!m_smallScreen) {
if (m_MediaObject.hasVideo()) {
// Flush event que so that sizeHint takes the
// recently shown/hidden m_videoWindow into account:
qApp->processEvents();
resize(sizeHint());
} else
resize(minimumSize());
}
}
switch (newstate) {
case Phonon::ErrorState:
if (m_MediaObject.errorType() == Phonon::FatalError) {
playButton->setEnabled(false);
rewindButton->setEnabled(false);
} else {
m_MediaObject.pause();
}
QMessageBox::warning(this, "Phonon Mediaplayer", m_MediaObject.errorString(), QMessageBox::Close);
break;
case Phonon::StoppedState:
m_videoWidget->setFullScreen(false);
// Fall through
case Phonon::PausedState:
playButton->setIcon(playIcon);
if (m_MediaObject.currentSource().type() != Phonon::MediaSource::Invalid){
playButton->setEnabled(true);
rewindButton->setEnabled(true);
} else {
playButton->setEnabled(false);
rewindButton->setEnabled(false);
}
break;
case Phonon::PlayingState:
playButton->setEnabled(true);
playButton->setIcon(pauseIcon);
if (m_MediaObject.hasVideo())
m_videoWindow.show();
// Fall through
case Phonon::BufferingState:
rewindButton->setEnabled(true);
break;
case Phonon::LoadingState:
rewindButton->setEnabled(false);
break;
}
}
void CustomMediaPlayer::setVolume(qreal volume)
{
// m_AudioOutput.setVolume(volume);
}
void CustomMediaPlayer::initVideoWindow()
{
QVBoxLayout *videoLayout = new QVBoxLayout();
videoLayout->addWidget(m_videoWidget);
videoLayout->setContentsMargins(0, 0, 0, 0);
m_videoWindow.setLayout(videoLayout);
m_videoWindow.setMinimumSize(100, 100);
}
void CustomMediaPlayer::playPause()
{
if (m_MediaObject.state() == Phonon::PlayingState)
m_MediaObject.pause();
else {
if (m_MediaObject.currentTime() == m_MediaObject.totalTime())
m_MediaObject.seek(0);
m_MediaObject.play();
}
}
void CustomMediaPlayer::setFile(const QString &fileName)
{
m_MediaObject.setCurrentSource(Phonon::MediaSource(fileName));
m_MediaObject.play();
}
bool CustomMediaPlayer::playPauseForDialog()
{
// If we're running on a small screen, we want to pause the video when
// popping up dialogs. We neither want to tamper with the state if the
// user has paused.
if (m_smallScreen && m_MediaObject.hasVideo()) {
if (Phonon::PlayingState == m_MediaObject.state()) {
m_MediaObject.pause();
return true;
}
}
return false;
}
void CustomMediaPlayer::bufferStatus(int percent)
{
if (percent == 100)
progressLabel->setText(QString());
else {
QString str = QString::fromLatin1("(%1%)").arg(percent);
progressLabel->setText(str);
}
}
void CustomMediaPlayer::updateTime()
{
long len = m_MediaObject.totalTime();
long pos = m_MediaObject.currentTime();
QString timeString;
if (pos || len)
{
int sec = pos/1000;
int min = sec/60;
int hour = min/60;
int msec = pos;
QTime playTime(hour%60, min%60, sec%60, msec%1000);
sec = len / 1000;
min = sec / 60;
hour = min / 60;
msec = len;
QTime stopTime(hour%60, min%60, sec%60, msec%1000);
QString timeFormat = "m:ss";
if (hour > 0)
timeFormat = "h:mm:ss";
timeString = playTime.toString(timeFormat);
if (len)
timeString += " / " + stopTime.toString(timeFormat);
}
timeLabel->setText(timeString);
}
void CustomMediaPlayer::rewind()
{
m_MediaObject.seek(0);
}
void CustomMediaPlayer::finished()
{
}
void CustomMediaPlayer::scaleChanged(QAction *act)
{
if (act->text() == tr("Scale and crop"))
m_videoWidget->setScaleMode(Phonon::VideoWidget::ScaleAndCrop);
else
m_videoWidget->setScaleMode(Phonon::VideoWidget::FitInView);
}
void CustomMediaPlayer::aspectChanged(QAction *act)
{
if (act->text() == tr("16/9"))
m_videoWidget->setAspectRatio(Phonon::VideoWidget::AspectRatio16_9);
else if (act->text() == tr("Scale"))
m_videoWidget->setAspectRatio(Phonon::VideoWidget::AspectRatioWidget);
else if (act->text() == tr("4/3"))
m_videoWidget->setAspectRatio(Phonon::VideoWidget::AspectRatio4_3);
else
m_videoWidget->setAspectRatio(Phonon::VideoWidget::AspectRatioAuto);
}
void CustomMediaPlayer::hasVideoChanged(bool bHasVideo)
{
info->setVisible(!bHasVideo);
m_videoWindow.setVisible(bHasVideo);
// m_fullScreenAction->setEnabled(bHasVideo);
}