Difference between revisions of "Development/Tutorials/Phonon/Simple Media Player"

Jump to: navigation, search
m (Text replace - "<code>" to "<syntaxhighlight lang="text">")
m (Text replace - "</code>" to "</syntaxhighlight>")
Line 33: Line 33:
 
add_executable(simpleplayer ${simpleplayer_SRCS} ${simpleplayer_MOC_SRCS})
 
add_executable(simpleplayer ${simpleplayer_SRCS} ${simpleplayer_MOC_SRCS})
 
target_link_libraries(simpleplayer ${QT_LIBRARIES} phonon)
 
target_link_libraries(simpleplayer ${QT_LIBRARIES} phonon)
</code>
+
</syntaxhighlight>
  
 
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 [http://doc.qt.nokia.com/latest/ the official Qt documentation]
 
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 [http://doc.qt.nokia.com/latest/ the official Qt documentation]
Line 46: Line 46:
 
     return app.exec();
 
     return app.exec();
 
}
 
}
</code>
+
</syntaxhighlight>
  
 
player.cpp and player.h will be written in the next section. You can build the project with <tt>'cmake /path/to/sources/ && make'</tt>. Running the <tt>simpleplayer</tt> 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.
 
player.cpp and player.h will be written in the next section. You can build the project with <tt>'cmake /path/to/sources/ && make'</tt>. Running the <tt>simpleplayer</tt> 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.
Line 90: Line 90:
  
 
#endif //PLAYER_H
 
#endif //PLAYER_H
</code>
+
</syntaxhighlight>
  
 
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.
 
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.
Line 96: Line 96:
 
<syntaxhighlight lang="text">
 
<syntaxhighlight lang="text">
 
TODO - tdfischer Jun 7, 2011
 
TODO - tdfischer Jun 7, 2011
</code>
+
</syntaxhighlight>

Revision as of 20:53, 29 June 2011

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

Content is available under Creative Commons License SA 4.0 unless otherwise noted.