Development/Tutorials/Phonon/Simple Media Player

Jump to: navigation, search

Contents

Abstract

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-tutorial2.png

The Code

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

main.cpp

Like all executable binaries, a main() function is needed. We implement a simple one in main.cpp:

#include <KAboutData>
#include <KApplication>
#include <KCmdLineArgs>
 
#include "mainwindow.h"
 
int main(int argc, char *argv[])
{
  KAboutData aboutData("basicaudioplayer", 0,
                       ki18n("Basic Audio Player"), "1.0",
                       ki18n("A basic audio player using Phonon."),
                       KAboutData::License_BSD,
                       ki18n("Copyright (c) 2011-2013 Jon Ander Peñalba <jonan88@gmail.com>"));
  KCmdLineArgs::init(argc, argv, &aboutData);
  KApplication app;
 
  MainWindow *window = new MainWindow();
  window->show();
 
  return app.exec();
}

mainwindow.h

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 mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <KMainWindow>
 
class KLineEdit;
class KPushButton;
 
namespace Phonon {
class MediaObject;
}
 
class MainWindow : public KMainWindow
{
    Q_OBJECT
 
  public:
    MainWindow(QWidget *parent=0);
 
  private slots:
    void openFile();
 
  private:
    Phonon::MediaObject *media;
 
    KLineEdit *file_name;
    KPushButton *file_button;
};
 
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
 
#include <QtGui/QHBoxLayout>
 
#include <KApplication>
#include <KFileDialog>
#include <KLineEdit>
#include <KPushButton>
 
#include <Phonon/AudioOutput>
#include <Phonon/MediaObject>
#include <Phonon/SeekSlider>
#include <Phonon/VolumeSlider>
 
MainWindow::MainWindow(QWidget *parent)
    : KMainWindow(parent)
{
  // Create the widgets
 
  // File selector
  file_name = new KLineEdit("Open an audio file", this);
  file_button = new KPushButton("Open", this);
  file_name->setEnabled(false);
  connect(file_button, SIGNAL(clicked()), this, SLOT(openFile()));
 
  // Audio control
  Phonon::SeekSlider *seek = new Phonon::SeekSlider(this);
  KPushButton *play_button = new KPushButton("Play", this);
  KPushButton *pause_button = new KPushButton("Pause", this);
  KPushButton *stop_button = new KPushButton("Stop", this);
  Phonon::VolumeSlider *volume = new Phonon::VolumeSlider(this);
  volume->setOrientation(Qt::Vertical);
 
  // Define the layout
  // File stuff
  QHBoxLayout *file_layout = new QHBoxLayout(this);
  file_layout->addWidget(file_name);
  file_layout->addWidget(file_button);
 
  // Buttons
  QHBoxLayout *button_layout = new QHBoxLayout(this);
  button_layout->addWidget(play_button);
  button_layout->addWidget(pause_button);
  button_layout->addWidget(stop_button);
 
  // Seek slider
  QVBoxLayout *left_layout = new QVBoxLayout(this);
  left_layout->addWidget(seek);
  left_layout->addLayout(button_layout);
 
  // Volume slider
  QHBoxLayout *audio_layout = new QHBoxLayout(this);
  audio_layout->addLayout(left_layout);
  audio_layout->addWidget(volume);
 
  // Let's bring it all together
  QWidget *central_widget = new QWidget(this);
  QVBoxLayout *central_layout = new QVBoxLayout(this);
  central_layout->addLayout(file_layout);
  central_layout->addLayout(audio_layout);
  central_widget->setLayout(central_layout);
  setCentralWidget(central_widget);
 
  // Create the media and define the output
  media = new Phonon::MediaObject(this);
  Phonon::AudioOutput *output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
  Phonon::createPath(media, output);
 
  // Connect the widgets to the audio
  seek->setMediaObject(media);
  volume->setAudioOutput(output);
  connect(play_button, SIGNAL(clicked()), media, SLOT(play()));
  connect(pause_button, SIGNAL(clicked()), media, SLOT(pause()));
  connect(stop_button, SIGNAL(clicked()), media, SLOT(stop()));
}
 
void MainWindow::openFile()
{
  QString file = KFileDialog::getOpenFileName();
  if (file != ""){
    file_name->setText(file);
    media->setCurrentSource(file);
    media->play();
  }
}

Build

CMakeLists.txt

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

project (phonon_tutorial2)
find_package(KDE4 REQUIRED)
include (KDE4Defaults)
include_directories(${KDE4_INCLUDES})
set(phonon_tutorial2_SRCS main.cpp mainwindow.cpp)
kde4_add_executable(phonon_tutorial2 ${phonon_tutorial2_SRCS})
target_link_libraries(phonon_tutorial2 ${KDE4_KDEUI_LIBS} ${PHONON_LIBS} ${KDE4_KIO_LIBS})
install(TARGETS phonon_tutorial2  ${INSTALL_TARGETS_DEFAULT_ARGS})

Here we link our example against phonon.

Make And Run

cmake . && make && ./phonon_tutorial2

This page was last modified on 10 May 2013, at 17:41. This page has been accessed 969 times. Content is available under Creative Commons License SA 3.0 as well as the GNU Free Documentation License 1.2.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal