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. QSoundEffect is only played 3 times using a QTimer
Forum Updated to NodeBB v4.3 + New Features

QSoundEffect is only played 3 times using a QTimer

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 477 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.
  • O Offline
    O Offline
    odelaune
    wrote on last edited by odelaune
    #1

    Hello, I use the following code

    main.cpp

    #include <QApplication>
    #include <QSoundEffect>
    #include <QObject>
    #include <QDebug>
    #include <QTimer>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QSoundEffect *player = new QSoundEffect();
        player->setSource(QUrl("qrc:/test.wav"));
        player->play();
    
        QTimer *timer = new QTimer();
        timer->setInterval(2'000); // 2 seconds
        QObject::connect(timer, &QTimer::timeout, [player]() {
          qDebug() << "is playing?" << player->isPlaying();
          // if(!player->isPlaying())
    	player->play();
        });
        timer->start();
        
        return a.exec();
    }
    

    Here is the CMakeLists.txt

    cmake_minimum_required(VERSION 3.5)
    
    project(AudioTestLoop LANGUAGES CXX)
    
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    
    set(CMAKE_AUTOUIC ON)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    
    set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    find_package(Qt6 COMPONENTS REQUIRED Core Widgets Multimedia)
    
    set(SOURCE_FILES
      resources.qrc
      main.cpp
      )
    
    add_executable(AudioTestLoop ${SOURCE_FILES})
    
    target_link_libraries(AudioTestLoop PRIVATE
      Qt6::Core
      Qt6::Widgets
      Qt6::Multimedia)
    

    Here is the content of resources.qrc

    <RCC>
        <qresource prefix="/">
            <file>test.wav</file>
        </qresource>
    </RCC>
    

    And you can download test.wav here (1-second sound) (the file will be deleted on February, 8th 2023).

    My problem is the following:
    the sound is played only 4 times (1 time because of player->play()) and then only 3 times in the timer connect. Then, the is a blank (the sound is not played after the fourth timer timeout, and then sound is played twice after the next timeout. After that, the sound is not played anymore.

    It looks like a bug but I would like you to confirm if you have the same before opening a new issue on Qt bugtracker (maybe I am doing something wrong).

    I am using Qt 6.4.2 installed with aqtinstall.

    mranger90M 1 Reply Last reply
    0
    • O odelaune

      Hello, I use the following code

      main.cpp

      #include <QApplication>
      #include <QSoundEffect>
      #include <QObject>
      #include <QDebug>
      #include <QTimer>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          QSoundEffect *player = new QSoundEffect();
          player->setSource(QUrl("qrc:/test.wav"));
          player->play();
      
          QTimer *timer = new QTimer();
          timer->setInterval(2'000); // 2 seconds
          QObject::connect(timer, &QTimer::timeout, [player]() {
            qDebug() << "is playing?" << player->isPlaying();
            // if(!player->isPlaying())
      	player->play();
          });
          timer->start();
          
          return a.exec();
      }
      

      Here is the CMakeLists.txt

      cmake_minimum_required(VERSION 3.5)
      
      project(AudioTestLoop LANGUAGES CXX)
      
      set(CMAKE_INCLUDE_CURRENT_DIR ON)
      
      set(CMAKE_AUTOUIC ON)
      set(CMAKE_AUTOMOC ON)
      set(CMAKE_AUTORCC ON)
      
      set(CMAKE_CXX_STANDARD 17)
      set(CMAKE_CXX_STANDARD_REQUIRED ON)
      
      find_package(Qt6 COMPONENTS REQUIRED Core Widgets Multimedia)
      
      set(SOURCE_FILES
        resources.qrc
        main.cpp
        )
      
      add_executable(AudioTestLoop ${SOURCE_FILES})
      
      target_link_libraries(AudioTestLoop PRIVATE
        Qt6::Core
        Qt6::Widgets
        Qt6::Multimedia)
      

      Here is the content of resources.qrc

      <RCC>
          <qresource prefix="/">
              <file>test.wav</file>
          </qresource>
      </RCC>
      

      And you can download test.wav here (1-second sound) (the file will be deleted on February, 8th 2023).

      My problem is the following:
      the sound is played only 4 times (1 time because of player->play()) and then only 3 times in the timer connect. Then, the is a blank (the sound is not played after the fourth timer timeout, and then sound is played twice after the next timeout. After that, the sound is not played anymore.

      It looks like a bug but I would like you to confirm if you have the same before opening a new issue on Qt bugtracker (maybe I am doing something wrong).

      I am using Qt 6.4.2 installed with aqtinstall.

      mranger90M Offline
      mranger90M Offline
      mranger90
      wrote on last edited by
      #2

      @odelaune
      Instead of using a lamda, why not just connect the timer timeout to the player's "play" slot ?

      O 1 Reply Last reply
      1
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        Looks like a bug, indeed.

        (Z(:^

        O 1 Reply Last reply
        0
        • mranger90M mranger90

          @odelaune
          Instead of using a lamda, why not just connect the timer timeout to the player's "play" slot ?

          O Offline
          O Offline
          odelaune
          wrote on last edited by
          #4

          @mranger90
          Even doing so, the sound is only played 3-4 times before definitively stopping.

          1 Reply Last reply
          0
          • sierdzioS sierdzio

            Looks like a bug, indeed.

            O Offline
            O Offline
            odelaune
            wrote on last edited by
            #5

            @sierdzio
            I have created QTBUG-110848.

            1 Reply Last reply
            1

            • Login

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