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 device selection
Forum Updated to NodeBB v4.3 + New Features

QSoundEffect device selection

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 227 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.
  • K Offline
    K Offline
    kegon
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kegon
        wrote last edited by
        #3

        Sorry, I forgot to update:

        This was a version I installed using the online installer.

        I switched to Qt 6.9.1 and it seems resolved now.

        1 Reply Last reply
        0
        • K kegon has marked this topic as solved

        • Login

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