我正在像这样在 QThread 中播放音频流:
// Setup
QAudioFormat format;
format.setFrequency(44100);
format.setChannels(2);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
format = info.nearestFormat(format);
this->m_AudioOutput = new QAudioOutput(format, this);
DECLARE_ALIGNED(16,uint8_t,audio_buffer)[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
// Playback
QIODevice *iodevice = this->m_AudioOutput->start();
for(;;) {
// Routine that fetches audio data from network
// data_size is length of the buffer
fetch_packet(&audio_buffer, data_size);
qint64 dataRemaining = data_size;
const char *b2 = (const char *)audio_buffer;
while (dataRemaining) {
qint64 bytesWritten = iodevice->write((const char *)b2, dataRemaining);
dataRemaining -= bytesWritten;
b2 = b2 + bytesWritten ;
}
msleep(10);
}
音频播放得很好,但应用程序的内存消耗似乎随着时间的推移而增加(大约每分钟 2MB)。我想知道我是否做错了什么。我想QAudioOutput
应该负责QIODevice
在读取并用于播放后删除 's 缓冲区?