c++qtqt5qtmultimedia

QAudioInput: failed to open audio device


I'm try to use Qt Multimedia to open microphone and access data. I followed the official example Audio Input Example and get the following code(Just a test program, I will inject into my other project when it's okay). Environment: Windows 10 20H2. Qt Creator, MinGW 64-bit or Microsoft Visual Studio 2019, MSVC2019 64-bit

#include "main_window.h"
#include "ui_main_window.h"
MainWindow::MainWindow(QWidget* parent): QMainWindow(parent), ui(new Ui::MainWindow), AvailableAudioInputDevices(QAudioDeviceInfo::availableDevices(QAudio::AudioInput)), Microphone(nullptr), MicrophoneStream(nullptr)
{
    ui->setupUi(this);
    /* List all devices */
    ui->OutputTextBrowser->setPlainText(QString::fromUtf8("Available audio input devices:"));
    for(int i=0;i<AvailableAudioInputDevices.size();i++)
    {
        ui->OutputTextBrowser->append(AvailableAudioInputDevices.at(i).deviceName());
    }
    /* Set up data format */
    DataFormat.setSampleSize(16);
    DataFormat.setSampleRate(16000);
    DataFormat.setCodec(QString("audio/pcm"));
    DataFormat.setSampleType(QAudioFormat::Float);
    DataFormat.setByteOrder(QAudioFormat::LittleEndian);
    DataFormat.setChannelCount(2);
    /* Check format supported */
    if(QAudioDeviceInfo::defaultInputDevice().isFormatSupported(DataFormat))
    {
        ui->OutputTextBrowser->append(QString::fromUtf8("Supported format."));
    }
    else
    {
        ui->OutputTextBrowser->append(QString::fromUtf8("Unsupported format."));
    }
    /* Open */
    Microphone.reset(new QAudioInput(QAudioDeviceInfo::defaultInputDevice(), DataFormat));
    MicrophoneStream.reset(Microphone->start());
}

I got the output from the text browser.

Available audio input devices:
Microphone Array (Realtek(R) Audio)
Stereo Mix (Realtek(R) Audio)
Supported format.

Output from the debug console.

QAudioInput: failed to open audio device

Of course my program can't work but the example work properly. I found some possible cause from Google but none work such as turning on Windows 10 privacy setting.


Solution

  • 16bit audio must be signed integer.

    DataFormat.setSampleType(QAudioFormat::SignedInt);