QSoundEffect for WebAssembly
-
Hi folks,
There is an officially announced support of Qt Multimedia for WebAssembly since release 6.2, but unfortunately I couldn't make it work.
I am trying to use QSoundEffect in a simple WebAssembly app with no success. What I tried is to play a wav audio from a resource file. I have installed Qt for WebAssembly 6.2.2 and 6.3.0. The app compiles and run successfully, but no any sound is produced. The same code compiles and run on Windows (MinGW 64bit), and the sound is ok.
In my *.pro file is added: QT += multimediaI have a resource file with a single resource:
<RCC>
<qresource prefix="/">
<file>door-bell.wav</file>
</qresource>
</RCC>(the above assumes the resource is put directly in the project folder, but I have tried in a subfolder as well, which works fine on Windows)
In my app class header (It is QDialog):
#include <QSoundEffect>
And I have a member object:
QSoundEffect soundDoorBell;In my app class constructor I have tried to set the audio source in two ways, both work on Windows:
this->soundDoorBell.setSource(QUrl("qrc:/door-bell.wav")); // Works on Win
this->soundDoorBell.setSource(QUrl::fromLocalFile(":/door-bell.wav")); // Works on Winthis->soundDoorBell.setLoopCount(1);
this->soundDoorBell.setVolume(1.0);Then the play method is called either from a timer, or a push button click slot:
this->soundDoorBell.play();
This work successfully on windows, but not when my webassembly app is deployed. I have tried on both Chrome (Version 102.0.5005.63), and Edge (Version 101.0.1210.53).
During my experiments, on the same app, a resource image was successfully used as a stylesheet, and this worked well for both Windows target and WebAssembly, see the example below:
LabelLogo->setStyleSheet("image: url(:/Logo.png;");I could not find much info or examples for Qt Multimedia for WebAssembly, except this forum thread:
https://forum.qt.io/topic/122313/webassembly-and-sound-effects/17
and the bug report regarding the module support, where the bug status is stated "Resolved" since 23 Aug 2021:
https://bugreports.qt.io/browse/QTBUG-69444
Please advise me on the proper use of a local resource as a QSoundEffect source if I do something wrong!
Thanks,
Peter -
QAudioSink should work in Qt webassembly, so QSoundEffect should work. I will look into this.
-
For anyone else having this issue, here's the bug report:
https://bugreports.qt.io/browse/QTBUG-104045 -
Based on the latest comment by Lorn in the above bug report, I tried using QAudioSink instead, and it happened to work!
This looks like more advanced API, offering more control but also requiring more settings. Perhaps it was considered for audio players usage rather than just sound effects.
I noticed one defect that may be insignificant while playing long tracks, but that makes it (by me) unsuitable for sound effects is that it adds a short "pop" sound. On Windows target it's at the end of the playback, on WebAssembly it appears both at the beginning and at the end of the playback. Could be related to the interpretation of the audio data length in the buffer, I'm trying to find a way to set some offset as a workaround, if possible at all. -
Small correction, as others might face the same issue: in more complex application QAudioSink also fails to work (but works when compiled for Windows target). Based on the latest comment by Lorn in the corresponding bug report, Qt Multimedia for WebAssembly requires threaded build. I am trying to get this, will keep this thread posted when more results are available.