QT播放PCM音频实例详细

#include
#include
#include
#include
#include
using namespace std;
//准备好PCM数据
//ffmpeg -i  PP.mp4 -f s16le out.pcm
int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);
	
	QAudioFormat fmt;
	fmt.setSampleRate(16000);//样本率
	fmt.setSampleSize(16);//样本大小
	fmt.setChannelCount(2);//通道大小
	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("16k.pcm", "rb");
	if (!fp)
	{
		cout << "文件打开失败" << endl;
	}
	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;
	return a.exec();
}

 

你可能感兴趣的:(流媒体音视频,ffmpeg,qt)