Tiina
Tiina

Reputation: 4785

Qt - how to set audio playback starts from the beginning of buffer?

I have a buffer with a size for example 4096, and store data into it, if the buffer is full, it would be start from the beginning of the buffer. It seems like this works fine.

But I have problem with playing data from buffer.

QByteArray          m_buffer;
QBuffer             m_audioOutputIODevice;
QAudioOutput*       m_audioOutput;
m_audioOutputIODevice.close();
m_audioOutputIODevice.setBuffer(&m_buffer);
m_audioOutputIODevice.open(QIODevice::ReadOnly);
m_audioOutput->start(&m_audioOutputIODevice);

Now I can play the sound from the buffer but when it reaches the end of buffer, play stops. How could I change the code so when it reaches the end of buffer it would all start from the beginning? Thank you very much

update codes:

connect(m_audioOutput,SIGNAL(stateChanged(QAudio::State)),SLOT(resetPlayBuffer(QAudio::State)));

    void bufferPlayback::resetPlayBuffer (QAudio::State state)
{
    if (state == QAudio::IdleState) {
        m_audioOutputIODevice.close();
        m_audioOutputIODevice.setBuffer(&m_buffer);
        m_audioOutputIODevice.open(QIODevice::ReadOnly);
    }
}

Upvotes: 1

Views: 1975

Answers (2)

johnathan
johnathan

Reputation: 2355

void stateChanged ( QAudio::State state ) <~ signal for when the player changes. Hook to a slot in your class, and just repeat the playback process when the state is stopped. Simple. One of the reasons I LOVE Qt.

Upvotes: 1

Jeremy Friesner
Jeremy Friesner

Reputation: 73061

AFAICT QAudioOutput doesn't have any built-in support for audio looping, so I think you would have to simulate audio-looping by sending fresh audio buffers to the QAudioOutput device periodically so that it would never run out of audio bytes to play.

I think the easiest way to do that would be to write your own subclass of QIODevice that pretends to be a very long (infinite?) file, but returns your looping samples over and over again when queried. Then pass your QIODevice-subclass-obect as an argument to QAudioOutput::start().

Upvotes: 0

Related Questions