Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Program crashes trying to play audio using QAudioOutput
Forum Updated to NodeBB v4.3 + New Features

Program crashes trying to play audio using QAudioOutput

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 444 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ERSUCC
    wrote on last edited by
    #1

    I am trying to use QAudioOutput to play an endless sine wave from a byte array. When I run it, the program crashes just after the audio device's state changes to ActiveState. When I use debugging mode, it says the crash occurs in QBuffer::readData(). Here is the creation of the buffer I am trying to use:

    QByteArray buffer;
    
    for (int i = 0; i < SAMPLE_RATE; i++)
    {
        buffer.append(QString("%1").arg(sin(2 * M_PI * (double)i / SAMPLE_RATE)));
    }
    
    audioPlayer->play(&buffer);
    

    Here is the play function in AudioPlayer:

    void AudioPlayer::play(QByteArray *data)
    {
        QMetaObject::invokeMethod(&audioThread, "play", Qt::QueuedConnection, Q_ARG(QByteArray, *data));
    }
    

    Here is the play function in AudioThread (buffer is a QBuffer member variable):

    void AudioThread::play(QByteArray data)
    {
        buffer.setBuffer(&data);
        buffer.open(QIODevice::ReadOnly);
    
        audioOutput->setBufferSize(buffer.size());
        audioOutput->start(&buffer);
    }
    

    The last line of play in AudioThread is the last line that I can trace in my code before the error occurs, and I'm guessing it is the line with the problem, because I never hear any audio. Thanks in advance for your help, and let me know if I should include any other sections of my code.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      @ERSUCC said in Program crashes trying to play audio using QAudioOutput:

      buffer.setBuffer(&data);

      I think this line is the cause of the crash.
      data here is a local variable, it will be destroyed at the end of this function.
      You set buffer to use it as internal buffer, but it is not accessible after that.

      void QBuffer::setBuffer(QByteArray *byteArray)
      ...
      The caller is responsible for ensuring that byteArray remains valid until the QBuffer is destroyed, or until setBuffer() is called to change the buffer.

      Try use buffer.setData(data) instead.

      1 Reply Last reply
      6
      • E Offline
        E Offline
        ERSUCC
        wrote on last edited by
        #3

        That worked! Thank you so much!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved