I'm having an issue playing a simple 'wav' file using QAudioOutput. Previously I used the QSound::PlaySound() to play the same wav and it worked fine, however I wanted a bit more control (mainly stop, pause, resume). However when I play the sound (any wav file) using QAudioOutput I get a 'pop' noise at the start of every sound. This did not occur with the QSound::PlaySound().

I've tried tweaking the QAudioFormat, but no luck there. I did basically just copy the code from the QAudioOutput help documents, with the exception that I determine the QAudioFormat by the WAV file's header. Note that I've also tested setting the QAudioFormat manually so I'm fairly confident the error is not there.

So basically when I want to play my wav file, I create a new SoundPlayer object, passing it the filename as a QString. The sound is played once the object is constructed.

SoundPlayer Constructor

Qt Code:
  1. SoundPlayer::SoundPlayer(QString fileName, QWidget *parent) :
  2. QWidget(parent)
  3. {
  4. this->inputFile.setFileName(fileName);
  5. this->inputFile.open(QIODevice::ReadOnly);
  6.  
  7. QAudioFormat format = getWaveFormat(fileName);
  8. format.setCodec("audio/pcm");
  9.  
  10. this->audio = new QAudioOutput(format, this);
  11.  
  12. connect(audio, SIGNAL(stateChanged(QAudio::State)), SLOT(finishedPlaying(QAudio::State)));
  13.  
  14. //play the sound file 'inputFile'
  15. audio->start(&inputFile);
  16.  
  17. }
To copy to clipboard, switch view to plain text mode