@Client::Client(QObject *parent) :
QObject(parent)
{
Socket = new QTcpSocket(this);
connect (this->Socket,SIGNAL(connected()),this, SLOT(SendTocToc()));
connect (this->Socket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(getError()));
connect (this->Socket,SIGNAL(readyRead()), this, SLOT(readSocket()));
QAudioFormat format;
// Set up the format, eg.
format.setFrequency(32000);
format.setChannels(1);
format.setSampleSize(8);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
if (!info.isFormatSupported(format)) {
qWarning()<<"raw audio format not supported by backend, cannot play audio.";
return;
}
audio = new QAudioOutput(format, this);
connect(audio,SIGNAL(stateChanged(QAudio::State)),SLOT(finishedPlaying(QAudio::State)));
audio->setBufferSize(512000);
}
void Client::readSocket()
{
if (audioincoming)
{
if (!audio->state() == 0 )
QTimer::singleShot(3000,this,SLOT(startAudio()));
return;
}
//[...]
}
void Client::startAudio()
{
audio->start(this->Socket);
} @
Now it works. I initialize QAudioOutput only on class constructor but I must delay a bit to "load buffer". So I tried to start QAudioOutput object after 3 secs, but I would to use a more efficient way. If I don't delay, audio lags and QAudioOutput crashes.
Any suggestions?