Developers
Phonon Logo

Discover Phonon
Using Phonon
Writing Backends
Getting Involved

As a KDE application developer all you need to know is the frontend API. Since some parts of that API are more or less finished, a preview of what programming with Phonon will be like can be given at this point.

For simple playback tasks using the Phonon::createPlayer function should be all you need. If you want to have more control read on.

diagram of simple audio playback example
The central objects in Phonon for audio playback are MediaObject and AudioOutput. The data produced by the MediaObject needs to find its way to the output. This is done with the Phonon::createPath function which returns a Path object that can be used to insert effects that should be applied to the audio signal.

Example: Simple Media Player

MediaPlayer::MediaPlayer(QWidget *parent)
  : QWidget(parent)
{
  QVBoxLayout *layout = new QVBoxLayout(this);

  m_vwidget = new VideoWidget(this);
  layout->addWidget(m_vwidget);

  m_aoutput = new AudioOutput(Phonon::VideoCategory, this);

  Phonon::createPath(m_media, m_aoutput);
  Phonon::createPath(m_media, m_vwidget);

  connect(m_playButton, SIGNAL(clicked()), m_media, SLOT(play()));
  m_volumeSlider = new VolumeSlider(this);
  layout->addWidget(m_volumeSlider);
  m_volumeSlider->setAudioOutput(m_aoutput);
}

void MediaPlayer::setUrl(const KUrl &url)
{
  m_media->setCurrentSource(url);
}

If you're familiar with Qt the program code should be almost self-documenting, except perhaps for the MediaControls class. But let's look at the example from the top.

After a vertical box layout is created a VideoWidget is created, added to the layout and then hidden. Then the Phonon objects VideoPath, AudioOutput, AudioPath and MediaObject are created. The MediaObject then is told to use those path objects and the outputs are added to the *Path objects. The latter can be used for connecting sources to sinks (or in the Phonon nomenclature: producers to outputs). The pathes have another important functionality that isn't used in this example: you can insert effects into the path. Finally the MediaControls widget is added to the layout and the MediaObject and AudioOutput are passed to the MediaControls object. This widget displays play, pause, stop buttons that call the according slots in MediaObject, shows a seek slider that shows the progress and can seek in the MediaObject, and as soon as the AudioOutput is set it also displays a volume slider.

That's it. All it needs now is a URL. MediaObject then tells us whether the media data contains a video signal, and if it does the VideoWidget is shown.

More to Come

Take a look at the API Documentation to see the current state. More examples and HOWTOs will also be made available.