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: makeThen run your program
Your main.cpp file should look like this:
@#include <QtGui/QApplication>
#include <phonon/AudioOutput>
#include <phonon/MediaObject>
#include <phonon/MediaSource>
#include <phonon/VideoWidget>
#include <phonon/VideoPlayer>using namespace Phonon;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MediaObject mo;
AudioOutput ao;
createPath(&mo,&ao);
mo.setCurrentSource(MediaSource("sample.wav")); // insert your multimedia file here
mo.play();
return a.exec();
}@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:
@
######################################################################Automatically generated by qmake (2.01a) Sat May 5 13:09:36 2012
######################################################################
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .Input
SOURCES += main.cpp
QT += phonon
@