.pro文件添加 QT+=multimedia
#include <QtCore/QCoreApplication>
#include <QAudioFormat>
#include <QAudioOutput>
#include <QThread>
//如何准备pcm数据
//ffmpeg -i 15.mp4 -f s16le out.pcm
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//设置采样格式
QAudioFormat fmt;
fmt.setSampleRate(44100); //设置采样率
fmt.setChannelCount(2); //设置通道数
fmt.setSampleSize(16); //设置采样大小
fmt.setCodec("audio/pcm"); //设置编码方式
fmt.setByteOrder(QAudioFormat::LittleEndian); //设置字节序
fmt.setSampleType(QAudioFormat::UnSignedInt); //设置样本数据类型
QAudioOutput *out = new QAudioOutput(fmt);
QIODevice *io = out->start(); //开始播放
//可运行的大小
int size = out->periodSize();
char *buf = new char[size];
FILE *fp = fopen("out.pcm", "rb");
if(!fp)
{
printf("file is null!\r\n");
}
while (!feof(fp))
{
if (out->bytesFree()<size)
{
QThread::msleep(1);
continue;
}
int len = fread(buf,1,size,fp);
if (len<=0)
{
break;
}
io->write(buf,len);
}
fclose(fp);
delete buf;
buf = 0;
return a.exec();
}