How can I play a sound file using Qt 5 and C++? I have tried QSound
but I am told it does not workin Ubuntu (my current Operating System) and I have heard of Phonon
but the library does not seem to be available in my Qt Package.
Asked
Active
Viewed 4,536 times
0

Omari Celestine
- 1,405
- 1
- 11
- 21
-
What makes you think QSound doesn't work on Ubuntu? – MrEricSir Aug 01 '17 at 00:12
-
When I tried it, it did not work and after viewing some information about one it it's functions to know if it works with my system or not, I got a false response. – Omari Celestine Aug 01 '17 at 00:22
-
You'll either need to get Qt's multimedia APIs working, in which case you can use QSound or QAudioOutput or similar, or find some non-Qt-based API to use for playing sounds. If you program is a Qt-based app, then the former approach would be preferable. – Jeremy Friesner Aug 01 '17 at 00:25
-
How is this different from https://stackoverflow.com/questions/14296326/how-to-play-sound-in-qt5-qt4-migration ? – Paul Rooney Aug 01 '17 at 00:36
-
I did not come across this but I don't seem to have the 'Qt Multimedia' library and I cannot find it in the repos. – Omari Celestine Aug 01 '17 at 00:45
2 Answers
6
Qt5
QFile inputFile;
QAudioOutput* audio;
inputFile.setFileName("/tmp/test.raw");
inputFile.open(QIODevice::ReadOnly);
QAudioFormat format;
// Set up the format, eg.
format.setFrequency(7600);
format.setChannels(1);
format.setSampleSize(6);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
if (!info.isFormatSupported(format)) {
qWarning()<<"raw audio format not supported by backend, cannot play audio.";
return;
}
audio = new QAudioOutput(format, this);
connect(audio,SIGNAL(stateChanged(QAudio::State)),SLOT(finishedPlaying(QAudio::State)));
audio->start(&inputFile);
C++
Use BOOL PlaySound(LPCTSTR pszSound, HMODULE hmod, DWORD fdwSound);
#pragma comment (lib, "winmm.lib")
...
PlaySound(TEXT("recycle.wav"), NULL, SND_ASYNC);
Set SND_
option asSND_ASYNC
.
Anyway you can listen the audio.

Bryant
- 324
- 2
- 15
-
Isn't `PlaySound` a Windows function? I thought OP is using Ubuntu. Not to mention, the Qt code looks like it was completely copied from [Bart's answer to 14296326](https://stackoverflow.com/a/14296398/14227825); under CC BY-SA 3.0 you should at least attribute. – phetdam Mar 05 '22 at 07:02
1
Install the package sox
that contains the utility play
. Or you can use mplayer
or any console media player. Then use QProcess
to start playing sound file.
#include <QProcess>
.......
QProcess qprocess;
qprocess.startDetached("play",QStringList()<<"wav/alarm-clock-01.wav");
.......

Michael
- 5,095
- 2
- 13
- 35
-
Thanks, this seems to work for me but it does I am getting an error when using the audio files from my 'qrc' resources file. any ideas? – Omari Celestine Aug 01 '17 at 00:50
-
@OmariCelestine As far as I know, an external program (one launched with QProcess) does not have access to the resources in the application. In any case, I would not consider this option as the best option since it does not allow for interaction with the player, such as pausing, playing etc. Rather consider the answer of Bryant if you need access to resources or if you want to interact with the player. – CJCombrink Aug 01 '17 at 06:53