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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
** $QT_END_LICENSE$
**
***************************************************************************/
#include <QtGui>
#include "mainwindow.h"
//![0]
MainWindow::MainWindow()
{
audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
mediaObject = new Phonon::MediaObject(this);
metaInformationResolver = new Phonon::MediaObject(this);
mediaObject->setTickInterval(1000);
//![0]
//![2]
connect(mediaObject, SIGNAL(tick(qint64)), this, SLOT(tick(qint64)));
connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
this, SLOT(stateChanged(Phonon::State, Phonon::State)));
connect(metaInformationResolver, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
this, SLOT(metaStateChanged(Phonon::State, Phonon::State)));
connect(mediaObject, SIGNAL(currentSourceChanged(const Phonon::MediaSource &)),
this, SLOT(sourceChanged(const Phonon::MediaSource &)));
connect(mediaObject, SIGNAL(aboutToFinish()), this, SLOT(aboutToFinish()));
//![2]
//![1]
Phonon::createPath(mediaObject, audioOutput);
//![1]
setupActions();
setupMenus();
setupUi();
timeLcd->display("00:00");
}
//![6]
void MainWindow::addFiles()
{
QStringList files = QFileDialog::getOpenFileNames(this, tr("Select Music Files"),
QDesktopServices::storageLocation(QDesktopServices::MusicLocation));
if (files.isEmpty())
return;
int index = sources.size();
foreach (QString string, files) {
Phonon::MediaSource source(string);
sources.append(source);
}
if (!sources.isEmpty())
metaInformationResolver->setCurrentSource(sources.at(index));
}
//![6]
void MainWindow::about()
{
QMessageBox::information(this, tr("About Music Player"),
tr("The Music Player example shows how to use Phonon - the multimedia"
" framework that comes with Qt - to create a simple music player."));
}
//![9]
void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState */)
{
switch (newState) {
case Phonon::ErrorState:
if (mediaObject->errorType() == Phonon::FatalError) {
QMessageBox::warning(this, tr("Fatal Error"),
mediaObject->errorString());
} else {
QMessageBox::warning(this, tr("Error"),
mediaObject->errorString());
}
break;
//![9]
//![10]
case Phonon::PlayingState:
playAction->setEnabled(false);
pauseAction->setEnabled(true);
stopAction->setEnabled(true);
break;
case Phonon::StoppedState:
stopAction->setEnabled(false);
playAction->setEnabled(true);
pauseAction->setEnabled(false);
timeLcd->display("00:00");
break;
case Phonon::PausedState:
pauseAction->setEnabled(false);
stopAction->setEnabled(true);
playAction->setEnabled(true);
break;
//![10]
case Phonon::BufferingState:
break;
default:
;
}
}
//![11]
void MainWindow::tick(qint64 time)
{
QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
timeLcd->display(displayTime.toString("mm:ss"));
}
//![11]
//![12]
void MainWindow::tableClicked(int row, int /* column */)
{
bool wasPlaying = mediaObject->state() == Phonon::PlayingState;
mediaObject->stop();
mediaObject->clearQueue();
if (row >= sources.size())
return;
mediaObject->setCurrentSource(sources[row]);
if (wasPlaying)
mediaObject->play();
else
mediaObject->stop();
}
//![12]
//![13]
void MainWindow::sourceChanged(const Phonon::MediaSource &source)
{
musicTable->selectRow(sources.indexOf(source));
timeLcd->display("00:00");
}
//![13]
//![14]
void MainWindow::metaStateChanged(Phonon::State newState, Phonon::State /* oldState */)
{
if (newState == Phonon::ErrorState) {
QMessageBox::warning(this, tr("Error opening files"),
metaInformationResolver->errorString());
while (!sources.isEmpty() &&
!(sources.takeLast() == metaInformationResolver->currentSource())) {} /* loop */;
return;
}
if (newState != Phonon::StoppedState && newState != Phonon::PausedState)
return;
if (metaInformationResolver->currentSource().type() == Phonon::MediaSource::Invalid)
return;
QMap<QString, QString> metaData = metaInformationResolver->metaData();
QString title = metaData.value("TITLE");
if (title == "")
title = metaInformationResolver->currentSource().fileName();
QTableWidgetItem *titleItem = new QTableWidgetItem(title);
titleItem->setFlags(titleItem->flags() ^ Qt::ItemIsEditable);
QTableWidgetItem *artistItem = new QTableWidgetItem(metaData.value("ARTIST"));
artistItem->setFlags(artistItem->flags() ^ Qt::ItemIsEditable);
QTableWidgetItem *albumItem = new QTableWidgetItem(metaData.value("ALBUM"));
albumItem->setFlags(albumItem->flags() ^ Qt::ItemIsEditable);
QTableWidgetItem *yearItem = new QTableWidgetItem(metaData.value("DATE"));
yearItem->setFlags(yearItem->flags() ^ Qt::ItemIsEditable);
//![14]
int currentRow = musicTable->rowCount();
musicTable->insertRow(currentRow);
musicTable->setItem(currentRow, 0, titleItem);
musicTable->setItem(currentRow, 1, artistItem);
musicTable->setItem(currentRow, 2, albumItem);
musicTable->setItem(currentRow, 3, yearItem);
//![15]
if (musicTable->selectedItems().isEmpty()) {
musicTable->selectRow(0);
mediaObject->setCurrentSource(metaInformationResolver->currentSource());
}
Phonon::MediaSource source = metaInformationResolver->currentSource();
int index = sources.indexOf(metaInformationResolver->currentSource()) + 1;
if (sources.size() > index) {
metaInformationResolver->setCurrentSource(sources.at(index));
}
else {
musicTable->resizeColumnsToContents();
if (musicTable->columnWidth(0) > 300)
musicTable->setColumnWidth(0, 300);
}
}
//![15]
//![16]
void MainWindow::aboutToFinish()
{
int index = sources.indexOf(mediaObject->currentSource()) + 1;
if (sources.size() > index) {
mediaObject->enqueue(sources.at(index));
}
}
//![16]
void MainWindow::setupActions()
{
playAction = new QAction(style()->standardIcon(QStyle::SP_MediaPlay), tr("Play"), this);
playAction->setShortcut(tr("Crl+P"));
playAction->setDisabled(true);
pauseAction = new QAction(style()->standardIcon(QStyle::SP_MediaPause), tr("Pause"), this);
pauseAction->setShortcut(tr("Ctrl+A"));
pauseAction->setDisabled(true);
stopAction = new QAction(style()->standardIcon(QStyle::SP_MediaStop), tr("Stop"), this);
stopAction->setShortcut(tr("Ctrl+S"));
stopAction->setDisabled(true);
nextAction = new QAction(style()->standardIcon(QStyle::SP_MediaSkipForward), tr("Next"), this);
nextAction->setShortcut(tr("Ctrl+N"));
previousAction = new QAction(style()->standardIcon(QStyle::SP_MediaSkipBackward), tr("Previous"), this);
previousAction->setShortcut(tr("Ctrl+R"));
addFilesAction = new QAction(tr("Add &Files"), this);
addFilesAction->setShortcut(tr("Ctrl+F"));
exitAction = new QAction(tr("E&xit"), this);
exitAction->setShortcuts(QKeySequence::Quit);
aboutAction = new QAction(tr("A&bout"), this);
aboutAction->setShortcut(tr("Ctrl+B"));
aboutQtAction = new QAction(tr("About &Qt"), this);
aboutQtAction->setShortcut(tr("Ctrl+Q"));
//![5]
connect(playAction, SIGNAL(triggered()), mediaObject, SLOT(play()));
connect(pauseAction, SIGNAL(triggered()), mediaObject, SLOT(pause()) );
connect(stopAction, SIGNAL(triggered()), mediaObject, SLOT(stop()));
//![5]
connect(addFilesAction, SIGNAL(triggered()), this, SLOT(addFiles()));
connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
}
void MainWindow::setupMenus()
{
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(addFilesAction);
fileMenu->addSeparator();
fileMenu->addAction(exitAction);
QMenu *aboutMenu = menuBar()->addMenu(tr("&Help"));
aboutMenu->addAction(aboutAction);
aboutMenu->addAction(aboutQtAction);
}
//![3]
void MainWindow::setupUi()
{
//![3]
QToolBar *bar = new QToolBar;
bar->addAction(playAction);
bar->addAction(pauseAction);
bar->addAction(stopAction);
//![4]
seekSlider = new Phonon::SeekSlider(this);
seekSlider->setMediaObject(mediaObject);
volumeSlider = new Phonon::VolumeSlider(this);
volumeSlider->setAudioOutput(audioOutput);
//![4]
volumeSlider->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
QLabel *volumeLabel = new QLabel;
volumeLabel->setPixmap(QPixmap("images/volume.png"));
QPalette palette;
palette.setBrush(QPalette::Light, Qt::darkGray);
timeLcd = new QLCDNumber;
timeLcd->setPalette(palette);
QStringList headers;
headers << tr("Title") << tr("Artist") << tr("Album") << tr("Year");
musicTable = new QTableWidget(0, 4);
musicTable->setHorizontalHeaderLabels(headers);
musicTable->setSelectionMode(QAbstractItemView::SingleSelection);
musicTable->setSelectionBehavior(QAbstractItemView::SelectRows);
connect(musicTable, SIGNAL(cellPressed(int, int)),
this, SLOT(tableClicked(int, int)));
QHBoxLayout *seekerLayout = new QHBoxLayout;
seekerLayout->addWidget(seekSlider);
seekerLayout->addWidget(timeLcd);
QHBoxLayout *playbackLayout = new QHBoxLayout;
playbackLayout->addWidget(bar);
playbackLayout->addStretch();
playbackLayout->addWidget(volumeLabel);
playbackLayout->addWidget(volumeSlider);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(musicTable);
mainLayout->addLayout(seekerLayout);
mainLayout->addLayout(playbackLayout);
QWidget *widget = new QWidget;
widget->setLayout(mainLayout);
setCentralWidget(widget);
setWindowTitle("Phonon Music Player");
}
|