使用ffmpeg准备pcm音频数据
ffmpeg -i 1080.mp4 -f s16le out.pcm
QT += multimedia
配置QAudioOutput播放参数
QAudioFormat fmt;
fmt.setSampleRate(44100);
fmt.setSampleSize(16);
fmt.setChannelCount(2);
fmt.setCodec("audio/pcm");
fmt.setByteOrder(QAudioFormat::LittleEndian);
fmt.setSampleType(QAudioFormat::SignedInt);
QAudioOutput * out = new QAudioOutput(fmt);
开始播放,并返回QIODevice
QIODevice * io = out->start();
读取文件,并播放,播放完成后释放资源
FILE * fp = fopen("../../raw/out.pcm", "rb");
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;