Development/Tutorials/Phonon/Simple Media Player: Difference between revisions

    From KDE TechBase
    (Created page with '<!-- TODO: Link classes to apidox --> One of the primary target audiences of Phonon is simple multimedia players. After following this tutorial, you will be able to create a mini...')
     
    m (Text replace - "<code>" to "<syntaxhighlight lang="text">")
    Line 14: Line 14:
    We first begin by writing a CMakeLists.txt file which is used by CMake to generate a proper Makefile for building:
    We first begin by writing a CMakeLists.txt file which is used by CMake to generate a proper Makefile for building:


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


    Line 38: Line 38:


    Like all executable binaries, a <tt>main()</tt> function is needed. We implement a simple one in main.cpp:
    Like all executable binaries, a <tt>main()</tt> function is needed. We implement a simple one in main.cpp:
    <code>
    <syntaxhighlight lang="text">
    #include <QtGui/QApplication>
    #include <QtGui/QApplication>


    Line 55: Line 55:


    Copy the following code into your player.h:
    Copy the following code into your player.h:
    <code>
    <syntaxhighlight lang="text">
    #ifndef PLAYER_H
    #ifndef PLAYER_H
    #define PLAYER_H
    #define PLAYER_H
    Line 94: Line 94:
    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.


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

    Revision as of 20:45, 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:

    <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_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: <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 {

    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;
    

    };

    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