QSoundEffect is only played 3 times using a QTimer
-
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.
-
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.
-
Looks like a bug, indeed.
-
@odelaune
Instead of using a lamda, why not just connect the timer timeout to the player's "play" slot ?@mranger90
Even doing so, the sound is only played 3-4 times before definitively stopping.