Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Solved QAudioInput: failed to open audio device

    General and Desktop
    2
    9
    495
    Loading More Posts
    • 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.
    • A
      asdlkjqpwoei last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 ?

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

        A 1 Reply Last reply Reply Quote 0
        • A
          asdlkjqpwoei @SGaist last edited by

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

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

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

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

            A 1 Reply Last reply Reply Quote 0
            • A
              asdlkjqpwoei @SGaist last edited by

              @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).

              1 Reply Last reply Reply Quote 0
              • A
                asdlkjqpwoei last edited by

                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();
                }
                
                1 Reply Last reply Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  Which version of Qt are you using ?

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

                  A 1 Reply Last reply Reply Quote 0
                  • A
                    asdlkjqpwoei @SGaist last edited by

                    @SGaist Qt 5.15.2

                    1 Reply Last reply Reply Quote 0
                    • A
                      asdlkjqpwoei last edited by

                      This issue has been solved at c++ - QAudioInput: failed to open audio device.

                      Solution:
                      16bit audio must be signed integer.

                      DataFormat.setSampleType(QAudioFormat::SignedInt);
                      
                      1 Reply Last reply Reply Quote 1
                      • First post
                        Last post