Monday, October 4, 2010

Writing a Simple Media Player in QT

Make sure the phonon library is installed with QT. I had libqt4-phonon-dev, libqt4-phonon, libphonon4, libphonon-dev installed. One of them surely provided the phonon library for me, don't know which.

We will write very basic media player. It just plays file in a window. The source of the player is hard coded. So let's start.

1. We will be working on 3 files. One is the header (SimplePlayer.h), it's definition (SimplePlayer.cpp) and main.cpp to start the application and center the window. So let's star by starting our text editor and create these files.


// SimplePlayer.h

#ifndef SIMPLEPLAYER_H
#define SIMPLEPLAYER_H

#include

class SimplePlayer : public QWidget
{
Q_OBJECT

public:
SimplePlayer(QWidget *parent = 0);
~SimplePlayer();
};

#endif // SIMPLEPLAYER_H


As we can see we don't have any controllers(widgets) to do any other thing then to start the application with default constructor. Next we write it's definition.


// SimplePlayer.cpp

#include "SimplePlayer.h"
#include
#include
#include

SimplePlayer::SimplePlayer(QWidget *parent)
: QWidget(parent)
{
QGridLayout *grid = new QGridLayout(this);
grid->setSpacing(20);

Phonon::VideoPlayer *videoPlayer = new
Phonon::VideoPlayer(Phonon::VideoCategory, this); // This(SimplePlayer) widget is parent

grid->addWidget(videoPlayer, 1, 0, 3, 1);
videoPlayer->play(Phonon::MediaSource("/home/myusername/Desktop/ted/Drk.mp4"));

setLayout(grid);
}

SimplePlayer::~SimplePlayer()
{

}


Above code is very simple. We create a GridLayout and added the VideoPlayer. VideoPlayer has been added recently so make sure you QT is new. It was added in Qt 4.4. For VideoPlayer we have to add Phonon::VideoCategory and at play we have to give it the source. The play method has two overload. Next play method has no parameter i.e play(). For that you have to add the source before calling play by using videoPlayer->load(Phonon::MediaSource("/your/source/file.flv")); Now let's add the main method.



#include
#include
#include "SimplePlayer.h"

/* No VideoPlayer functionality. It's just to center the window */
void center(QWidget &widget)
{
int x, y;
int screenWidth;
int screenHeight;

int WIDTH = 350;
int HEIGHT = 250;

QDesktopWidget *desktop = QApplication::desktop();

screenWidth = desktop->width();
screenHeight = desktop->height();

x = (screenWidth - WIDTH) / 2;
y = (screenHeight - HEIGHT) / 2;

widget.setGeometry(x, y, WIDTH, HEIGHT);
}

int main(int argc, char **argv)
{
QApplication a(argc, argv);
SimplePlayer playerWindow;
playerWindow.setWindowTitle("Simple Player");
playerWindow.show();
center(playerWindow);

return a.exec();
}


We started by centering the window. If you want to use what ever is provided by default center is not required at all. If you don't use center method comment that code inside main method also. In center we take the resolution of desktop and center it. Inside main it's just basic qt code.

Building the code

All we have till now is a directory and three files inside it. To compile this file we have to call qmake and make. Let's do it now.


$ qmake -project
$ qmake
$ make

If we run above three commands last line must generate error. If you have error on other two lines before the last, you must recheck your code. When you are sure the first two lines don't give error come to this stage. Why did the third line generated error? We have to tell Qt that you want to use the phonon library. To do that we just add this line at top after the Automatically generated by qmake of .pro file. If my project directory is MoviePlayer, now after running the first two command I have a file called MoviePlayer.pro. So edit this file and add phonon library to it.

######################################################################
# Automatically generated by qmake (2.01a) Tue Oct 5 08:31:33 2010
######################################################################

QT += phonon # This is the line we added
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

# Input
HEADERS += SimplePlayer.h
SOURCES += main.cpp SimplePlayer.cpp


If you run the make command again, you should not have any error. In fact now you should have an executable now. If you run that program, your movie should be played immediately.

Enhancement


Now we have our first media player. But we want to add other basic functionality like pause, stop, seek, load. We would want to add a button to do these. First see what are available.


Public Slots

void load ( const Phonon::MediaSource & source )
void pause ()
void play ( const Phonon::MediaSource & source )
void play ()
void seek ( qint64 ms )
void setVolume ( float volume )
void stop ()
19 public slots inherited from QWidget
1 public slot inherited from QObject


What we see above is methods of Phonon::VideoPlayer. These are slots so can be added to any of our buttons clicked() signals. Now you can add some buttons and perform above operations.

2 comments: