Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QAudioInput: failed to open audio device



  • I'm try to use Qt Multimedia to open microphone and access data. I followed the official 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. The problem has block me a couple of days and I still get no idea.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What I would check is the exact differences you have with regard to the example.

    One silly question, do you have another application using the same device ?



  • @SGaist No other applications use the device when I debug.


  • Lifetime Qt Champion

    When exactly does the error happen ? Already in the constructor ?



  • @SGaist All I got only QAudioInput: failed to open audio device no matter where it is. I try to start() at other function or active through the signals&slots but got the same issue. QAudioInput::error() return 1(QAudio::OpenError).



  • This is the complete test program code.

    /* main_window.h */
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QAudioDeviceInfo>
    #include <QAudioFormat>
    #include <QAudioInput>
    #include <QIODevice>
    #include <QMainWindow>
    QT_BEGIN_NAMESPACE
    namespace Ui
    {
        class MainWindow;
    }
    QT_END_NAMESPACE
    class MainWindow: public QMainWindow
    {
        Q_OBJECT
        private:
            /* User interferace */
            Ui::MainWindow *ui;
            /* Multimedia */
            const QList<QAudioDeviceInfo> AvailableAudioInputDevices;
            QAudioFormat DataFormat;
            QAudioInput* Microphone;
            /* IO stream */
            QIODevice* MicrophoneStream;
        public:
            MainWindow(QWidget *parent = nullptr);
            ~MainWindow();
    };
    #endif // MAINWINDOW_H
    
    // ---------------------------------------
    
    /* main_window.cpp */
    #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(1);
        /* 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=new QAudioInput(QAudioDeviceInfo::defaultInputDevice(), DataFormat);
        MicrophoneStream=Microphone->start();
    }
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    // ------------------------------------------
    
    /* main.cpp */
    #include "main_window.h"
    #include <QApplication>
    #include <QLocale>
    #include <QTranslator>
    int main(int argc, char *argv[])
    {
        QApplication Application(argc, argv);
        QTranslator Translator;
        const QStringList UILanguages = QLocale::system().uiLanguages();
        for (const QString &locale : UILanguages)
        {
            const QString baseName = "Test1_QtWidgetApplication1_" + QLocale(locale).name();
            if (Translator.load(":/i18n/" + baseName))
            {
                Application.installTranslator(&Translator);
                break;
            }
        }
        MainWindow main_window;
        main_window.show();
        return Application.exec();
    }
    

  • Lifetime Qt Champion

    Which version of Qt are you using ?



  • @SGaist Qt 5.15.2


Log in to reply