I'm using Qt 4.8 with Qt Creator 2.4.1 on Windows 7 Ultimate x64.
I'm taking audio input using QAudioInput
class and playing it using QAudioOutput
. There is a 2 seconds timeout after which I stop taking input and then setup the output as follows:
class MainWindow
{
// ...
QByteArray output_data;
QBuffer output_data_buffer;
QAudioOutput *audio_out;
// ...
};
MainWindow::MainWindow(QWidget *parent)
{
// ...
output_data_buffer.setBuffer(&output_data);
// ...
}
void MainWindow::audioInputStopped(QByteArray data)
{
output_data = data;
output_data_buffer.open(QIODevice::ReadOnly);
audio_out = new QAudioOutput(audio_format, this);
connect(audio_out, SIGNAL(stateChanged(QAudio::State)),
SLOT(audioOutputStateChanged(QAudio::State)));
audio_out->start(&output_data_buffer);
}
The audio format I'm using is supported by both input and output devices. I checked them using QAudioDeviceInfo::isFormatSupported()
. The 2 seconds audio (data
in audioInputStopped()
) always plays fine.
In the slot audioOutputStateChanged
, I'm always encountering QAudio::UnderrunError
error from audio_out->error()
after the buffer is finished playing. After audio_out->start()
is called, the state (passed as parameter in audioOutputStateChanged()
) and error goes as follows:
- No error. Active state.
- No error. Stopped state.
- Underrun error. Idle state.
Note that I'm stopping audio_out
in idle state following this example. Why the code is encountering underrun error? Is this normal?