How to play sound

  • i am creating a small application that plays sound by clicking "Play Sound". How can i do that.
    here is my code

    #ifndef PLAYAUDIO_H
    #define PLAYAUDIO_H

    #include <QWidget>

    class QPushButton;

    class playAudio : public QWidget

    playAudio(QWidget *parent = 0);

    public slots:
    void playSound();

    QPushButton *play;

    #endif // PLAYAUDIO_H

    #include "playaudio.h"
    #include <QtGui>

    playAudio::playAudio(QWidget *parent) :
    QLabel *label = new QLabel(tr("Click Play to Play a Sound"));
    play = new QPushButton(tr("Play Sound"));

    connect(play, SIGNAL(clicked()), this, SLOT(playSound()));
    QGridLayout *mainLayout = new QGridLayout;
    mainLayout->addWidget(label, 0, 0);
    mainLayout->addWidget(play, 1, 0);
    setWindowTitle(tr("Play Sound"));


    void playAudio::playSound()



    #include <QtGui/QApplication>
    #include "playaudio.h"

    int main(int argc, char *argv[])
    QApplication a(argc, argv);
    playAudio w;;

    return a.exec&#40;&#41;;


  • Try QSound.

    All it does is play a sound file, it doesn't support pausing or seeking, and it only works with simple formats like WAVE. It's meant mostly for short sound effects.

  • If you're a linux user:
    QSound will not work if your Xorg doesn't use NAS (probably it doesn't use).

    Try "QtMultimedia": module or even Phonon API's.

  • Somehow I doubt that they use Phonon for all the little sound effects in Linux Qt programs.

    Anyway, I never tried QSound, so I don't know if it will work.

  • In fact they do use phonon...

    Huge thread about it:

  • Ah,

    The problem I've mentioned on that thread has been solved by using pulseaudio API's directly. But I do not recommend that =)

Log in to reply