当前位置:网站首页>Player actual combat 12 QT playing audio
Player actual combat 12 QT playing audio
2022-06-12 14:08:00 【Sister Suo】
We mainly use the following three classes :
QAudioDeviceInfo Class provides an audio output device
QAudioFormat Class provides audio parameter settings
QAudioOutput Class provides the PCM The interface that sends raw audio data to the audio output device .
1. Import the header file and open PCM file
#include <QtCore/QCoreApplication>
#include<qaudioformat.h>
#include<qaudiooutput.h>
#include<qthread.h>
#include<iostream>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
FILE* fp = fopen("test.pcm","rb");
if (fp)
cout << " Open the success " << endl;
else
cout << " Open the failure " << endl;
2. Instantiation QAudioFormat And set the parameters
QAudioFormat fmt;
fmt.setSampleRate(44100);
fmt.setSampleSize(16);// Number of sampling bits
fmt.setChannelCount(2);
fmt.setCodec("audio/pcm");
fmt.setByteOrder(QAudioFormat::LittleEndian);// Byte order , Not bitwise , such as 16, The small end 16 Big end 61
fmt.setSampleType(QAudioFormat::UnSignedInt);
3. Open up a QAudioOutput Space , Use the object instantiated above fmt As its new Make the constructor arguments
And call QAudioOutput Of start Method to start playing ( Just turned on the switch , The data has not yet started to flow , To hear the playing sound, you need to use io->wirte() take PCM Data to play )
QAudioOutput* out = new QAudioOutput(fmt);
QIODevice* io = out->start();
4. Put the audio data to be played into the buffer queue in batches and play
int size = out->periodSize();
char* buf = new char[size];
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();
}
out->periodSize() Is the size of the incoming data
When the free size of the buffer is smaller than the size of the data put in at one time , Has reached the awaited , Wait until the extra data in the buffer is played and enough space is left before putting data into the buffer , Judge with the following statement if (out->bytesFree() < size)
int len=fread(buf, 1, size, fp); take fp Point to the PCM The data reads buf in
use buf As a media connection QT Original documents
io->write(buf, len);// Resampled PCM Pass to QT, Just started playing
边栏推荐
- 1414. minimum number of Fibonacci numbers with sum K
- 【活动早知道】LiveVideoStack近期活动一览
- 使用make方法创建slice切片的坑
- Alibaba Cloud Development Board haas510 submission Device Properties
- Des File Encryptor based on MFC framework
- Mold and remainder
- 注重点击,追求更多用户进入网站,可以选择什么出价策略?
- Hash tables, sets, maps, trees, heaps, and graphs
- How to use Android studio to create an Alibaba cloud Internet of things app
- 单总线温度传感器18B20数据上云(阿里云)
猜你喜欢
浅谈中国程序员为什么要跳槽?
TestEngine with ID ‘junit-vintage‘ failed to discover tests
[wustctf2020] selfie score query -1
编译安装基于fastcgi模式的多虚拟主机的wordpress和discuz的LAMP架构
Alibaba cloud development board haas510 parses serial port JSON data and sends attributes
Alibaba cloud development board haas510 submission device attributes
如果要打造品牌知名度,可以选择什么出价策略?
肝了一个月的原创小袁个人博客项目开源啦(博客基本功能都有,还包含后台管理)
Introduction to database system (Fifth Edition) notes Chapter 1 Introduction
Shell脚本到底是什么高大上的技术吗?
随机推荐
Interview question 17.14 Minimum number of K (almost double hundreds)
Display logs in the database through loganalyzer
Create a slice slice pit using the make method
Cmake basic tutorial - 01 a-hello-cmake
Chapter IV expression
Go zero micro Service Practice Series (II. Service splitting)
Player screen orientation scheme
[video lesson] a full set of tutorials on the design and production of Android studio Internet of things app -- all mastered during the National Day
1414. minimum number of Fibonacci numbers with sum K
Ffmpeg Learning Guide
Binary tree traversal
阿里云开发板HaaS510连接物联网平台--HaaS征文
Use of awlive structures
280 weeks /2171 Take out the least number of magic beans
SystemC learning materials
Fourteen week assignment
基于Profibus-DP协议的PLC智能从站设计
Why do Chinese programmers change jobs?
Greed issues - Egypt scores
SystemC:SC_ Thread and SC_ METHOD