Saturday, April 9, 2011

Playing sound with Qt and Phonon

In games generally we need to play small sound clips. For Qt program in such case I prefer to use Phonon module.

Following is simple sample code that plays sound clips on button click event.

In constructor I am creating media object and audio output. MediaObject class provides playback interface, AudioOutput send audio data to output device and by createPath method call we are creating connection of media object to audio output.

Then in playSound method I am setting sound file which needs to be played by calling setCurrentSource api and providing path of sound file. Finally by calling play API on media object i am playing sound file.

SoundWidget::SoundWidget(QWidget *parent) :
    QWidget(parent) 
{
    QVBoxLayout* main = new QVBoxLayout(this);
    QPushButton* btn = new QPushButton("Play Sound");
    connect(btn,SIGNAL(clicked()),this,SLOT(playSound()));
    main->addWidget(btn);

    mMediaObject = new Phonon::MediaObject(this);
    mAudioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
    mAudioOutput->setVolume(0.5);

    Phonon::createPath(mMediaObject, mAudioOutput);
}

void SoundWidget::playSound() 
{
    mMediaObject->setCurrentSource(QString("./Sound5.wav") );
    mMediaObject->play();
}

Following is header file for above code. You also need to add phonon module in Qt project file by putting "QT += phonon" in pro file.
#ifndef SOUNDWIDGET_H
#define SOUNDWIDGET_H

#include <QWidget>
#include <phonon/mediaobject.h>
#include <phonon/audiooutput.h>

class SoundWidget : public QWidget
{
    Q_OBJECT
public:
    explicit SoundWidget(QWidget *parent = 0);   

public slots:
    void playSound();

private:
    Phonon::MediaObject* mMediaObject;
    Phonon::AudioOutput* mAudioOutput;
};

#endif // SOUNDWIDGET_H

7 comments:

  1. Hi, I have looked at your sample code, it's very helpful..thanks. I have a question, if the phone had been set into silent mode or mute or master volume set to zero, would it still play the sound? I have done a similar code as yours, but it plays sound with max volume no matter what is the volume setting on the phone. Do you know why is this happening?

    ReplyDelete
  2. Can you tell me which phone are you using, symbian or maemo ?

    I tried it maemo, no experience on symbian. On maemo, I tried your use case it looks to be working fine.

    Can you please provide more data so i can try it out.

    ReplyDelete
  3. Hi there

    Am using a very similar way to play sound in games, although there's always a small lag, depending on device speed! I'm testing on Maemo, Meego and Symbian

    Could you please help me what is the best solution to use in a game!

    ReplyDelete
  4. I can't get it working on N900.
    I think my problem is getting the sound files to the device. I've tried to mount a directory while running, to send the sound file to my phone in advance and every variation of those, but cannot find a file while running on N900.

    I try with QFile.exists("filename") and always get false.

    How the sound files are to be mounted to the device?
    Where to put them?
    How to refer to them correctly?

    ReplyDelete
  5. i generally put my sound files in MyDocs and play it. or put in opt directory, sound files are transffered while installation.

    ReplyDelete
  6. Yes, thanks, it works!

    ReplyDelete
  7. Thanks for sharing your code.

    ReplyDelete