Reputation: 4785
I am a freshgirl to Qt. (C++ as well)
I saw an example from QAudioInput class reference, and in order to pass the compile I made a little change (all the difference you see from my code to the example is because of compile failed at first).
I looked up a lot on the Internet, still got nothing. And the example http://doc.qt.io/archives/qt-4.7/multimedia-audioinput-audioinput-h.html does work. And I read it carefully, nothing different except mine is to save to a file, and that one is to save to a buffer.
so, please help me with 1, what are the reasons to cause QAudioInput: IOError (I know there is problem on IO devices, but what is the problem?) 2, how could I make the code work?
Here are the code:
//audioprocess.h
#ifndef AUDIOPROCESS_H
#define AUDIOPROCESS_H
#include <QAudioInput>
#include <QFile>
#include <QDebug>
#include <QTimer>
#include <QObject>
class audioprocess : public QObject
{
Q_OBJECT
public:
void startRecording();
private slots:
void stopRecording();
private:
QFile outputFile; // class member.
QAudioInput *audioInput; // class member.
};
#endif // AUDIOPROCESS_H
//----------------------------------------------
//audioprocess.cpp
#include "audioprocess.h"
void audioprocess::startRecording()
{
outputFile.setFileName("/audio_qt.raw");
outputFile.open( QIODevice::WriteOnly | QIODevice::Truncate );
QAudioFormat format;
// set up the format you want, eg.
format.setFrequency(8000);
format.setChannels(1);
format.setSampleSize(8);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);
QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
if (!info.isFormatSupported(format)) {
qWarning()<<"default format not supported try to use nearest";
format = info.nearestFormat(format);
}
audioInput = new QAudioInput(format,reinterpret_cast<QObject*>(this));
QTimer::singleShot(100, reinterpret_cast<QObject*>(this), SLOT(stopRecording()));
audioInput->start(&outputFile);
// Records audio for 3000ms
}
void audioprocess::stopRecording()
{
audioInput->stop();
outputFile.close();
delete audioInput;
}
//--------------------------------------------------------
//main.cpp
#include <QtGui/QApplication>
#include "audioprocess.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
audioprocess audiorecord;
audiorecord.startRecording();
return a.exec();
}
Upvotes: 1
Views: 4320
Reputation: 4750
Are you sure that the application has the rights to write into the root folder (file name = "/audio_qt.raw")?
I would try to write into the user's home folder.
A quick test to check if this is really the problem is to check the return value of outputFile.open()
(should return true).
Upvotes: 1