Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to play sound
Qt 6.11 is out! See what's new in the release blog

How to play sound

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 9.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    doforumda
    wrote on last edited by
    #1

    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;;
    

    }
    @

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MTK358
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        danilocesar
        wrote on last edited by
        #3

        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.

        <a href="http://www.danilocesar.com">Danilo Cesar Lemes de Paula</a>
        Software Engineer

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MTK358
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            danilocesar
            wrote on last edited by
            #5

            In fact they do use phonon...

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

            <a href="http://www.danilocesar.com">Danilo Cesar Lemes de Paula</a>
            Software Engineer

            1 Reply Last reply
            0
            • D Offline
              D Offline
              danilocesar
              wrote on last edited by
              #6

              Ah,

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

              <a href="http://www.danilocesar.com">Danilo Cesar Lemes de Paula</a>
              Software Engineer

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved