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 use QSoundEffect (desparate to get simple code to work)
QtWS25 Last Chance

How to use QSoundEffect (desparate to get simple code to work)

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 3.6k Views
  • 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.
  • S Offline
    S Offline
    Snorkelbuckle
    wrote on 4 Jul 2019, 01:39 last edited by
    #1

    Hiya!

    I am having a devil of a time to get a simple sound to play! First, I've read all the docs I can read and now I'm barfing up text. I have even compiled and run the sample application "Maroon in Trouble". Maroon works fine, it plays sound. I thought maybe it was my wav file giving a problem, so I confiscated wav file from Maroon and I still can't get it to work. This project was created using the new project wizard for a Qt Widgets Application.

    OS: Windows 10, using ming32 compiler, Qt 5.13.0. BTW, I get same result with Qt 5.12.x, I upgraded to 5.13.0 thinking hoping it would help, the upgrade was a "Full uninstall, full install of new version". Here is my very simple QWidget app that won't play sound for nothing. Please, any advice here, how do I even troubleshoot this? Excluding the generated main.cpp. Also, the sound file is put into a resource file and it is directly taken from Maroon sample app.

    Project file .pro

    QT       += core gui multimedia
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = SoundTest
    TEMPLATE = app
    
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which has been marked as deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    CONFIG += c++11
    
    SOURCES += \
            main.cpp \
            mainwindow.cpp
    
    HEADERS += \
            mainwindow.h
    
    FORMS += \
            mainwindow.ui
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    
    RESOURCES += \
        sounds.qrc
    

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QSoundEffect>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    public slots:
        void playSound();
    
    private:
        Ui::MainWindow *ui;
    
    
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        connect(ui->actionMakeSomeNoise, &QAction::triggered, this, &MainWindow::playSound);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::playSound()
    {
        QSoundEffect effect;
        effect.setSource(QUrl("://projectile-action.wav"));
        effect.setLoopCount(10);
        effect.setMuted(false);
        effect.setVolume(1.0);
        effect.play();
    }
    
    
    J 1 Reply Last reply 4 Jul 2019, 02:23
    0
    • S Snorkelbuckle
      4 Jul 2019, 01:39

      Hiya!

      I am having a devil of a time to get a simple sound to play! First, I've read all the docs I can read and now I'm barfing up text. I have even compiled and run the sample application "Maroon in Trouble". Maroon works fine, it plays sound. I thought maybe it was my wav file giving a problem, so I confiscated wav file from Maroon and I still can't get it to work. This project was created using the new project wizard for a Qt Widgets Application.

      OS: Windows 10, using ming32 compiler, Qt 5.13.0. BTW, I get same result with Qt 5.12.x, I upgraded to 5.13.0 thinking hoping it would help, the upgrade was a "Full uninstall, full install of new version". Here is my very simple QWidget app that won't play sound for nothing. Please, any advice here, how do I even troubleshoot this? Excluding the generated main.cpp. Also, the sound file is put into a resource file and it is directly taken from Maroon sample app.

      Project file .pro

      QT       += core gui multimedia
      
      greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
      
      TARGET = SoundTest
      TEMPLATE = app
      
      # The following define makes your compiler emit warnings if you use
      # any feature of Qt which has been marked as deprecated (the exact warnings
      # depend on your compiler). Please consult the documentation of the
      # deprecated API in order to know how to port your code away from it.
      DEFINES += QT_DEPRECATED_WARNINGS
      
      # You can also make your code fail to compile if you use deprecated APIs.
      # In order to do so, uncomment the following line.
      # You can also select to disable deprecated APIs only up to a certain version of Qt.
      #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
      
      CONFIG += c++11
      
      SOURCES += \
              main.cpp \
              mainwindow.cpp
      
      HEADERS += \
              mainwindow.h
      
      FORMS += \
              mainwindow.ui
      
      # Default rules for deployment.
      qnx: target.path = /tmp/$${TARGET}/bin
      else: unix:!android: target.path = /opt/$${TARGET}/bin
      !isEmpty(target.path): INSTALLS += target
      
      RESOURCES += \
          sounds.qrc
      

      mainwindow.h

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      #include <QSoundEffect>
      
      namespace Ui {
      class MainWindow;
      }
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MainWindow(QWidget *parent = nullptr);
          ~MainWindow();
      
      public slots:
          void playSound();
      
      private:
          Ui::MainWindow *ui;
      
      
      };
      
      #endif // MAINWINDOW_H
      

      mainwindow.cpp

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          connect(ui->actionMakeSomeNoise, &QAction::triggered, this, &MainWindow::playSound);
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      void MainWindow::playSound()
      {
          QSoundEffect effect;
          effect.setSource(QUrl("://projectile-action.wav"));
          effect.setLoopCount(10);
          effect.setMuted(false);
          effect.setVolume(1.0);
          effect.play();
      }
      
      
      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 4 Jul 2019, 02:23 last edited by
      #2

      @Snorkelbuckle said in How to use QSoundEffect (desparate to get simple code to work):

      QSoundEffect effect;
      
      • Fact #1: QSoundEffect::play() is asynchronous. This means the function returns before the sound finishes playing.
      • Fact #2: You created your QSoundEffect object as a local variable. This means the object gets destroyed when MainWindow::playSound() returns.
      • Result: Your QSoundEffect object gets destroyed before it has the chance to play the sound.

      The fix? Make sure your QSoundEffect does not get destroyed too early. Make it a member variable of MainWindow, not a local variable of playSound().

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      3
      • S Offline
        S Offline
        Snorkelbuckle
        wrote on 4 Jul 2019, 02:46 last edited by Snorkelbuckle 7 Apr 2019, 02:47
        #3

        Thanks for that, but now this still doesn't work. But the commented section does. When I check the status, it goes from 3 to 0.

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        
        {
            ui->setupUi(this);
            efx = new QSoundEffect(this);
            efx->setSource(QUrl(":/projectile-action.wav"));
            efx->setVolume(0.90);
            efx->setLoopCount(2);
            connect(ui->actionMakeSomeNoise, &QAction::triggered, this, &MainWindow::playSound);
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        void MainWindow::playSound()
        {
            // THIS works when uncommented
            //QSound *efx2 = new QSound(":/projectile-action.wav");
            //efx2->setLoops(2);
            //efx2->play();
        
            ui->statusBar->showMessage(QString("efx status: %1").arg(efx->status()));
            efx->play();
        }
        
        1 Reply Last reply
        0
        • S Offline
          S Offline
          Snorkelbuckle
          wrote on 6 Jul 2019, 15:32 last edited by
          #4

          Any other ideas, anybody? So far it is looking like a bug to me, can anybody post some code that actually works on Windows 10 for a QWidget app? Or is the updated code I posted technically correct?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 6 Jul 2019, 15:47 last edited by mrjj 7 Jun 2019, 15:48
            #5

            Hi
            Your code works fine for me? ( win 10, Qt 5.12.3
            Only change i did was to use fromLocalFile

            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
                efx = new QSoundEffect(this);
                efx->setSource(QUrl::fromLocalFile(":/1.wav"));
                efx->setVolume(0.90);
                efx->setLoopCount(2);
            }
            
            void MainWindow::on_pushButton_released()
            {
                efx->play();
                ui->statusBar->showMessage(QString("efx status: %1").arg(efx->status()));
            }
            
            

            test project
            https://www.dropbox.com/s/r7h92vtm6mrk6aa/SoundTest.zip?dl=0

            1 Reply Last reply
            3
            • S Offline
              S Offline
              Snorkelbuckle
              wrote on 6 Jul 2019, 18:56 last edited by
              #6

              Thank you, mrjj!

              Works now! It was just that I needed the QUrl::fromLocalFile().

              J 1 Reply Last reply 6 Jul 2019, 18:59
              0
              • S Snorkelbuckle
                6 Jul 2019, 18:56

                Thank you, mrjj!

                Works now! It was just that I needed the QUrl::fromLocalFile().

                J Offline
                J Offline
                JKSH
                Moderators
                wrote on 6 Jul 2019, 18:59 last edited by
                #7

                @Snorkelbuckle said in How to use QSoundEffect (desparate to get simple code to work):

                It was just that I needed the QUrl::fromLocalFile().

                Or don't use QUrl (which you found when you wrote efx2)

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                3

                2/7

                4 Jul 2019, 02:23

                5 unread
                • Login

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