Qt QUdpsocket audiostreaming

时间:2017-12-26 20:55:51

标签: qt qudpsocket

我正在尝试clone QTCpsocket的音频流模型,但现在使用QUdpsocket(虚拟连接),虽然看起来代码正在执行,但是,实际上它不是做这项工作,我无法获得流式音频;

主要关注点是:是否可以使用QUDpsocket启动QAudioOutput ???

然而要清楚地提到这个代码适用于TCP socket

代码段:

在server.h文件中

private:
    QUdpSocket *socketUDP;

和.CPP文件

udpServer::udpServer(QObject *parent) : QObject(parent)
{
    socketUDP = new QUdpSocket(this);
    serverAddress = QHostAddress("192.168.1.8");
    //socketUDP->bind(serverAddress, 1357);
    socketUDP->bind(1357, QUdpSocket::ShareAddress);
    socketUDP->open(QIODevice::ReadOnly);
    connect(socketUDP, &QUdpSocket::readyRead, this, &udpServer::playStreamedAudio);

}

然后是playstream()方法:

    void udpServer::playStreamedAudio() {
      // set the QAudioFormat parameters of output audio device.
      my_QAudioFormat = new QAudioFormat;
      my_QAudioFormat->setSampleRate(48000);
      my_QAudioFormat->setChannelCount(1);
      my_QAudioFormat->setSampleSize(8);
      my_QAudioFormat->setCodec("audio/pcm");
      my_QAudioFormat->setByteOrder(QAudioFormat::LittleEndian);
      my_QAudioFormat->setSampleType(QAudioFormat::UnSignedInt);
      //
    // get default audio output device 
      audiOutputDevice = QAudioDeviceInfo::defaultOutputDevice();

      audiooutput = new QAudioOutput(audiOutputDevice,my_QAudioFormat, this);
  // attach to socket!
    qDebug() << "Playaing AudioStream";
    socketUDP->open(QIODevice::ReadOnly);
    audiooutput->start(socketUDP); // the Audio output device shall listen to server socket for audio
}

1 个答案:

答案 0 :(得分:0)

事实证明,UDP套接字可能不适合作为QioDevices进行接口...似乎它们不是那样的,数据包最好写入文件然后进行处理。