MainWindow Class Implementation
The MainWindow class handles both the user interface and Phonon. We will now take a look at the code relevant for Phonon. The code required for setting up the GUI is explained elsewhere.
We start with the constructor:
MainWindow::MainWindow()
{
audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
mediaObject = new Phonon::MediaObject(this);
metaInformationResolver = new Phonon::MediaObject(this);
mediaObject->setTickInterval(1000);
We start by instantiating our media and audio output objects. As mentioned, the media object knows how to playback multimedia (in our case sound files) while the audio output can send it to a sound device.
For the playback to work, the media and audio output objects need to get in contact with each other, so that the media object can send the sound to the audio output. Phonon is a graph based framework, i.e., its objects are nodes that can be connected by paths. Objects are connected using the createPath() function, which is part of the Phonon namespace.
Phonon::createPath(mediaObject, audioOutput);
We also connect signals of the media object to slots in our MainWindow. We will examine them shortly.
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()));
Finally, we call private helper functions to set up the GUI. The setupUi() function contains code for setting up the seek , and volume slider. We move on to setupUi():
void MainWindow::setupUi()
{
...
seekSlider = new Phonon::SeekSlider(this);
seekSlider->setMediaObject(mediaObject);
volumeSlider = new Phonon::VolumeSlider(this);
volumeSlider->setAudioOutput(audioOutput);
After creating the widgets, they must be supplied with the MediaObject and AudioOutput objects they should control.
In the setupActions(), we connect the actions for the play, pause, and stop tool buttons, to slots of the media object.
connect(playAction, SIGNAL(triggered()), mediaObject, SLOT(play()));
connect(pauseAction, SIGNAL(triggered()), mediaObject, SLOT(pause()) );
connect(stopAction, SIGNAL(triggered()), mediaObject, SLOT(stop()));
We move on to the slots of MainWindow, starting with addFiles():
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));
}
In the addFiles() slot, we add files selected by the user to the sources list. We then set the first source selected on the metaInformationProvider MediaObject, which will send a state changed signal when the meta information is resolved; we have this signal connected to the metaStateChanged() slot.
The media object informs us of state changes by sending the stateChanged() signal. The stateChanged() slot is connected to this signal.
void MainWindow::stateChanged(Phonon::State newState, Phonon::State )
{
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;
The errorString() function gives a description of the error that is suitable for users of a Phonon application. The two values of the ErrorState enum helps us determine whether it is possible to try to play the same file again.
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;
We update the GUI when the playback state changes, i.e., when it starts, pauses, stops, or resumes.
The media object will report other state changes, as defined by the State enum.
The tick() slot is connected to a MediaObject signal which is emitted when the playback position changes:
void MainWindow::tick(qint64 time)
{
QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
timeLcd->display(displayTime.toString("mm:ss"));
}
The time is given in milliseconds.
When the table is clicked on with the mouse, tableClick() is invoked:
void MainWindow::tableClicked(int row, int )
{
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();
}
Since we stop the media object, we first check whether it is currently playing. row contains the row in the table that was clicked upon; the indices of sources follows the table, so we can simply use row to find the new source.
void MainWindow::sourceChanged(const Phonon::MediaSource &source)
{
musicTable->selectRow(sources.indexOf(source));
timeLcd->display("00:00");
}
When the media source changes, we simply need to select the corresponding row in the table.
void MainWindow::metaStateChanged(Phonon::State newState, Phonon::State )
{
if (newState == Phonon::ErrorState) {
QMessageBox::warning(this, tr("Error opening files"),
metaInformationResolver->errorString());
while (!sources.isEmpty() &&
!(sources.takeLast() == metaInformationResolver->currentSource())) {} ;
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);
When metaStateChanged() is invoked, metaInformationProvider has resolved the meta data for its current source. A MediaObject will do this before entering StoppedState. Note that we could also have used the metaDataChanged() signal for this purpose.
Some of the meta data is then chosen to be displayed in the music table. A file might not contain the meta data requested, in which case an empty string is returned.
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);
}
}
If we have media sources in sources of which meta information is not resolved, we set a new source on the metaInformationProvider, which will invoke metaStateChanged() again.
We move on to the aboutToFinish() slot:
void MainWindow::aboutToFinish()
{
int index = sources.indexOf(mediaObject->currentSource()) + 1;
if (sources.size() > index) {
mediaObject->enqueue(sources.at(index));
}
}
When a file is finished playing, the Music Player will move on and play the next file in the table. This slot is connected to the MediaObject's aboutToFinish() signal, which is guaranteed to be emitted while there is still time to enqueue another file for playback.
The main() function.
Phonon requires that the application has a name; it is set with setApplicationName(). This is because D-Bus, which is used by Phonon on Linux systems, demands this.
int main(int argv, char **args)
{
QApplication app(argv, args);
app.setApplicationName("Music Player");
app.setQuitOnLastWindowClosed(true);
MainWindow window;
window.show();
return app.exec();
}