Tutorial: Simple Media Player
Files:
The Simple Media Player application tutorial shows how to use the Qt Extended Media API to create a simple Media player application. It also demonstrates using a QDocumentSelectorDialog to get audio/video selections from the document system.
The Simple Player application displays a list of audio/video files. When a file is selected the the Media API is called and the media is played.
Firstly a container for all player objects is created.
context = new QMediaContentContext(this);
Then a watch is added for media control.
controlNotifier = new QMediaControlNotifier(QMediaControl::name(), this);
connect(controlNotifier, SIGNAL(valid()), this, SLOT(mediaControlValid()));
context->addObject(controlNotifier);
Then a watch is added for the video as well.
videoNotifier = new QMediaControlNotifier(QMediaVideoControl::name(), this);
connect(videoNotifier, SIGNAL(valid()), this, SLOT(mediaVideoControlValid()));
context->addObject(videoNotifier);
Once you have a file to play.
QContent content(filename);
m_mediaContent = new QMediaContent(content);
context->setMediaContent(m_mediaContent);
The Media API will emit the valid signal that you have attached to mediaControlValid()
m_control = new QMediaControl(m_mediaContent);
m_control->start();
If it has video it will then emit the valid signal you have attached to mediaVideoControlValid()
m_video = new QMediaVideoControl(m_mediaContent);
videoWidget = m_video->createVideoWidget(this);
videoWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
layout->addWidget(videoWidget);
BasicMedia Class Definition
class BasicMedia : public QWidget
{
Q_OBJECT
public:
BasicMedia(QWidget* parent);
~BasicMedia();
void stop();
void start();
void setFilename(QString filename);
void volumeup();
void volumedown();
protected:
void keyReleaseEvent( QKeyEvent * );
private:
QMediaContentContext* context;
QMediaContent* m_mediaContent;
QMediaControl* m_control;
QMediaVideoControl* m_video;
int m_state;
int volume;
private slots:
void mediaVideoControlValid();
void mediaControlValid();
private:
QWidget* videoWidget;
QString vfile;
QVBoxLayout* layout;
};
The BasicMedia class is used by the simpleplayerbase.ui and is called PlayScreen.
SimplePlayer Class Definition
The SimplePlayer class sets up the ui,buttons and uses QDocumentSelectorDialog to get access to the Document System. For more see the following classes called from basicmedia.cpp QMediaControlNotifier QMediaContentContext QMediaVideoControl QMediaContent QMediaControl
Building the Simple Media Player application
To install and run the demonstration, carry out the following steps.
- Create a new directory (e.g. $HOME/src/simpleplayer) and copy all the example files to that directory.
mkdir $HOME/src/simpleplayer
cd $HOME/src/simpleplayer
cp -r <qt-extended-source-directory>/examples/simpleplayer/* .
chmod +w *
- Build the new application.
export QPEDIR=<qt-extended-build-directory>
$QPEDIR/bin/qbuild
$QPEDIR/bin/qbuild image
- Run Qt Extended.
$QPEDIR/bin/runqtopia
- Go into the list of Applications and scroll down until you find the Simple Player application.