Play Audio File Using Qt Mobility
Jump to navigation
Jump to search
English Български
Play Audio File Using Qt Mobility
Overview
This article shows how to play audio file using "QMediaPlayer":http://doc.qt.nokia.com/qtmobility/qmediaplayer.html from Qt Mobility 1.1.
Project Configuration
Modify the project configuration .pro file by including Qt Mobility support:
<br />CONFIG ''= mobility<br />MOBILITY''= multimedia<br />
Source Code
- .h
Include required headers:
<br />#include <QMediaPlayer><br />
Declare slot and private members:
<br />private slots:<br /> void statusChanged(QMediaPlayer::MediaStatus status);
private:
QMediaPlayer '''m_pPlayer;<br />
.cpp
Play a file located on the device:
<br />m_pPlayer = new QMediaPlayer(this);<br />connect(m_pPlayer, SIGNAL (positionChanged(qint64)), this, SLOT (statusChanged(qint64)));<br />//Select a file<br />m_pPlayer->setMedia(QUrl::fromLocalFile("e:SoundsDigitalGirl_Rules.mp3"));<br />//Set the volume<br />m_pPlayer->setVolume(50);<br />m_pPlayer->play();<br />
Implement the declared slot:
void MainWindow::statusChanged(QMediaPlayer::MediaStatus status)
{
if ( (QMediaPlayer::LoadedMedia == status) && m_pPlayer)
{
m_pPlayer->play();
}
}