Qt QUdpsocket 音频流

Qt QUdpsocket audiostreaming

我正在尝试 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
}

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