0

我只是从 Qt 的文档中复制了示例代码,唯一的区别是我选择使用内存缓冲区而不是本地文件,所以我放置了一个QBuffer bufferin QAudio::start(),但是当stateChanged发出时,buffer它是空的,并导致QAudio::IOError

Class Dummy { private: QBuffer 缓冲区;};

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
}

为什么会失败?

编辑


这里仍然很少,没有错误处理:

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

在 audio->start (&buffer) 之前,

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

4

1 回答 1

0

你需要在open()使用QIODevice它之前。插入类似...

buffer.open(QIODevice::ReadWrite);

...前...

audio->start (&buffer); // was originally a QFile , i put a QBuffer here
于 2012-01-06T15:27:09.277 回答