QT audio communication problems

Source: Internet
Author: User

QT audio communication problems

Today, I want to talk about something new. The function of using QT to implement audio communication is not very high. Haha, in fact, we just use some interfaces to do some simple work, it's not for you to write a transmission protocol and collect Audio Information. Well, let's talk about the specific process of implementing audio communication in QT!

First, if we use the UDP communication protocol, the Audio Information Collection class, and the standard input and output class, then we are the following classes:

QUdpSocket: class used for udp Communication in QT

QAudioInput: audio collection class

QIODevice: standard input/output device

QAudioOutput: Data audio class

Audio collection Port

The network module and multimedia module are used separately in the classes used above. Therefore, you need to add network and multimedia support in the project file (*. pro file ):

1 QT + = multimedia2 QT + = networkView Code

First, we need to set the audio collection format and the type of transmitted data packets on the audio collection port, and use the QAudioInput object to collect audio data in the specified format. The Code provides the following:

1 MainWindow: MainWindow (QWidget * parent): 2 QWidget (parent), 3 ui (new Ui: MainWindow) 4 {5 ui-> setupUi (this ); 6 // set the sound format 7 QAudioFormat; 8 format. setSampleRate (8000); // set the sampling frequency to 9 format. setChannelCount (1); // set the channel count to 10 formats. setSampleSize (16); // set the sample size, which is generally 8 or 1611 format. setCodec ("audio/pcm"); // set the encoding format to 12 format. setSampleType (QAudioFormat: SignedInt); // set the sampling type 13 format. setByteOrder (QAudioFormat: LittleEndian); // set the byte order to 14 15 input = new QAudioInput (format, this); 16 17 videoWriteSocket = new QUdpSocket (this ); 18}View Code

Next, we need to define the data packet format for Audio Information transmitted between networks:

1 // audio data packet format 2 struct videoPack {3 char data [1024]; 4 int lens; 5 };View Code

Now we can collect the audio data. The next step is to write the audio data to the standard input device and write it to the broadcast Port:

1 inputDevice = input-> start (); 2 connect (inputDevice, SIGNAL (readyRead (), 3 this, SLOT (onReadyRead ()));View Code 1 void MainWindow: onReadyRead () 2 {3 qDebug () <"send audioinput .... "; 4 videoPack vp; 5 memset (& vp, 0, sizeof (vp); 6 // read audio data 7 vp. lens = inputDevice-> read (vp. data, 1024); 8 videoWriteSocket-> writeDatagram (const char *) & vp, 9 sizeof (vp), 10 QHostAddress ("224.0.0.0"), 11 10000); 12}View Code

Now, through the above steps, you can write the audio data to the broadcast port in the specified format, next, you only need to read our Audio Information on the read port.

Audio reading Port

First, we need to set the read port to be exactly the same as the audio collection format on the audio collection port and the data packet type transmitted. Otherwise, you cannot process the Audio Information.

Now, let's take a look at how to use the udp port to read Audio Information. The Code is as follows:

1 MainWindow: MainWindow (QWidget * parent): 2 QWidget (parent), 3 ui (new Ui: MainWindow) 4 {5 ui-> setupUi (this ); 6 // window udp socket object 7 videoReadSocket = new QUdpSocket (this); 8 // bind to broadcast port 9 videoReadSocket-> bind (QHostAddress: AnyIPv4, random, 10 QUdpSocket :: shareAddress11 | QUdpSocket: ReuseAddressHint); 12 // Add to broadcast group 13 videoReadSocket-> joinMulticastGroup (QHostAddress ("224.0.0.0 ")); 14 // if there is a socket action that will trigger the readyRead SIGNAL, we will read the Audio Information 15 connect (videoReadSocket, SIGNAL (readyRead (), 16 this, SLOT (readyReadSlot (); 17 // set the audio format 18 QAudioFormat; 19 format. setSampleRate (8000); // set the sampling frequency to 20 format. setChannelCount (1); // set the channel count to 21 formats. setSampleSize (16); // set the sample size, which is generally 8 or 1622 format. setCodec ("audio/pcm"); // set the encoding format 23 format. setSampleType (QAudioFormat: SignedInt); // set the sampling type to 24 format. setByteOrder (QAudioFormat: LittleEndian); // set the byte order to 25 output = new QAudioOutput (format, this ); 26 27 // enable the interface for reading audio data 28 outputDevice = output-> start (); 29}View Code

Let's take a look at the processing code of the slot function in readyReadSlot.

1 void MainWindow: readyReadSlot () 2 {3 qDebug () <"video reveiver... "; 4 videoPack vp; 5 memset (& vp, 0, sizeof (vp); 6 videoReadSocket-> readDatagram (char *) & vp, sizeof (vp )); 7 outputDevice-> write (vp. data, vp. lens); 8}View Code

After a series of procedures above, we can try to see if we can accept the audio information. In addition, the object used in the constructor, I have included his declaration in the declaration of this class and appeared in the class as a private member, so I will not give it, you just need to add the corresponding declaration to the header file.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.