May 5, 2012

entricular entricular
Lab Rat
9 posts

How do you use Phonon to play a multimedia file in a Qt application on Ubuntu Linux ?

 

I have been trying to do some research on Phonon for a while, because I use Ubuntu Linux and I wanted to learn how to include a .wav file or .mp3 file while showing a splashpage in my Qt Application. So far this is the closest I have come to finding out how to use Phonon in an application.

For some strange reason I believe QSound does not work in Ubuntu Linux but Phonon seems to be the best choice when it comes to running multimedia files with Qt on Ubuntu Linux. This is the shared code but you must remember to include Qt phonon in your .pro file before you compile this program or else you will get many error messages.

I tested this on Ubuntu Linux 10.04 LTS using Qt SDK 4.7.4 and it compiled and played with no problems. I think the QSound issues with Linux have something technical to do with DBus interface. Anyway make sure you have a sample.wav or sample.mp3 file in the same directory.

In order to run the following code:

Create a directory
Change into your newly created directory
Use a text editor to create the main.cpp file within your directory
Copy/Type/Paste the following code below
Run qmake -project QT+=phonon
this will generate a QT plus phonon enabled .pro file
Then run qmake and finally make and this should produce the program that will play your sound file.

To Compile:
Step 1: qmake -project QT+=phonon
Step 2: qmake
Step 3: make

Then run your program

Your main.cpp file should look like this:

  1. #include <QtGui/QApplication>
  2.  
  3. #include <phonon/AudioOutput>
  4. #include <phonon/MediaObject>
  5. #include <phonon/MediaSource>
  6. #include <phonon/VideoWidget>
  7. #include <phonon/VideoPlayer>
  8.  
  9. using namespace Phonon;
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13.     QApplication a(argc, argv);
  14.     MediaObject mo;
  15.     AudioOutput ao;
  16.     createPath(&mo,&ao);
  17.     mo.setCurrentSource(MediaSource("sample.wav")); // insert your multimedia file here
  18.     mo.play();
  19.     return a.exec();
  20. }

Make sure you add this: ‘QT += phonon’ to your qmake -project generated pro file

Your .pro should look like this before you run qmake and make to compile the application:

  1. ######################################################################
  2. # Automatically generated by qmake (2.01a) Sat May 5 13:09:36 2012
  3. ######################################################################
  4.  
  5. TEMPLATE = app
  6. TARGET =
  7. DEPENDPATH += .
  8. INCLUDEPATH += .
  9.  
  10. # Input
  11. SOURCES += main.cpp
  12.  
  13. QT += phonon

0 replies

 
  ‹‹ SQLite and Utf-8 encoding problem      Log ››

You must log in to post a reply. Not a member yet? Register here!