Home · Examples 


Music Player Example

Code:

The Music Player Example shows how to use Phonon - the multimedia framework that comes with Qt - to create a simple music player. The player can play music files, and provides simple playback control, such as pausing, stopping, and resuming the music.

The player has a button group with the play, pause, and stop buttons familiar from most music players. The top-most slider controls the position in the media stream, and the bottom slider allows adjusting the sound volume.

The user can use a file dialog to add music files to a table, which displays meta information about the music - such as the title, album, and artist. Each row contains information about a single music file; to play it, the user selects that row and presses the play button. Also, when a row is selected, the files in the table are queued for playback.

Phonon offers playback of sound using an available audio device, e.g., a sound card or an USB headset. For the implementation, we use two objects: a MediaObject, which controls the playback, and an AudioOutput, which can output the audio to a sound device. We will explain how they cooperate when we encounter them in the code. For a high-level introduction to Phonon, see its overview.

The API of Phonon is implemented through an intermediate technology on each supported platform: DirectShow, QuickTime, and GStreamer. The sound formats supported may therefore vary from system to system. We do not in this example try to determine which formats are supported, but let Phonon report an error if the user tries to play an unsupported sound file.

Our player consists of one class, MusicPlayer, which both constructs the GUI and handles the playback. We will now go through the parts of its implementation that concerns Phonon.

MusicPlayer Class

The MusicPlayer 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.

Let's start by examining the variables of MusicPlayer:

    private SeekSlider seekSlider;
    private MediaObject mediaObject;
    private MediaObject metaInformationResolver;
    private AudioOutput audioOutput;
    private VolumeSlider volumeSlider;
    private List<MediaSource> sources =
        new Vector<MediaSource>();
We use the
SeekSlider to move the current playback position in the media stream, and the VolumeSlider controls the sound volume. Both of these widgets come ready made with Phonon. We use another MediaObject, metaInformationProvider, to get the meta information from the music files. More on this later.
    public MusicPlayer()
    {
        audioOutput = new AudioOutput(Phonon.Category.MusicCategory);
        mediaObject = new MediaObject(this);
        metaInformationResolver = new MediaObject(this);

        Phonon.createPath(mediaObject, audioOutput);

        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 in the Phonon class.

        mediaObject.tick.connect(this, "tick(long)");
        mediaObject.stateChanged.connect(this, "stateChanged(Phonon$State,Phonon$State)");
        metaInformationResolver.stateChanged.
                connect(this, "metaStateChanged(Phonon$State,Phonon$State)");
        mediaObject.currentSourceChanged.connect(this, "sourceChanged(MediaSource)");
        mediaObject.aboutToFinish.connect(this, "aboutToFinish()");
The MediaObject informs us of the state of the playback and properties of the media it is playing back through a series of signals. We connect the signals we need to slots in MusicPlayer.
        setupActions();
        setupMenus();
        setupUi();
        timeLcd.display("00:00"); 
    }
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():
    private void setupUi()
    {
...
        seekSlider = new SeekSlider(this);
        seekSlider.setMediaObject(mediaObject);

        volumeSlider = new VolumeSlider(this);
        volumeSlider.setAudioOutput(audioOutput);
After creating the widgets, they must be supplied with the MediaObject and AudioOutput objects they should control.

In setupActions(), we connect the actions for the play, pause, and stop tool buttons, to slots of the media object.

        playAction.triggered.connect(mediaObject, "play()");
        pauseAction.triggered.connect(mediaObject, "pause()");
        stopAction.triggered.connect(mediaObject, "stop()");
We move on to the the slots of MusicPlayer, starting with addFiles():
    private void addFiles()
    {
        List<String> files = QFileDialog.getOpenFileNames(this,
                            tr("Select Music Files"), ".");

        if (files.isEmpty())
            return;

        int index = sources.size();
        for (String string : files) {
            MediaSource source = new MediaSource(string);
        
            sources.add(source);
        } 
        if (!sources.isEmpty())
            metaInformationResolver.setCurrentSource(sources.get(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 metaInformationProviderMediaObject, 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.

    private void stateChanged(Phonon.State newState, Phonon.State oldState)
    {
        switch (newState) {
            case ErrorState:
                if (mediaObject.errorType().equals(Phonon.ErrorType.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 PlayingState:
                playAction.setEnabled(false);
                pauseAction.setEnabled(true);
                stopAction.setEnabled(true);
                break;
            case StoppedState:
                stopAction.setEnabled(false);
                playAction.setEnabled(true);
                pauseAction.setEnabled(false);
                timeLcd.display("00:00");
                break;
            case 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:

    private void tick(long time)
    {
        QTime displayTime = new QTime(0, (int) (time / 60000) % 60, (int) (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:

    private void tableClicked(int row, int column)
    {
        boolean wasPlaying = mediaObject.state().equals(Phonon.State.PlayingState);

        mediaObject.stop();
        mediaObject.clearQueue();

        mediaObject.setCurrentSource(sources.get(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.
    private void sourceChanged(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.
    private void metaStateChanged(Phonon.State newState, Phonon.State oldState)
    {
        if (newState.equals(Phonon.State.ErrorState)) {
            QMessageBox.warning(this, tr("Error opening files"),
                metaInformationResolver.errorString());
            while (!sources.isEmpty() &&
                   !(sources.remove(sources.size() - 1).equals(metaInformationResolver.currentSource())));
            return;
        }

        if (!newState.equals(Phonon.State.StoppedState))
            return;

        if (metaInformationResolver.currentSource().type().equals(MediaSource.Type.Invalid))
                return;

        Map<String, List<String>> metaData = metaInformationResolver.metaData();


        String title = "";
        if (metaData.get("TITLE") != null)
            title = metaData.get("TITLE").get(0);

        if (title.equals(""))
            title = metaInformationResolver.currentSource().fileName();

        String artist = "";
        if (metaData.get("ARTIST") != null)
            artist = metaData.get("ARTIST").get(0);

        String album = "";
        if (metaData.get("ALBUM") != null)
            album = metaData.get("ALBUM").get(0);

        String year = "";
        if (metaData.get("DATE") != null)
            year = metaData.get("DATE").get(0);

        QTableWidgetItem titleItem = new QTableWidgetItem(title);
        QTableWidgetItem artistItem = new QTableWidgetItem(artist);
        QTableWidgetItem albumItem = new QTableWidgetItem(album);
        QTableWidgetItem yearItem = new QTableWidgetItem(year);
When metaStateChanged() is invoked, metaInformationProvider has resolved the meta data for its current source. A MediaObject will do this before leaving the LoadingState. 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());
        }

        MediaSource source = metaInformationResolver.currentSource();
        int index = sources.indexOf(metaInformationResolver.currentSource()) + 1;
        if (sources.size() > index) {
            metaInformationResolver.setCurrentSource(sources.get(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:

    private void aboutToFinish()
    {
        int index = sources.indexOf(mediaObject.currentSource()) + 1;
        if (sources.size() > index) {
            mediaObject.enqueue(sources.get(index));
            musicTable.selectRow(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.
    public static void main(String args[])
    {
            QApplication.initialize(args);
            QApplication.setApplicationName("Music Player");

            new MusicPlayer().show();

            QApplication.exec();
    }


Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies) Trademarks
Qt Jambi 4.5.2_01