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

    playaudio.h
    @
    #ifndef PLAYAUDIO_H
    #define PLAYAUDIO_H

    #include <QWidget>

    class QPushButton;

    class playAudio : public QWidget
    {
    Q_OBJECT

    public:
    playAudio(QWidget *parent = 0);

    public slots:
    void playSound();

    private:
    QPushButton *play;
    };

    #endif // PLAYAUDIO_H
    @

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

    playAudio::playAudio(QWidget *parent) :
    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);
    setLayout(mainLayout);
    setWindowTitle(tr("Play Sound"));
    

    }

    void playAudio::playSound()
    {

    }
    @

    main.cpp

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

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

    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":http://doc.trolltech.com/4.6/qtmultimedia.html 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: http://lists-archives.org/kde-devel/24844-multiple-sounds-with-phonon.html



  • 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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.