summaryrefslogtreecommitdiffstats
path: root/examples/phonon/qmusicplayer
diff options
context:
space:
mode:
authorQt by Nokia <qt-info@nokia.com>2011-04-27 12:05:43 +0200
committeraxis <qt-info@nokia.com>2011-04-27 12:05:43 +0200
commit356978ecce23e076e1b622d5d41dd8c04bf7bcf8 (patch)
tree8e1874cc32750e30b84b2561387d48424e076ae3 /examples/phonon/qmusicplayer
Initial import from the monolithic Qt.
This is the beginning of revision history for this module. If you want to look at revision history older than this, please refer to the Qt Git wiki for how to use Git history grafting. At the time of writing, this wiki is located here: http://qt.gitorious.org/qt/pages/GitIntroductionWithQt If you have already performed the grafting and you don't see any history beyond this commit, try running "git log" with the "--follow" argument. Branched from the monolithic repo, Qt master branch, at commit 896db169ea224deb96c59ce8af800d019de63f12
Diffstat (limited to 'examples/phonon/qmusicplayer')
-rw-r--r--examples/phonon/qmusicplayer/main.cpp56
-rw-r--r--examples/phonon/qmusicplayer/mainwindow.cpp354
-rw-r--r--examples/phonon/qmusicplayer/mainwindow.h111
-rw-r--r--examples/phonon/qmusicplayer/qmusicplayer.pro17
4 files changed, 538 insertions, 0 deletions
diff --git a/examples/phonon/qmusicplayer/main.cpp b/examples/phonon/qmusicplayer/main.cpp
new file mode 100644
index 0000000..708135f
--- /dev/null
+++ b/examples/phonon/qmusicplayer/main.cpp
@@ -0,0 +1,56 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+#include <QtGui>
+
+#include "mainwindow.h"
+
+//![1]
+int main(int argv, char **args)
+{
+ QApplication app(argv, args);
+ app.setApplicationName("Music Player");
+ app.setQuitOnLastWindowClosed(true);
+
+ MainWindow window;
+ window.show();
+
+ return app.exec();
+}
+//![1]
diff --git a/examples/phonon/qmusicplayer/mainwindow.cpp b/examples/phonon/qmusicplayer/mainwindow.cpp
new file mode 100644
index 0000000..b804a72
--- /dev/null
+++ b/examples/phonon/qmusicplayer/mainwindow.cpp
@@ -0,0 +1,354 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+** $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(Phonon::MediaSource)),
+ this, SLOT(sourceChanged(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("Ctrl+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");
+}
+
diff --git a/examples/phonon/qmusicplayer/mainwindow.h b/examples/phonon/qmusicplayer/mainwindow.h
new file mode 100644
index 0000000..a96f0c0
--- /dev/null
+++ b/examples/phonon/qmusicplayer/mainwindow.h
@@ -0,0 +1,111 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef MAINWINDOW_H
+#define MAINWINDOW_H
+
+#include <QMainWindow>
+#include <phonon/audiooutput.h>
+#include <phonon/seekslider.h>
+#include <phonon/mediaobject.h>
+#include <phonon/volumeslider.h>
+#include <phonon/backendcapabilities.h>
+#include <QList>
+
+QT_BEGIN_NAMESPACE
+class QAction;
+class QTableWidget;
+class QLCDNumber;
+QT_END_NAMESPACE
+
+//![0]
+
+class MainWindow : public QMainWindow
+{
+//![0]
+ Q_OBJECT
+
+public:
+ MainWindow();
+
+ QSize sizeHint() const {
+ return QSize(500, 300);
+ }
+
+private slots:
+ void addFiles();
+ void about();
+//![1]
+ void stateChanged(Phonon::State newState, Phonon::State oldState);
+ void tick(qint64 time);
+ void sourceChanged(const Phonon::MediaSource &source);
+ void metaStateChanged(Phonon::State newState, Phonon::State oldState);
+ void aboutToFinish();
+ void tableClicked(int row, int column);
+//![1]
+
+private:
+ void setupActions();
+ void setupMenus();
+ void setupUi();
+
+//![2]
+ Phonon::SeekSlider *seekSlider;
+ Phonon::MediaObject *mediaObject;
+ Phonon::MediaObject *metaInformationResolver;
+ Phonon::AudioOutput *audioOutput;
+ Phonon::VolumeSlider *volumeSlider;
+ QList<Phonon::MediaSource> sources;
+//![2]
+
+ QAction *playAction;
+ QAction *pauseAction;
+ QAction *stopAction;
+ QAction *nextAction;
+ QAction *previousAction;
+ QAction *addFilesAction;
+ QAction *exitAction;
+ QAction *aboutAction;
+ QAction *aboutQtAction;
+ QLCDNumber *timeLcd;
+ QTableWidget *musicTable;
+};
+
+#endif
diff --git a/examples/phonon/qmusicplayer/qmusicplayer.pro b/examples/phonon/qmusicplayer/qmusicplayer.pro
new file mode 100644
index 0000000..25ab7eb
--- /dev/null
+++ b/examples/phonon/qmusicplayer/qmusicplayer.pro
@@ -0,0 +1,17 @@
+QT += phonon
+
+HEADERS += mainwindow.h
+SOURCES += main.cpp \
+ mainwindow.cpp
+
+# install
+target.path = $$[QT_INSTALL_EXAMPLES]/phonon/qmusicplayer
+sources.files = $$SOURCES $$HEADERS $$FORMS $$RESOURCES *.pro *.png images
+sources.path = $$[QT_INSTALL_EXAMPLES]/phonon/qmusicplayer
+INSTALLS += target sources
+
+wince*{
+DEPLOYMENT_PLUGIN += phonon_ds9 phonon_waveout
+}
+
+symbian:TARGET.UID3 = 0xA000CF6A