Archived:Using Symbian C++ Audio APIs in a Qt app
Qt now has cross-platform APIs for playing audio. This article may be useful for understanding how to use Qt with Symbian C++, not however care should be taken because the code is not Leave safe and does not fully follow Symbian conventions (specifically not all leaves are trapped coming into Qt code).
Article Metadata
Compatibility
Article
Contents |
Description
I did a small research and found that all the S60 API are not yet included in Qt libs. So I thought rather differently and explore to make a Qt project using S60 APIs. Here I have called the Symbian C++ class from Qt classes. The below example shows how to use Symbian C++ class to play Audio File from Qt Projects. Lets create an empty Qt project.
Adding Capabilities and Libraries
Open the .pro file and add the capabilities and libraries
symbian: {
TARGET.UID3 = 0xEA524E4F
TARGET.CAPABILITY = ReadUSerData \
WriteUserData \
NetworkServices \
LocalServices
LIBS += -lcone -leikcore -lavkon -lhwrmvibraclient -lmediaclientaudio -lprofileengine -lcntmodel
}
Audio Player Class in Symbian C++
SimpleAudioPlayer.h
// --------------------------------------------------------------------------
// SimpleAudioPlayer.h
//
// --------------------------------------------------------------------------
#ifndef SIMPLEAUDIOPLAYER_H
#define SIMPLEAUDIOPLAYER_H
// INCLUDES
#include <e32std.h>
#include <e32base.h>
#include <MdaAudioSamplePlayer.h>
// CLASS DECLARATION
class CSimpleAudioPlayer : public CBase, public MMdaAudioPlayerCallback
{
public: // Constructors and destructor
static CSimpleAudioPlayer* NewL();
static CSimpleAudioPlayer* NewLC();
~CSimpleAudioPlayer();
public: // New methods
void PlayL(const TDesC& aFileName);
void Resume();
void Pause();
void Stop();
void Rewind(TInt aIntervalInSeconds);
void FastForward(TInt aIntervalInSeconds);
private:
CSimpleAudioPlayer();
void ConstructL();
private:// From MMdaAudioPlayerCallback
void MapcInitComplete(TInt aError,
const TTimeIntervalMicroSeconds& aDuration);
void MapcPlayComplete(TInt aError);
private: // Public methods
void DisplayErrorMessage(TInt aError);
public: // Member variables
CMdaAudioPlayerUtility* iPlayerUtility;
TTimeIntervalMicroSeconds AudioTrackduration;
};
#endif // SIMPLEAUDIOPLAYER_H
// End of File
SimpleAudioPlayer.cpp
// --------------------------------------------------------------------------
// SimpleAudioPlayer.cpp
//
// Copyright 2007, Symbian Press
// --------------------------------------------------------------------------
// INCLUDE FILES
#include <eikenv.h>
#include "SimpleAudioPlayer.h"
#include "aknnotewrappers.h"
// CONSTANTS
const TInt KOneSecond = 1000 * 1000; // 1 second in microseconds
const TInt KVolumeDenominator = 2;
// METHODS DEFINITION
CSimpleAudioPlayer::CSimpleAudioPlayer()
{
}
CSimpleAudioPlayer::~CSimpleAudioPlayer()
{
delete iPlayerUtility;
}
CSimpleAudioPlayer* CSimpleAudioPlayer::NewLC()
{
CSimpleAudioPlayer* self = new (ELeave) CSimpleAudioPlayer();
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CSimpleAudioPlayer* CSimpleAudioPlayer::NewL()
{
CSimpleAudioPlayer* self = CSimpleAudioPlayer::NewLC();
CleanupStack::Pop(self);
return self;
}
void CSimpleAudioPlayer::ConstructL()
{
iPlayerUtility = CMdaAudioPlayerUtility::NewL(*this);
}
void CSimpleAudioPlayer::PlayL(const TDesC& aFileName)
{
iPlayerUtility->Close();
iPlayerUtility->OpenFileL(aFileName);
}
void CSimpleAudioPlayer::Pause()
{
iPlayerUtility->Pause();
}
void CSimpleAudioPlayer::Resume()
{
iPlayerUtility->Play();
}
void CSimpleAudioPlayer::Stop()
{
iPlayerUtility->Stop();
}
void CSimpleAudioPlayer::Rewind(TInt aIntervalInSeconds)
{
iPlayerUtility->Pause();
// Get the current position of the playback.
TTimeIntervalMicroSeconds position;
iPlayerUtility->GetPosition(position);
// Add the interval to the current position.
position = position.Int64() - aIntervalInSeconds * KOneSecond;
// Set the new position.
iPlayerUtility->SetPosition(position);
iPlayerUtility->Play();
}
void CSimpleAudioPlayer::FastForward(TInt aIntervalInSeconds)
{
iPlayerUtility->Pause();
// Get the current position of the playback.
TTimeIntervalMicroSeconds position;
iPlayerUtility->GetPosition(position);
// Subtract the interval from the current position.
position = position.Int64() + aIntervalInSeconds * KOneSecond;
// Set the new position.
iPlayerUtility->SetPosition(position);
iPlayerUtility->Play();
}
void CSimpleAudioPlayer::MapcInitComplete(TInt aError,
const TTimeIntervalMicroSeconds& aDuration)
{
AudioTrackduration = aDuration;
if (KErrNone == aError)
{
//iPlayerUtility->SetVolume(
// iPlayerUtility->MaxVolume() );// / KVolumeDenominator);
iPlayerUtility->Play();
}
else
{
iPlayerUtility->Close();
// Do something when an error happens.
//DisplayErrorMessage(aError);
}
}
void CSimpleAudioPlayer::MapcPlayComplete(TInt aError)
{
if (KErrNone == aError)
{
}
else
{
// Do something when an error happens.
//DisplayErrorMessage(aError);
}
}
void CSimpleAudioPlayer::DisplayErrorMessage(TInt aError)
{
const TInt KMaxBuffer = 15;
_LIT(KErrorMessage, "Error: %d");
TBuf<KMaxBuffer> buffer;
buffer.AppendFormat(KErrorMessage, aError);
TRAP_IGNORE(CEikonEnv::Static()->InfoWinL(KNullDesC, buffer));
}
// End of File
Qt Classes
Call the Symbian C++ API from Qt class QtUIApp.h
#ifdef Q_OS_SYMBIAN
#include "SimpleAudioPlayer.h"
class CSimpleAudioPlayer;
#endif
...
QPushButton* pushButtonClickMe;
#ifdef Q_OS_SYMBIAN
CSimpleAudioPlayer* iAudioPlayer;
#endif
...
private slots:
void PlayFile();
QtUIApp.cpp
#ifdef Q_OS_SYMBIAN
#include <aknnotewrappers.h>
#include "stringloader.h"
#include <coemain.h>
#endif
...
QtUIApp::QtUIApp(QWidget *parent)
: QMainWindow(parent)
{
...
pushButtonClickMe = new QPushButton("Play",this);
connect(pushButtonClickMe, SIGNAL(pressed()),SLOT(PlayFile()));
#ifdef Q_OS_SYMBIAN
iAudioPlayer = CSimpleAudioPlayer::NewL();
#endif
}
QtUIApp::~QtUIApp()
{
#ifdef Q_OS_SYMBIAN
if(iAudioPlayer)
{
delete iAudioPlayer;
iAudioPlayer =NULL;
}
#endif
}
void QtUIApp::PlayFile()
{
#ifdef Q_OS_SYMBIAN
_LIT(KPath,"C:\\Data\\Others\\1.wav");
iAudioPlayer->PlayL(KPath);
#endif
}
This way we can make great UI using Qt and also can access Symbian C++ Native API.
Conclusion
Please Note: This is a small experiment that I did with my app. I have not tested this with all the S60 APIs. But I assume this should work. Anyway you can go ahead and test with other APIs too and feel free to put your comments.
--somnathbanik 07:18, 6 January 2011 (UTC) By Somnath Banik
Citation
Parts of this code may have seen under copyright from Symbian website therefore the Creative Commons License has been added
Note that this content was originally hosted on the Symbian Foundation developer wiki.


(no comments yet)