我正在尝试设置一个简单的工作示例来播放.raw文件,音频似乎被扭曲了。当.raw文件播放时,我仍然可以看出所有内容,它只是相当扭曲,就像收听超出范围的广播电台一样。
QString mResourcePath ="test.raw";
QFile audio_file(mResourcePath);
if(audio_file.open(QIODevice::ReadOnly)) {
audio_file.seek(4); // skip wav header
QByteArray audio_data = audio_file.readAll();
audio_file.close();
QBuffer audio_buffer(&audio_data);
audio_buffer.open(QIODevice::ReadOnly);
qDebug() << audio_buffer.size();
QAudioFormat format;
format.setSampleSize(8);
format.setSampleRate(8000);
format.setChannelCount(1);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::BigEndian);
format.setSampleType(QAudioFormat::UnSignedInt);
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
if (!info.isFormatSupported(format)) {
qWarning()<<"raw audio format not supported by backend, cannot play audio.";
return;
}
qDebug() << info.deviceName();
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 :(得分:1)
你的问题的标题表明你想播放u-law音频,这是对数PCM。但是,这条线
format.setCodec("audio/pcm");
初始化线性PCM的播放。 2种可能的解决方案: