Adventures in 2D computer game development: Qt’s not-so-user-friendly Phonon media playing class

Phononnnnn! Khannn! Khan!

Here’s the code snippet Nokia provides at their documentation site regarding the Phonon class:

Phonon::MediaObject *music =
         Phonon::createPlayer(Phonon::MusicCategory,
                              Phonon::MediaSource("/path/mysong.wav"));
     music->play();

That’s just to play a simple audio file. It’ll work provided the documentation was presented in proper order! Ironically, I scoured the Internet for hours when I couldn’t get it to work. But, surely enough, just a couple of paragraphs later in the documentation, it turns out you need to add a reference to the Qt project file (*.pro):

QT += phonon

It may also look like this, as you need to make sure you make all the necessary references for every class you use:

QT += core gui phonon

And also, don’t forget those #include references, either.

#include "phonon/audiooutput.h"
#include "phonon/backendcapabilities.h"
#include "phonon/mediaobject.h"

So with all the necessary references in place, you can pass a QString of the filename you want as a Phonon::MediaSource to the Phonon:createPlayer function. And the thing will play most audio formats beside the ubiquitous MP3 format (Ain’t that wizard, Ani?). So this works when you implement the code in a new project.

However, if you plug this same snippet of code into another project (say, one you’ve been working on for a while that you’re trying to incorporate instrinsic media playback as an aspect of the application), the application compiles fine, but there’s no audio to be heard. You can try QSound, but it don’t work either, Holmes. So, if you’re making a game engine and want all the bells and whistles, you’ll realize how wonky Qt can be: it will play fine one way, but won’t in another, using the SAME code!

You could, however, just rip off their mediaplayer demo, but why bother with the extra stuff? That thing compiles fine on my end, but again, when I try to implement their documentation’s simplest code, I got to get to SMDH (shaking my damn head). I wonder if I should invest any more time into Phonon, as it seems Nokia might be working on another media playback class called QtMultimedia. Imagine if you built an entire app on Phonon, only having to scrap it to rewrite it with the new class.

Lesson of the day, kids: if you go with Qt, go with caution. It’s very good when it’s good. But it comes to a deadlock when it’s bad.

Shatner Yells Phonon

Shatner Yells Phonon

UPDATE: Oct. 11, 2011 – My bad. It will work as long as the QString points to a remote, separate file and not an embedded resource. Nokia might fix this in the future. Also, be wary that you’re using forward slashes “/” rather than backslashes “\” in your file path. As for QtMultimedia, it’s just a low-level class rather than a high-level class like Phonon. You see, in programming, low-level code is best utilized by a high-level programmer, whereas high-level code is easier for noobs. Low-level means it’s closer to machine language and you get to play with file pointers, direct memory and stuff (and risk doing lots of harm to your computer if you don’t know what you’re doing). Anyway, QtMultimedia gives access to each frame from video. That’s cool because it means Qt users might be able to implement an app that can draw sprites on top of video. 😉

Tags: , , , , , , , , ,

Comments are closed.