Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How can I use QUdpSocket and QAudioOutput for streaming?



  • I need to streaming a song *.wav file over LAN network. I tried to use this How i can play streaming audio over Ethernet in Qt? but don't works. I tried to create a buffer and put it to play at the same time, but there is no sound at all. The idea is that I transmit an audio file and play at the same time through the udp, as if it were a radio.

    Receiver:

    void Widget::on_listenStation_clicked()
    {
    
        quint16 port = ui->portStation->value();
        QHostAddress ip;
        ip.setAddress( ui->lineEdit_Ip->text());
        uSocket->bind( port, QUdpSocket::ShareAddress);
        connect(uSocket, SIGNAL(readyRead()),this, SLOT(processPendingDatagrams()));
    
        ui->textEdit->append("Listening");
    }
    
    void Widget::processPendingDatagrams()
    {
        char *data;
        QByteArray datagram;
        QHostAddress address;
    
    
        QAudioOutput* audio;
        QAudioFormat format;
        format.setSampleRate(44100);
        format.setChannelCount(2);
        format.setSampleSize(16);
        format.setCodec("audio/pcm");
        format.setByteOrder(QAudioFormat::LittleEndian);
        format.setSampleType(QAudioFormat::UnSignedInt);
    
    
        while (uSocket->hasPendingDatagrams()) {
    
            datagram.resize (uSocket->pendingDatagramSize ());
            qint64 writtedBytes = uSocket->readDatagram (datagram.data (), datagram.size (), &address);
            QBuffer buffer(&datagram);
            buffer.open(QIODevice::ReadOnly);
            //   data = datagram.data ();
    
            audio = new QAudioOutput(format);
            audio->start(&buffer);
            buffer.close();
    
            qDebug()<<datagram.data();
            qDebug() << "Recive" << writtedBytes << " bytes.";
        }
    
    
    }
    

    sender:

    void Widget::on_pushButton_enviar_clicked()
    {
    
            QByteArray data;
           //Data.append("Hello UDP");
            QFile file("G:/LinkinPark2.wav");
            if(!file.open(QFile::ReadOnly)){
                QMessageBox::warning(this, "title", "file not open");
            }
            //QTextStream out(&Data, QIODevice::WriteOnly);
            for(;;){
                if(!file.atEnd()){
                        // 320
                       data = file.read(320);
                       qint64 writtenBytes = mSocket->writeDatagram(data, QHostAddress::LocalHost, 1235);
                       qDebug() << "Sent " << writtenBytes << " bytes.";
                }else{
                    break;
                }
                    qDebug()<<data;
            }
            file.flush();
            file.close();
    }
    



Log in to reply