QSoundEffect device selection
-
Hi,
I am trying to play a WAV file using the following code:
QSoundEffect effect; effect.setSource(QUrl::fromLocalFile(soundedit->text())); effect.setLoopCount(1); effect.setVolume(0.5f); effect.play(); QEventLoop loop; loop.exec();
This plays the effect through my monitor even if my headphones are plugged in. I want to use the default device, i.e. headphones if they are plugged in and monitor if not; the same as the system.
I am running KDE so when I pull the headphone plug out I see on screen that the audio device is GA104 but with headphones it is Family something.
So maybe I am using the wrong audio device?
I ran
auto devices = QMediaDevices::audioOutputs(); for(int i = 0; i < devices.size(); i++) std::cerr << "device " << i << ": " << devices[i].description().toStdString() << "\n";
and the result was
device 0: GA104 High Definition Audio Controller Digital Stereo (HDMI) device 1: Family 17h/19h/1ah HD Audio Controller Analog Stereo
So I added
std::cerr << "using device: " << effect.audioDevice().description().toStdString() << "\n";
before the
effect.play()
but there is no description string.using device:
So I added
effect.setAudioDevice(QMediaDevices::defaultAudioOutput());
before
effect.play()
and my statement above that tells me the device being used.Now running the code with headphones plugged in says that the Family device is used and audio is played through the headphones. Success! But if that process calls the same function again all the following sounds are through the monitor, while the code reports it is using the Family device.
If I run the code without headphones, the sound goes through the GA104 and is heard from the monitor. If I then plug in the headphones the device changes to Family but all sounds are still through the monitor.
It seems like a bug to me.
For clarity, here is my code after making the changes mentioned above, plus a few inconsequential extras:
std::cerr << "Available devices:\n"; auto devices = QMediaDevices::audioOutputs(); for(int i = 0; i < devices.size(); i++) std::cerr << "device " << i << ": " << devices[i].description().toStdString() << "\n"; std::cerr << "Playing sound: " << soundedit->text().toStdString() << "\n"; QSoundEffect effect; effect.setSource(QUrl::fromLocalFile(soundedit->text())); effect.setLoopCount(1); effect.setVolume(0.5f); effect.setAudioDevice(QMediaDevices::defaultAudioOutput()); std::cerr << "using device: " << effect.audioDevice().description().toStdString() << "\n"; effect.play(); QEventLoop loop; loop.exec();
I am using Qt 6.6.2.
Thanks in advance for any help.
-
Hi,
Is this the Qt version that comes with your distribution ? If not, can you try that one ?
Did you also check with a more recent version ? 6.9 was released no too long ago. -