Important: Please read the Qt Code of Conduct -

QAudioOutput Unable to setVolume

  • Currently I'm struggling to set the volume for a QAudioOutput object...
    In the documentation its noted that ::setVolume takes a qreal

    I have a horizontal slider with value 0 to 100.
    Then I convert the incoming horizontal Slider value like this:

    output is a QAudioOutput

    void SoundSystem::SetVolume(int volume)
        if(output->state() ==  QAudio::ActiveState)
            //qreal linearVolume = QAudio::convertVolume(volume / qreal(100.0),
            //output->setVolume(qRound(linearVolume * 100));
            qreal newVol =  (qreal)volume / 100;
            qDebug() << "Volume  Value: "  << newVol;
            qDebug() << "Check Value: "  << output->volume();

    The debugging output:

    handleAudioStateChanged ActiveState
    Volume Value: 0.99
    Check Value: 0.99
    Volume Value: 0.98
    Check Value: 0.98
    Volume Value: 0.97
    Check Value: 0.97
    Volume Value: 0.96
    Check Value: 0.96
    Volume Value: 0.95
    Check Value: 0.95
    Volume Value: 0.7
    Check Value: 0.7
    handleAudioStateChanged StoppedState
    sound stopped

    It seems like the value is set currently and even when I query the objects current volume using ::volume() member I get the correct values. However the volume doesn't change. Only when it get to max or min does the stream either mute or play at full volume.

    So basically when I move the slider it goes from 0 (Min value from the Slider) to 100 (MAX value from the slider.)
    converting that to a range between 0 and 1. All which works when I move the slider the audio either plays or if the slider is low the audio is muted, everything in between doesn't do anything to the volume of the QAudioOutput output.
    I have also tried to do it like its stated in the Qt Documentation on how to set the volume and that too didn't work.

    My setup:
    Windows 10 Pro x64
    Qt 5.9.3
    MinGW32 Compiler.

    Any idea's?

  • @Banshee10000

    Can you setVolume directly with qreal values which you get from QAudio::convertVolume ?

  • Hello @Ratzz I have tried to set the value directly, with qreal values as suggested but same results. Volume doesn't change unless slider is at 0 then the audio mutes otherwise its playing at max. so either 0 or 1. even though I feed it a range between 0 and 1 its ignored
    Debug from qreal values set directly using QAudio::convertVolume

    handleAudioStateChanged ActiveState
    Check Value: 1
    Check Value: 0.849485
    Check Value: 0.761439
    Check Value: 0.69897
    Check Value: 0.650515
    Check Value: 0.610924
    Check Value: 0.577451
    Check Value: 0.548455
    Check Value: 0.522879
    Check Value: 0.5
    Check Value: 0
    handleAudioStateChanged StoppedState
    sound stopped

    I have also tried to use doubles and floats instead of qreal and I have downloaded the latest version of Qt 5.11.1 to see if that address the issue but the same issue remains. No idea however why though.

  • Hi @Banshee10000

    Did you check the example code available in Qt ?
    the link suggested by @Ratzz
    Can you run this example and check the volume ?

  • I was just testing with the example on my system which works. The volume can be set correctly and behaves as expected in the example. Could it maybe be PCM data that I'm sending to QAudioOutput that is preventing correct volume change ?

    I'm also using QAudioOutput in a thread. the slider is on the mainWindow UI sending the int value to the thread via signals and slots. I know that the value from the slider is received and converted in the thread correctly as per debug output. As well as when I move the slider it does mute the audio when its at its lowest point but moving it instantly goes from mute to max vol.

  • After a lot of trial and error I managed to solve this. It was indeed the way I create the PCM data.
    I used a unSignedInt as the sampleType and it should be SignedInt. Then setVolume works.

Log in to reply