我尝试播放存储在void * data
长度为data_size
的音频但收到消息:“有缓冲区下溢!”
int callback(void * data, long data_size) {
QBuffer audio_buffer;
audio_buffer.setData(static_cast<const char*>(data), data_size);
audio_buffer.open(QIODevice::ReadOnly);
QAudioFormat format;
format.setSampleSize(16);
format.setSampleRate(44100);
format.setChannelCount(1);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
// get default output device
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
// start playback
QAudioOutput output(info, format);
output.start(&audio_buffer);
// ...then wait for the sound to finish
QEventLoop loop;
QObject::connect(&output, SIGNAL(stateChanged(QAudio::State)), &loop, SLOT(quit()));
do {
loop.exec();
} while(output.state() == QAudio::ActiveState);
}
答案 0 :(得分:0)
尝试下面的一些更改
.pro文件
QT+= multimedia
还会检查是否支持格式
// get default output device
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
if (!info.isFormatSupported(format))
format = info.nearestFormat(format);
// start playback
QAudioOutput output(info, format);
output.start(&audio_buffer);