speed488
speed488

Reputation: 306

Qt QAudioOutput StateChnged always on ActiveState (Qt 5.1 Windows MinGW)

I have a derived class from QObject that has a bunch of concatenated WAVE files in a QByteArray as a member variable.

I want to play specific files in that array (provided that I have the offset of it) through a QAudioOuput.

Here is the code of the PlaySound Method:

void DRMUtils::PlaySound(int offset){

    mAudioFormat = new QAudioFormat(GetFormat(offset));
    mAudioOut = new QAudioOutput(*mAudioFormat);
    mBufferedAudio = new QBuffer();
    mBufferedAudio->setData(GetSound(offset));
    mBufferedAudio->open(QIODevice::ReadOnly);


    connect(mAudioOut, SIGNAL(stateChanged(QAudio::State)), this, SLOT(handleAudioStateChanged(QAudio::State)));
    mAudioOut->start(mBufferedAudio);
}

I get the file format from the 44 first bytes (WAVE standard) with GetFormat(offset) and I get the data in QByteArray format with GetSound(offset)

Everything seems to work fine (I can hear the sample play), but the state of QAudioFormat never changes from "ActiveState"

Here is my slot code:

void DRMUtils::handleAudioStateChanged(QAudio::State newState)
{
    qDebug() << "State: " << newState;
    switch (newState) {
    case QAudio::IdleState:
        // Finished playing
        mAudioOut->stop();
        mBufferedAudio->close();
        delete mAudioOut;
        delete mBufferedAudio;
        delete mAudioFormat;
        qDebug() << "DELETED!";
        break;

    case QAudio::StoppedState:
        // Stopped for other
        qDebug() << "STOPPED!";
        if (mAudioOut->error() != QAudio::NoError) {
            // Error handling
            qDebug() << "STOPPED ERROR!";
        }
        break;

    default:
        // ... other cases as appropriate
        qDebug() << "DEFAULT!";
        break;
    }
}

My Debug output is always:

State:  ActiveState
DEFAULT!

Do I have to "cap" the mBufferedAudio in some way that QAudioOutput "knows" when the sample is completed?

Also, I anybody can tell me when why when I initialize mAudioOut like this (declaring the parent):

mAudioOut = new QAudioOutput(*mAudioFormat, this);

instead of (not declaring the parent):

mAudioOut = new QAudioOutput(*mAudioFormat);

I don't get any output to the speakers (with parent declared).

Thanks

Upvotes: 1

Views: 652

Answers (1)

Pavel Strakhov
Pavel Strakhov

Reputation: 40502

May be this gets deleted? So mAudioOut is deleted too when the parent is specified. It would also explain why you don't receive state changes (if the object gets deleted, the slot cannot be called anymore).

Upvotes: 1

Related Questions