6.5. Playing Sounds

There should rarely be a need for playing a sound. Sounds should be used only in games, and they should never be required for the use of your program unless, of course, your program's purpose is to play sound. Do not play sounds in the event of an error; that is the purpose of KNotify, which will be discussed shortly.

Playing sound is a simple command:


   1 
   2 KAudioPlayer::play("squish.wav");
   3 

You can also use this in a slot:


   1 
   2 QButton *button = new QButton(this);
   3 KAudioPlayer *player = new KAudioPlayer("foo.wav", button);
   4 connect(button, SIGNAL(clicked()), player, SLOT(play()));
   5 

In this example, the file $KDEDIR/share/sounds/foo.wav is played whenever button is clicked.

You can also send absolute filenames, in which case it will ignore the KDEDIR/share/sounds path.

This is not a superbly fast class, because it plays sounds via DCOP. For true real-time sound, you should use MCOP. For more information on multimedia, see Chapter 14, "Multimedia."