C ++ Qt QAudioInput + QIODevice

时间:2018-02-11 19:06:16

标签: c++ qt microphone

我的目的是从麦克风捕捉声音并实时播放。问题是,当我按下“开始”按钮时,应用程序输出中有一些消息,如QAudioInput:IOError,无法写入QIODevice。 我在看“qaudioinput”这个例子。 在评论每一行的过程中,我发现问题符合要求 audioInput->启动(devInput); 当下一行 audioOutput->启动(devOutput); 工作正常。 还有一个,有人可以解释qaudioinput示例中的推拉模式吗?互联网上没有关于它的信息。

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>
#include <QDialog>
#include <QHBoxLayout>
#include <QPushButton>
#include <QtMultimedia/QAudioFormat>
#include <QtMultimedia/QAudioInput>
#include <QtMultimedia/QAudioOutput>
#include <QByteArray>
#include <QIODevice>

class audioIn : public QIODevice
{
    Q_OBJECT
public:
    audioIn(const QAudioFormat &format, QObject *parent);
    ~audioIn();
    qint64 writeData(const char *data, qint64 len);
    qint64 readData(char *data, qint64 maxlen);
    void start();
    void stop();
private:
    const QAudioFormat m_format;
};
class audioOut : public QIODevice
{
    Q_OBJECT
public:
    audioOut(const QAudioFormat &format, QObject *parent);
    ~audioOut();
    qint64 writeData(const char *data, qint64 len);
    qint64 readData(char *data, qint64 maxlen);
    void start();
    void stop();
private:
    const QAudioFormat m_format;
};

class mainwindow:public QDialog
{
    Q_OBJECT
public:
    mainwindow(QWidget *parent = 0);
    ~mainwindow();
private:
    QHBoxLayout* layout;
    QPushButton* start;
    QByteArray buffer;
    QAudioFormat m_format;
    QAudioInput* audioInput;
    QAudioOutput* audioOutput;
    audioIn* devInput;
    audioOut* devOutput;
    QIODevice* m_input;
    QIODevice* m_output;

public slots:
    void startrec();
    void readMore();
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"

audioIn::audioIn(const QAudioFormat &format, QObject *parent):QIODevice(parent), m_format(format)
{

}
audioIn::~audioIn()
{
}
void audioIn::start()
{
    open(QIODevice::WriteOnly);
}
void audioIn::stop()
{
    close();
}
qint64 audioIn::readData(char *data, qint64 maxlen)
{
    Q_UNUSED(data);
    Q_UNUSED(maxlen);
    return 0;
}
qint64 audioIn::writeData(const char *data, qint64 len)
{
    Q_UNUSED(data);
    Q_UNUSED(len);
    return 0;
}

audioOut::audioOut(const QAudioFormat &format, QObject *parent):QIODevice(parent), m_format(format)
{

}
audioOut::~audioOut()
{
}
void audioOut::start()
{
    open(QIODevice::ReadOnly);
}
void audioOut::stop()
{
    close();
}
qint64 audioOut::readData(char *data, qint64 maxlen)
{
    Q_UNUSED(data);
    Q_UNUSED(maxlen);
    return 0;
}
qint64 audioOut::writeData(const char *data, qint64 len)
{
    Q_UNUSED(data);
    Q_UNUSED(len);
    return 0;
}

mainwindow::mainwindow(QWidget *parent): QDialog(parent)
{
    layout = new QHBoxLayout;
    start = new QPushButton("Start");
    layout->addWidget(start);
    setLayout(layout);
    connect(start, SIGNAL(clicked(bool)), this, SLOT(startrec()));
}
mainwindow::~mainwindow()
{

}

void mainwindow::startrec()
{
    m_format.setByteOrder(QAudioFormat::LittleEndian);
    m_format.setChannelCount(2);
    m_format.setSampleRate(44100);
    m_format.setSampleSize(16);
    m_format.setCodec("audio/pcm");
    m_format.setSampleType(QAudioFormat::UnSignedInt);

    QAudioDeviceInfo infoIn(QAudioDeviceInfo::defaultInputDevice());
    if(!infoIn.isFormatSupported(m_format))
        m_format = infoIn.nearestFormat(m_format);
    devInput = new audioIn(m_format, this);
    audioInput = new QAudioInput(infoIn, m_format, this);

    QAudioDeviceInfo infoOut(QAudioDeviceInfo::defaultOutputDevice());
    if(!infoOut.isFormatSupported(m_format))
        m_format = infoOut.nearestFormat(m_format);
    devOutput = new audioOut(m_format, this);
    audioOutput = new QAudioOutput(infoOut,m_format, this);

    devInput->start();
    devOutput->start();
    m_input = audioInput->start();
    m_output = audioOutput->start();

    connect(m_input, SIGNAL(readyRead()), SLOT(readMore()));
}

void mainwindow::readMore()
{
    qint64 len = audioInput->bytesReady();
    if(len > 7056)
        len = 7056;
    qint64 l = m_input->read(buffer.data(), len);
    if(l > 0)
        m_output->write(buffer.constData(), l);
}

1 个答案:

答案 0 :(得分:0)

这是你的问题:

void audioIn::start()
{
    open(QIODevice::WriteOnly);
}

如果您想从设备读取,最好在ReadOnly或ReadWrite模式下打开它....

(显然你的audioIn类需要读取数据,而不是写入)。