< Development‎ | Tutorials
Revision as of 20:45, 29 June 2011 by Neverendingo (talk | contribs) (Text replace - "<code>" to "<syntaxhighlight lang="text">")

Development/Tutorials/Phonon/Simple Media Player

One of the primary target audiences of Phonon is simple multimedia players. After following this tutorial, you will be able to create a minimal multimedia player application using C++, Phonon, Qt, and CMake.


Phonon's native language is C++. For other languages such as Python, Ruby, QML, etc, have a look at our page covering other language bindings for language-specific information. Other than what is noted on that page, Phonon's API is 100% the same for all platforms.

This simple player application will consist of two .cpp files, one .h, and a CMakeLists.txt:

  • main.cpp
  • player.cpp
  • player.h
  • CMakeLists.txt

We first begin by writing a CMakeLists.txt file which is used by CMake to generate a proper Makefile for building:

<syntaxhighlight lang="text"> cmake_minimum_required(VERSION 2.6.2 FATAL_ERROR)

find_package(Qt4 REQUIRED) include(${QT_USE_FILE})

find_package(Phonon REQUIRED)






add_executable(simpleplayer ${simpleplayer_SRCS} ${simpleplayer_MOC_SRCS}) target_link_libraries(simpleplayer ${QT_LIBRARIES} phonon)

First, we define a minimum cmake version. Without it, you'll get an ugly but harmless warning. Next, the CMake script looks for the Qt package and sets up the build environment to use it. Finally, we define a variable to contain our list of sources then run Qt's moc tool, build an executable, and link it against phonon. If you are unfamiliar with moc, please refresh your memory with the official Qt documentation

Like all executable binaries, a main() function is needed. We implement a simple one in main.cpp: <syntaxhighlight lang="text">

  1. include <QtGui/QApplication>

int main(int argc, char **argv) {

   QApplication app(argc, argv);
   return app.exec();


player.cpp and player.h will be written in the next section. You can build the project with 'cmake /path/to/sources/ && make'. Running the simpleplayer binary won't do anything other than sit in the Qt event loop, waiting for the last window to be closed. Since no windows were created, it waits forever.

Building a GUI

Now that we have a working build environment, real work can begin. The first step is to build a simple GUI. In this simple player, the GUI will be nothing more than a simple QWidget with a few children. No fancy designer .ui files, no complicated file browser, no exciting features.

Copy the following code into your player.h: <syntaxhighlight lang="text">

  1. ifndef PLAYER_H
  2. define PLAYER_H
  1. include <QtGui/QWidget>
  2. include <phonon/Global>

class QPushButton; namespace Phonon {

   class MediaObject;
   class Mrl;

} class Player : public QWidget {



   Player(QWidget *parent = 0, Qt::WindowFlags flag = 0);

public slots:

   void load(const Phonon::Mrl &url);
   void load();

private slots:

   void mediaStateChanged(Phonon::State newState, Phonon::State oldState);
   void playPause();


   Phonon::MediaObject *m_media;
   QPushButton *m_playPause;
   QPushButton *m_stop;


  1. endif //PLAYER_H

The code defines a Player class that inherits from QWidget. It contains a reference to a phonon MediaObject along with two push buttons. Two slots to handle updating the user interface in response to playback state changes and button presses are present. In addition, it contains a method to load a specific URL (later passed in via the command line) and another one to prompt the user for a file to load.

<syntaxhighlight lang="text"> TODO - tdfischer Jun 7, 2011

This page was last edited on 10 May 2013, at 16:41. Content is available under Creative Commons License SA 4.0 unless otherwise noted.