我正在尝试在 Qt 中编写一个程序,该程序同时记录来自麦克风的音频并同时播放。我正在使用 Qt 5.9.4 并且我在 Fedora 29 上(无法更新到最新版本,因为我们的生产环境是 Fedora 29 - 无法更新,已经问过老板了)。
我编写了一些准系统代码,如下所示。但是每次我运行程序时,我都会收到以下错误消息:
using null output device, none available
using null input device, none available
我已经安装了每个 qt5* 包。我也安装了 alsa-utils 和脉冲音频。
我还查看了这些或多或少对我有帮助但没有解决我的问题的方法:
- Qt - 如何同时录制和播放声音
- https://forum.qt.io/topic/10399/how-to-record-and-play-sound-simultaneously/5
- https://www.qtcentre.org/threads/45300-How-to-record-and-play-sound-simultaneously
- Qt - 没有音频输出设备 - Yocto/poky
- Qt + conan = 使用空输出设备,无可用
我不知道这是与 Fedora 相关的问题还是与 Qt 相关的问题。请帮忙!
myaudiorecorder.h:
#ifndef MYAUDIORECORDER_H
#define MYAUDIORECORDER_H
#include <QAudioFormat>
#include <QAudioDeviceInfo>
#include <QTextStream>
#include <QAudioInput>
#include <QAudioOutput>
#include <QObject>
class MyAudioRecorder : public QObject
{
Q_OBJECT
public:
MyAudioRecorder();
QAudioFormat formatIn;
QAudioFormat formatOut;
QAudioInput *m_audioInput;
QAudioOutput *m_audioOutput;
QAudioDeviceInfo m_InputDevice;
QAudioDeviceInfo m_OutputDevice;
QIODevice *m_input;
QIODevice *m_output;
QAudioDeviceInfo deviceIn;
QAudioDeviceInfo deviceOut;
void getFormat();
void createAudioInput();
void createAudioOutput();
void beginAudio();
};
#endif // MYAUDIORECORDER_H
myaudiorecorder.cpp:
#include "myaudiorecorder.h"
MyAudioRecorder::MyAudioRecorder() {
getFormat();
createAudioInput();
createAudioOutput();
}
void MyAudioRecorder::getFormat(){
formatIn.setSampleSize(8);
formatIn.setCodec("audio/pcm");
formatIn.setByteOrder(QAudioFormat::LittleEndian);
formatIn.setSampleType(QAudioFormat::UnSignedInt);
deviceIn = QAudioDeviceInfo::availableDevices(QAudio::AudioInput).at(1);
if(!deviceIn.isFormatSupported(formatIn)){
QTextStream(stdout) << " default formatIn not supported " << endl;
formatIn = deviceIn.nearestFormat(formatIn);
} else {
QTextStream(stdout) << " default formatIn supported " << endl;
}
deviceOut = QAudioDeviceInfo::availableDevices(QAudio::AudioOutput).at(0);
if(!deviceOut.isFormatSupported(formatOut)) {
QTextStream(stdout) << "1. default formatOut not supported " << endl;
formatOut = deviceOut.nearestFormat(formatOut);
}
}
void MyAudioRecorder::createAudioInput(){
m_audioInput = new QAudioInput(m_InputDevice, formatIn, 0);
}
void MyAudioRecorder::createAudioOutput(){
m_audioOutput = new QAudioOutput(m_OutputDevice, formatOut, 0);
}
void MyAudioRecorder::beginAudio(){
m_output = m_audioOutput->start();
m_input = m_audioInput->start();
}