October 6, 2010

doforumda doforumda
Lab Rat
161 posts

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

  1. #ifndef PLAYAUDIO_H
  2. #define PLAYAUDIO_H
  3.  
  4. #include <QWidget>
  5.  
  6. class QPushButton;
  7.  
  8. class playAudio : public QWidget
  9. {
  10.     Q_OBJECT
  11.  
  12. public:
  13.     playAudio(QWidget *parent = 0);
  14.  
  15. public slots:
  16.     void playSound();
  17.  
  18. private:
  19.     QPushButton *play;
  20. };
  21.  
  22. #endif // PLAYAUDIO_H

playaudio.cpp

  1. #include "playaudio.h"
  2. #include <QtGui>
  3.  
  4. playAudio::playAudio(QWidget *parent) :
  5.     QWidget(parent)
  6. {
  7.     QLabel *label = new QLabel(tr("Click Play to Play a Sound"));
  8.     play = new QPushButton(tr("Play Sound"));
  9.  
  10.     connect(play, SIGNAL(clicked()), this, SLOT(playSound()));
  11.  
  12.     QGridLayout *mainLayout = new QGridLayout;
  13.     mainLayout->addWidget(label, 0, 0);
  14.     mainLayout->addWidget(play, 1, 0);
  15.     setLayout(mainLayout);
  16.     setWindowTitle(tr("Play Sound"));
  17. }
  18.  
  19. void playAudio::playSound()
  20. {
  21.  
  22. }

main.cpp

  1. #include <QtGui/QApplication>
  2. #include "playaudio.h"
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.     QApplication a(argc, argv);
  7.     playAudio w;
  8.     w.show();
  9.  
  10.     return a.exec();
  11. }

5 replies

October 6, 2010

MTK358 MTK358
Lab Rat
27 posts

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.

October 6, 2010

danilocesar danilocesar
Lab Rat
150 posts

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

Try QtMultimedia [doc.trolltech.com] module or even Phonon API’s.

 Signature 

Danilo Cesar Lemes de Paula
Software Engineer

October 6, 2010

MTK358 MTK358
Lab Rat
27 posts

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.

October 6, 2010

danilocesar danilocesar
Lab Rat
150 posts

In fact they do use phonon…

Huge thread about it: http://lists-archives.org/kde-devel/24844-multiple-sounds-with-phonon.html

 Signature 

Danilo Cesar Lemes de Paula
Software Engineer

October 6, 2010

danilocesar danilocesar
Lab Rat
150 posts

Ah,

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

 Signature 

Danilo Cesar Lemes de Paula
Software Engineer

 
  ‹‹ Change QLineEdit text cursor shape      QScopedPointer and QSharedPointer don’t always solve the memory management problem ››

You must log in to post a reply. Not a member yet? Register here!