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. QAudioInput: failed to open audio device
Forum Updated to NodeBB v4.3 + New Features

QAudioInput: failed to open audio device

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 2.1k 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.
  • A Offline
    A Offline
    asdlkjqpwoei
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • SGaistS SGaist

        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 ?

        A Offline
        A Offline
        asdlkjqpwoei
        wrote on last edited by
        #3

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

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

          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
          0
          • SGaistS SGaist

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

            A Offline
            A Offline
            asdlkjqpwoei
            wrote on last edited by
            #5

            @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
            0
            • A Offline
              A Offline
              asdlkjqpwoei
              wrote on last edited by
              #6

              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
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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
                0
                • SGaistS SGaist

                  Which version of Qt are you using ?

                  A Offline
                  A Offline
                  asdlkjqpwoei
                  wrote on last edited by
                  #8

                  @SGaist Qt 5.15.2

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    asdlkjqpwoei
                    wrote on last edited by
                    #9

                    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
                    1

                    • Login

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