14.6. KDE Multimedia Besides MCOP

As you have seen in the previous sections, MCOP is the basis of many multimedia things, and it allows you many freedoms. However, in the very common situation in which you just want to notify the user by playing a sample, there are simpler interfaces. Also, not all media types have been integrated so far; I'll try to briefly address the other possibilities that are available.

14.6.1. KNotify API and KAudioPlayer

There are two very simple ways to get a sound played in a KDE application. Of course, they use the aRts soundserver. However, they are available in the kdecore library and require no extra libraries. Thus, they are the most convenient forms of doing audio.

First is the KAudioPlayer class (declared in kaudioplayer.h). It is supposed to play a sound file once, and without feedback. It works like this:

   2 KAudioPlayer::play("/var/share/foo.wav");

You see, it's simple. It also has the capability to use signals and slots to play files. That looks like the following:

   2 KAudioPlayer player("/var/share/foo.wav");
   3 connect(&btn,SIGNAL(clicked()),&player,SLOT(play()));

However, for some applications, configurable sound events would be nicer for the user. For instance, how can you know which sound somebody prefers when getting new mail? How can you know whether the user prefers a sound at all, and not simply a message onscreen?

Of course, every application could write the configuration for that itself. There is a better solution, however. Using the KNotify API, you create an eventsrc, where you describe your events. The user can reconfigure them later. The following is a sample eventsrc file (which needs to be installed in $KDEDIR/share/apps/keventtest/eventsrc):

   2 [!Global!]
   3 Name=keventtest
   4 Comment=Event Test Program
   5 [newmail]
   6 Name=New Mail
   7 Comment=Occurs when you've got new mail
   8 default_sound=/var/samples/samples/011.WAV
   9 default_presentation=1

And here is how to use it:

   2 KNotifyClient::event("newmail");

The user now can disable this event, specify another sound file, or make it a message box instead of a sound file in the KDE control center. This is why you should prefer events to simply playing files in most cases.

14.6.2. LibKMid

If you want to have background MIDI music for games and similar things, there is LibKMid. It comes with the capability to read and play MIDI files. It also does fork itself, so you don't have to care that it keeps running while you do longer calculations.

I think that the following example will show the most essential things you need to know:

   2 KMidSimpleAPI::kMidInit();
   3 KMidSimpleAPI::kMidLoad("fancymusic.mid");
   4 KMidSimpleAPI::kMidPlay();
   6 sleep(30);  /* of course, you can do anything here */
   8 KMidSimpleAPI::kMidStop();
   9 KmidSimpleAPI::kMidDestruct();

The initialization and loading are done with one function call each. After you have triggered playing (kMidPlay), you can do anything you like (for instance, for a visual application, return into the event loop). LibKMid will care that the MIDI file keeps running in the background.

14.6.3. aKtion

Finally , a hint about aKtion. Video isn't integrated in MCOP yet. It probably will be someday. However, that doesn't mean that there is no way to play videos. Based on the xanim decoders, aKtion is able to play most common video formats. It can, of course, be used as standalone application.

However, it is also available as an embeddable part via the KParts technology. That makes it suitable for using it inside Konqueror, but also inside your application.