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. Can't get QSoundEffect to play

Can't get QSoundEffect to play

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 496 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.
  • L Offline
    L Offline
    leinad
    wrote on last edited by
    #1

    Hi,

    I wrote a very simple program to play .wav file which I got from c:/Windows/Media. For some reason when I try to play the wav file nothing happens. I even copied the audio dll's from my Qt 5.12.2 folder into the bin folder where the program exe files live.

    Below is the program.
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    playSound();
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::playSound()
    {
    QSoundEffect effect;
    QString consoleBinDir(QApplication::applicationDirPath());
    QString waveFile = QDir::cleanPath(consoleBinDir + "/Alarm01.wav");
    effect.setSource(QUrl::fromLocalFile(waveFile));
    qDebug() << QUrl::fromLocalFile(waveFile);
    effect.setLoopCount(1);
    effect.setVolume(1.0);
    effect.play();
    }
    When I print out QUrl::fromLocalFile(waveFile) I get "QUrl("file:///C:/Qt-development/build-sound-Desktop_Qt_5_15_2_MSVC2019_64bit-Debug/debug/Alarm01.wav")" which seems correct. The wave file is certainly located there.

    Can someone please explain to me what I'm doing wrong? It's only a few lines of code!

    Thanks

    Christian EhrlicherC 1 Reply Last reply
    0
    • L leinad

      Hi,

      I wrote a very simple program to play .wav file which I got from c:/Windows/Media. For some reason when I try to play the wav file nothing happens. I even copied the audio dll's from my Qt 5.12.2 folder into the bin folder where the program exe files live.

      Below is the program.
      #include "mainwindow.h"
      #include "ui_mainwindow.h"

      MainWindow::MainWindow(QWidget *parent)
      : QMainWindow(parent)
      , ui(new Ui::MainWindow)
      {
      ui->setupUi(this);

      playSound();
      

      }

      MainWindow::~MainWindow()
      {
      delete ui;
      }

      void MainWindow::playSound()
      {
      QSoundEffect effect;
      QString consoleBinDir(QApplication::applicationDirPath());
      QString waveFile = QDir::cleanPath(consoleBinDir + "/Alarm01.wav");
      effect.setSource(QUrl::fromLocalFile(waveFile));
      qDebug() << QUrl::fromLocalFile(waveFile);
      effect.setLoopCount(1);
      effect.setVolume(1.0);
      effect.play();
      }
      When I print out QUrl::fromLocalFile(waveFile) I get "QUrl("file:///C:/Qt-development/build-sound-Desktop_Qt_5_15_2_MSVC2019_64bit-Debug/debug/Alarm01.wav")" which seems correct. The wave file is certainly located there.

      Can someone please explain to me what I'm doing wrong? It's only a few lines of code!

      Thanks

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @leinad said in Can't get QSoundEffect to play:

      QSoundEffect effect;

      Basic C++ - how long does this object live?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • L Offline
        L Offline
        leinad
        wrote on last edited by
        #3

        It exists I guess after the play() is called, how can I make it live until the play is completed? I tried putting a QThread::sleep after the play to hold the tread but still no sound.

        jsulmJ 1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @leinad said in Can't get QSoundEffect to play:

          ow can I make it live until the play is completed?

          Again c++ basics - you can e.g. make it a member of your class.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • L leinad

            It exists I guess after the play() is called, how can I make it live until the play is completed? I tried putting a QThread::sleep after the play to hold the tread but still no sound.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @leinad said in Can't get QSoundEffect to play:

            I tried putting a QThread::sleep after the play to hold the tread but still no sound.

            ?!
            Make effect a class member, or allocate it on the heap...

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • L Offline
              L Offline
              leinad
              wrote on last edited by
              #6

              Got it! It works thanks!

              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