C++ Qt Game Tutorial 7 - Adding Sound Effects/Music

We cover resource files, QMediaPlayer, setMedia(), QUrl, and play(). Code for this tutorial:

Video tags:


Thanks dude, I am following along in Python and this series has been really helpful so far!
Error in line #include no such file or directory in pro file i have already included multimedia but still same error plz help.
Can we change setPos of the bullet x axis and yaxis in void move() when Qt left and rightkey is pressed ? how plz help thanks ..
Search on google'mario world soundtrack download'then scrolldown and click 4-11 - Athletic BGM.mp3 you will love that song.
Hi Abdullah! thanks for an awesome tutorial :)just one question thu, I have tried to add sound to when the bullet hit the plane but came no where ! how would u go about it? thanks in advance
Game can't start when I try to add a 4 min music file as background music...
When I try to play the music, it closes T-T What can I do?? ++ Now it's not closing, but it doesn't play the music D,:
Cannot find -|qtmultimedia In the usr/include folder there are qt4 and qt5 folders.qt4 does not contain qtmultimedia.It must be that my QT Creator is not looking into the qt5 folder.
This helped me. Thank you.
If someone cannot listen the sounds on your computer it's because of missing the codec, try to install the codec for your system.
Hello, I'm trying to play a wav file  from a QMediaPlayer each time a QPushButton is clicked, but when I do I get a serious delay - 500 ms aproximatively for a file that is not even 1 sec long... I don't get what could be wrong.
I'm encountering an issue: when I press the space key many time in a short duration, the app will not respond (hang out). I tried to create a small GUI app that will play the sound with QMediaPlayer when the user presses the space but the issue was repeated.
Hi, I write the same code as you did, and I tried 4 different mp3 music, but only 1 succeed to play and I did hear the background music, the other 3 didn'tmake a sound. Is it a bug or just something is wrong with my mp3 music?
Hi man, first, this is a good tutorial video but i have a little problem;Is QMediaPlayer exist in Qt 5 or more only, am I right? because im using Qt 4.8 and I don't can find QMediaPlayer
Thank you. Your information in this tutorial is util. Continue doing it.
I am getting this unused parameter 'parent' errror for Game::Game(QWidget *parent) on the parent object
Hey, thanks for the tutorials, have been helping a lot! just one question, is there any reason you did not just put bulletSound->stop() bulletSound->play() ? or is it too slow to execute? thanks?
Hi and thanks i have an error wen i want to create an object from QMediaPlayer i added multimedia in .pro and included QMediaPlayer in heder but i give : (Game_Tutorial:5500): GStreamer-CRITICAL **: gst_object_ref_sink: assertion 'GST_IS_OBJECT (object)' failed i tried to solve it but i couldn't I'm using ubutnu gnome 16.04 and qt 5.6
Hi Abdullah! There are two memory leaks in the code of this video. For each call to the new operator, a delete operation should be invoked. Fortunately, Qt has the QObject's parent/child mechanism. Instead of     QMediaPlayer * music = new QMediaPlayer(); Provide a parent for the QMediaPlayer object in its constructor:     QMediaPlayer * music = new QMediaPlayer(this); A tip: Typing a "qrc://path/to/resource" is error prone. You can right click the resource in the sidebar and select the "Copy url qrc://..." option, then paste the url in your code. Best regards!
Went to: tool/options/Qt Versions and changed my qmake location to /usr/lib/x86_64-linux-gnu/qt5/bin/qmake
Hi Abdullah, How can I infinitely loop the background music? Thanks!
I get the next error : " 'music' does not name a type".
Thanks for all your work, Abdullah.It's explained many things and has been a big help getting me started. I am unable to get sounds to work.I'm using Qt 5.x, Creator 3.5.x, and Windows 10. Are there alternatives to the Code you use in your tutorials, some way of playing sounds that might be more friendly with the OS I'm using? Otherwise can you advise me on what I should try to do next to fix this.A Codec pack, or plug-in...? Thanks
Hi Abdullah, First of all, good job! Unfortunately, I'm having some problems to play the bullet sound. When I run the program, the sound works a few times, but then stops working. When it happens seems to be totally random, sometimes after I press space only once, sometimes after I press many times. Btw, my OS is Ubuntu 14.04. Thanks
I don't have a QMediaPlayer library, are you you using Qt 5 or did you have to download something else?
It appears not to be possible to play sounds from a resource file using QMediaPlayer (at least  on the Mac). I gave up on the background music but the effects can be played out of resource files if you use QSoundEffect with code like: effect = new QSoundEffect(this); effect->setSource(QUrl::fromLocalFile("/sounds/firing.wav")); in the constructor of the player, and if (effect->isPlaying()){   effect->stop();  // for rapid fire } effect->play(); in the keyPressedEvent() method of the player
Nice! i was struggling to get sounds to play in qt. thanks!
I have ubuntu 14.04 and this occurs ERROR: Unknown module(s) in QT: multimedia solved it with $ sudo apt-get install qtmultimedia5-dev libqt5multimediawidgets5 libqt5multimedia5-plugins libqt5multimedia5 and restart qt ofc for more infoEDIT: but now it says GStreamer; Unable to play - "qrc:/sounds/Super Mario Bros. Music - Ground Theme.mp3" Error: "Could not determine type of stream."
Hi Abdullah! On debian i got this error: Warning: "No hay un decodificador disponible para el tipo \u00ABaudio/mpeg, mpegversion=(int)1, mpegaudioversion=(int)1, layer=(int)3, rate=(int)44100, channels=(int)2, parsed=(boolean)true\u00BB." And i solved install the gstreamer just like this: su apt-get install python-gst0.10 gstreamer0.10-plugins-good \     gstreamer0.10-plugins-ugly But, i had another error, and i dont know how to fix it, if i click out of the window of the game, the back ground music continue playing, but my bullets don't, if you know how to solve this i really appreciate that you post how to. Thank you for your time, and this amazing tutorial series. Greetings !
Hi Abdullah, Just out of curiosity, and if you would mind giving us some hints either in theory or Qt practice of how to plan and implement scrolling background for the first game(the airplane), and thank you for your valuable time and help.

C++ Qt Game Tutorial 8 - Adding Graphics

We cover qgraphicspixmapitem, qpixmap, and qimage. code for this tutorial:

C++ Qt Game Tutorial 6 - Adding Player Health and Score

We cover qgraphicstextitem, setplaintext(), setfont(), qfont, setdefaulttextcolor(). we also talk about qgraphicitem parent/children. code for this tutorial:

C++ Qt Game Tutorial 1 - Drawing the Player (rectangle)

We begin our journey of making games with c++ and qt by gently introducing 3 fundamental qt classes that you will use in every single one of your games. specifi

Snake Game made Using C++ / Qt using Graphics

This game has been made using c++/qt music: www.bensound.com source:

C++ Qt 12 - QFile

How to work with the qt qfile class to write and read files

(C++) How to play Sound (Code::Blocks)

Make sure you #include windows.h, before you #include mmsystem.h. in linker i added "winmm". playsound(text("yourmusicfile.wav"), null, snd_sync);all music in t

Qt 5 Demo

The following is a quick tour of what is new in qt 5. it is an application written with qt quick, based on qt 5. the source\ncode is available from:the video i

Qt Calculator GUI Tutorial Part 1

We have a new name now, programming liftoff! learn how to design a user interface for a calculator using qt's drag-and-drop gui designer.this is part one in a

Qt Creator Kit Setup

This video shows how to create and setup a kit in qt creator for deploying qt applications onto your beaglebone.

Music Player using C++

Sorry for the sound quality! there was some problem with the video recorder! ;) subscribe and message me for the tutorial!

Qt Tutorials For Beginners 18 - QAction, QMenu, QToolBar

Qaction class c++ - how to add a list of qactions to a qmenu and handle checkable qactions (menu items) with icons qt5 tutorial qactions qaction class referen

C++ Qt Game Tutorial 9 - Starting a Tower Defense Game

We start creating a tower defense game. we review several important concepts and learn several new concepts. new concepts include qvector, qpoint, qline, qpolyg

Qt Tutorials For Beginners 31 - QPrintDialog and QPrinter

How do i set a custom page/paper size in qprinter printing - qt how to print information via qprinter? c++ - qprinter resolution is wrong in linux. c++ - qt qp

Qt 5 and OpenGL - Terrain Tessellation Example

Music: small example showing qt5 and opengl being used to render a heightmap based terrain using opengl 4 tessellation shaders. blog post at

C++ Qt 74 - QPainter and QPen

Learn how to drawn on the screen with qpainter and qpen

C++ Qt Game Tutorial 7 - Adding Sound Effects/Music