Play Audio File Using Qt Mobility: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
[[Category:HowTo]]<br />[[Category:snippets]]
[[Category:HowTo]]
[[Category:snippets]]


'''English''' [[Play_Audio_File_Using_Qt_Mobility_Bulgarian|Български]]
'''English''' [[Play_Audio_File_Using_Qt_Mobility_Bulgarian|Български]]
Line 13: Line 14:
Modify the project configuration .pro file by including Qt Mobility support:
Modify the project configuration .pro file by including Qt Mobility support:


<code><br />CONFIG ''= mobility<br />MOBILITY''= multimedia<br /></code>
<code>
CONFIG ''= mobility
MOBILITY''= multimedia
</code>


== Source Code ==
== Source Code ==
Line 19: Line 23:
* .h  
* .h  


Include required headers:<br /><code><br />#include <QMediaPlayer><br /></code>
Include required headers:
<code>
#include <QMediaPlayer>
</code>


Declare slot and private members:<br /><code><br />private slots:<br /> void statusChanged(QMediaPlayer::MediaStatus status);
Declare slot and private members:
<code>
private slots:
void statusChanged(QMediaPlayer::MediaStatus status);


private:
private:


QMediaPlayer '''m_pPlayer;<br /></code>
QMediaPlayer '''m_pPlayer;
<br />''' .cpp
</code>


Play a file located on the device:<br /><code><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 /></code>
''' .cpp


Implement the declared slot:<br /><code><br />void MainWindow::statusChanged(QMediaPlayer::MediaStatus status)<br />{<br /> if ( (QMediaPlayer::LoadedMedia == status) &amp;&amp; m_pPlayer)<br /> {<br /> m_pPlayer->play();<br /> }<br />}
Play a file located on the device:
<code>
m_pPlayer = new QMediaPlayer(this);
connect(m_pPlayer, SIGNAL (positionChanged(qint64)), this, SLOT (statusChanged(qint64)));
//Select a file
m_pPlayer->setMedia(QUrl::fromLocalFile("e:SoundsDigitalGirl_Rules.mp3"));
//Set the volume
m_pPlayer->setVolume(50);
m_pPlayer->play();
</code>
 
Implement the declared slot:
<code>
void MainWindow::statusChanged(QMediaPlayer::MediaStatus status)
{
if ( (QMediaPlayer::LoadedMedia == status) &amp;&amp; m_pPlayer)
{
m_pPlayer->play();
}
}

Revision as of 11:30, 25 February 2015


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:

CONFIG ''= mobility
MOBILITY''= multimedia

Source Code

  • .h

Include required headers:

#include <QMediaPlayer>

Declare slot and private members:

private slots:
 void statusChanged(QMediaPlayer::MediaStatus status);

private:

QMediaPlayer '''m_pPlayer;

.cpp

Play a file located on the device:

m_pPlayer = new QMediaPlayer(this);
connect(m_pPlayer, SIGNAL (positionChanged(qint64)), this, SLOT (statusChanged(qint64)));
//Select a file
m_pPlayer->setMedia(QUrl::fromLocalFile("e:SoundsDigitalGirl_Rules.mp3"));
//Set the volume
m_pPlayer->setVolume(50);
m_pPlayer->play();

Implement the declared slot: void MainWindow::statusChanged(QMediaPlayer::MediaStatus status) {

if ( (QMediaPlayer::LoadedMedia == status) && m_pPlayer)
{
m_pPlayer->play();
}

}