我的目的是从麦克风捕捉声音和实时播放。问题是,当我按“开始”按钮时,应用程序输出中有一些消息,如QAudioInput: IOError,无法写入QIODevice。我在看"qaudioinput“这个例子。在对每一行进行注释时,我发现问题出现在line audioInput->start(devInput);下一行audioOutput->start(devOutput);正常工作。再来一次,有谁能解释一下定量输入例子中的推拉模式吗?互联网上没有关于这方面的信息。
主窗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_Hmainwindow.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);
}发布于 2018-02-11 21:01:15
这是你的问题:
void audioIn::start()
{
open(QIODevice::WriteOnly);
}如果您想从设备中读取,最好在ReadOnly或ReadWrite模式下打开它.
(显然,您的audioIn类需要读取数据,而不是写入数据)。
https://stackoverflow.com/questions/48735303
复制相似问题