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
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?
ReplyDeleteCan you tell me which phone are you using, symbian or maemo ?
ReplyDeleteI 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.
Hi there
ReplyDeleteAm 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!
I can't get it working on N900.
ReplyDeleteI 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?
i generally put my sound files in MyDocs and play it. or put in opt directory, sound files are transffered while installation.
ReplyDeleteYes, thanks, it works!
ReplyDeleteThanks for sharing your code.
ReplyDelete