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.
Starting
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:
cmake_minimum_required(VERSION 2.6.2 FATAL_ERROR)
find_package(Qt4 REQUIRED)
include(${QT_USE_FILE})
find_package(Phonon REQUIRED)
add_definitions(${QT_DEFINITIONS} ${PHONON_DEFINITIONS})
include_directories(${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR} ${PHONON_INCLUDES})
set(simpleplayer_SRCS
main.cpp
)
qt4_automoc(${simpleplayer_SRCS})
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:
#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:
#ifndef PLAYER_H
#define PLAYER_H
#include <QtGui/QWidget>
#include <phonon/Global>
class QPushButton;
namespace Phonon {
class MediaObject;
class Mrl;
}
class Player : public QWidget {
Q_OBJECT
public:
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();
private:
Phonon::MediaObject *m_media;
QPushButton *m_playPause;
QPushButton *m_stop;
};
#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.
TODO - tdfischer Jun 7, 2011