Question

I just copied the sample code from Qt's doc , the only difference is i choose to use memory buffer instead of a local file , so i placed a QBuffer buffer in QAudio::start() , but when stateChanged emitted , buffer was empty , and result in a QAudio::IOError

Class Dummy { private: QBuffer buffer; };

void Dummpy::loop()

{
    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);
    }

    audio = new QAudioInput(format, this);
    connect (audio , SIGNAL(stateChanged(QAudio::State)) , SLOT(stateChanged(QAudio::State)));

    QTimer::singleShot(3000, this, SLOT(stopRecording()));
    audio->start (&buffer); // was originally a QFile , i put a QBuffer here
}

Why is it failing ?

EDIT


Still minimal here , no error handling:

void Window::stateChanged(const QAudio::State &state)
{
    if ( state == QAudio::StoppedState )
    {
        buffer.open(QIODevice::ReadOnly);
        qDebug() << "Finished." << buffer.readAll().length();
        buffer.close();
    }
}

And before audio->start (&buffer) ,

buffer.open(QIODevice::WriteOnly | QIODevice::Truncate);

Was it helpful?

Solution

You need to open() a QIODevice before you use it. Insert something like...

buffer.open(QIODevice::ReadWrite);

...before...

audio->start (&buffer); // was originally a QFile , i put a QBuffer here
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top