質問

I am playing audio in my code like this:

// decode routine

QAudioFormat format;
format.setFrequency(aCodecCtx->sample_rate);
format.setChannels(2);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
if (!info.isFormatSupported(format)) {
    cout<<"raw audio format not supported by backend, cannot play audio." <<endl;
    format = info.nearestFormat(format);
}
QAudioOutput * audio = new QAudioOutput(format);
connect(audio,SIGNAL(stateChanged(QAudio::State)),SLOT(stateChanged(QAudio::State)));
if( !buffer->open(QBuffer::ReadWrite) )
    cout << "Couldnt open Buffer" << endl;
cout << "buffer.size()=" << buffer->size() <<endl;
audio->start(buffer);

I was running this code in a worker thread because the decoder routine is heavy. But no sound was playing. I shifted this code to the main thread and everything works fine.

Why is this so? The QAudioOutput doc does not say anything about which thread it needs to run on

役に立ちましたか?

解決

I forgot to start an event loop in the worker thread. Otherwise the thread exits and that's why the audio does not play

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top