You Are Here:

Community: Wiki

This page was last modified on 20 September 2009, at 21:12.

Record audio files

From Forum Nokia Wiki

Reviewer Approved   

CAudioRecorder illustrates how to use CMdaAudioRecorderUtility for recording wav files.

CAudioRecorder requires MMdaObjectStateChangeObserver callback interface to be implemented by the calling class. The only function defined in this interface is used to update different states of the file playing. The recording to the file can start only after this interface function is called with EOpen. Note that EOpen is also given when the audio recording finishes, thus you should also check the previous state variable before calling any functions.

This example also requires calling class to implement own callback interface function that is used for updating the playing status. This status is updated according to the players status as well the playing process is updated using timer implementation.


Headers:

#include <MdaAudioSampleEditor.h>
#include <Mda\Client\Utility.h>
#include <Mda\Common\Resource.h>


Link against:

LIBRARY	mediaclientaudio.lib

Capabilities:

CAPABILITY UserEnvironment

AudioRecorder.cpp

#include <e32base.h>
#include <aknviewappui.h>
 
#include <Mda\Common\Resource.h>
#include <BAUTILS.H>
 
#include "AudioRecorder.h"
 
const TInt KReFreshTimeOut = 1000000; // re-fresh every second
 
CAudioRecorder* CAudioRecorder::NewL(MExmapleRecStateObserver& aObserver)
{
CAudioRecorder* self = CAudioRecorder::NewLC(aObserver);
CleanupStack::Pop(self);
return self;
}
 
CAudioRecorder* CAudioRecorder::NewLC(MExmapleRecStateObserver& aObserver)
{
CAudioRecorder* self = new (ELeave) CAudioRecorder(aObserver);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
 
CAudioRecorder::CAudioRecorder(MExmapleRecStateObserver& aObserver)
:iObserver(aObserver),iVolume(5)
{
}
 
CAudioRecorder::~CAudioRecorder()
{
delete iExampleTimer;
if(iToneUtility)
{
iToneUtility->Stop();
iToneUtility->Close();
}
 
delete iToneUtility;
delete iFormat;
delete iCodec;
delete iSettings;
}
 
 
void CAudioRecorder::ConstructL()
{
iExampleTimer = CExampleTimer::NewL(CActive::EPriorityStandard,*this);
ReportStateAndTime();
}
 
void CAudioRecorder::TimerExpired(TAny* /*aTimer*/,TInt aError)
{
ReportStateAndTime();
if(iExampleTimer && aError != KErrCancel)
iExampleTimer->After(KReFreshTimeOut);
}
 
void CAudioRecorder::RecordL(const TDesC& aFileName)
{
iCurrentFile.iName.Copy(aFileName);
 
if(iExampleTimer)
iExampleTimer->Cancel();
 
if(iToneUtility)
{
iToneUtility->Stop(); // stop any play/rec
iToneUtility->Close();// close previously opened file.
}
 
delete iToneUtility, iToneUtility = NULL;
iToneUtility = CMdaAudioRecorderUtility::NewL(*this);
 
// and free ther reserved resources.
delete iFormat, iFormat = NULL;
delete iCodec, iCodec = NULL;
delete iSettings, iSettings = NULL;
 
// if the file exists, we append sound data to it.
if(BaflUtils::FileExists(CCoeEnv::Static()->FsSession(),aFileName))
iToneUtility->OpenFileL(iCurrentFile.iName);
else
{
BaflUtils::EnsurePathExistsL(CCoeEnv::Static()->FsSession(),iCurrentFile.iName);
 
// record new Wav sound file.
iFormat = new (ELeave) TMdaWavClipFormat;
iCodec = new (ELeave) TMdaWavCodec();
 
iSettings = new (ELeave) TMdaAudioDataSettings;
iSettings->iSampleRate = 8000;
iSettings->iChannels = 1;// mono
iToneUtility->OpenL(&iCurrentFile,&iMdaWavClipFormat,NULL,NULL);
}
 
if(iExampleTimer)
iExampleTimer->After(KReFreshTimeOut);
}
 
void CAudioRecorder::StopL(void)
{
if(iExampleTimer)
iExampleTimer->Cancel();
 
if(iToneUtility)
iToneUtility->Stop();
 
ReportStateAndTime();
}
 
void CAudioRecorder::SetVolume(TInt& aVolume)
{
if(aVolume < 1)
aVolume = 1;
else if(aVolume > 10)
aVolume = 10;
 
iVolume = aVolume;// save to internal value always
if(iToneUtility) // and if utility exists, set it to it as well.
{
TInt Vol = ((iToneUtility->MaxVolume() * iVolume) / 10);
iToneUtility->SetVolume(Vol);
}
}
 
void CAudioRecorder::ReportStateAndTime(void)
{
TInt CurrPosition(0),FileDuration(0);
CMdaAudioClipUtility::TState CurrState(CMdaAudioClipUtility::ENotReady);
 
if(iToneUtility)
{
CurrState = iToneUtility->State();
 
TInt64 HelpPos = iToneUtility->Position().Int64();
CurrPosition = HelpPos / 1000000;
 
HelpPos = iToneUtility->RecordTimeAvailable().Int64();
FileDuration = HelpPos / 1000000;
}
iObserver.StateUpdate(CurrState,CurrPosition,FileDuration);
}
 
 
void CAudioRecorder::MoscoStateChangeEvent(CBase* aObject, TInt aPreviousState, TInt aCurrentState, TInt /*aErrorCode*/)
{
if(aObject == iToneUtility)
{
ReportStateAndTime();
switch(aCurrentState)
{
case CMdaAudioClipUtility::EOpen:
{
if(aPreviousState == CMdaAudioClipUtility::ENotReady)
{
TInt Vol = ((iVolume * iToneUtility->MaxVolume()) / 10);
iToneUtility->SetVolume(Vol);
 
TRAPD(err, iToneUtility->SetGain(iToneUtility->MaxGain());
iToneUtility->RecordL(););
}
} break;
 
case CMdaAudioClipUtility::EPlaying:
case CMdaAudioClipUtility::ERecording:
case CMdaAudioClipUtility::ENotReady:
default: // no need to do anything on these states.
break;
}
}
}

AudioRecorder.h

#ifndef AUDIORECORDER_H__
#define AUDIORECORDER_H__
 
#include <MdaAudioSampleEditor.h>
#include <Mda\Client\Utility.h>
#include "CExampleTimer.h"
 
//This code has been corrected by Michel David. At least, it now compiles!!
class MExmapleRecStateObserver
{
public:
virtual void StateUpdate(CMdaAudioClipUtility::TState aState, TInt aPosition, TInt aDuration)=0;
};
 
 
class CAudioRecorder : public CBase, public MMdaObjectStateChangeObserver, public MExampleTimerNotify
{
public:
static CAudioRecorder* NewL(MExmapleRecStateObserver& aObserver);
static CAudioRecorder* NewLC(MExmapleRecStateObserver& aObserver);
~CAudioRecorder();
 
public: // public functions
void RecordL(const TDesC& aFileName);
void StopL(void);
void SetVolume(TInt& aVolume);
 
private:// internal functions
void ReportStateAndTime(void);
void ConstructL();
CAudioRecorder(MExmapleRecStateObserver& aObserver);
 
protected: // from MMdaObjectStateChangeObserver & MExampleTimerNotify
void MoscoStateChangeEvent(CBase* aObject, TInt aPreviousState, TInt aCurrentState, TInt aErrorCode);
void TimerExpired(TAny* aTimer,TInt aError);
 
private:
MExmapleRecStateObserver& iObserver;
CMdaAudioRecorderUtility* iToneUtility;
TInt iVolume;
TMdaWavClipFormat iMdaWavClipFormat;
TMdaFileClipLocation iCurrentFile;
CExampleTimer* iExampleTimer;
TMdaClipFormat* iFormat;
TMdaPackage* iCodec;
TMdaAudioDataSettings* iSettings;
};
 
 
 
 
#endif /*AUDIORECORDER_H__*/

Links

Audio Recording APIs

Recording audio with stream

MMF

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fE25E455E25BE44E2595E25E455E2588E25B6E25E459E259FE25B3E25E459E25A2E2591E25E456E2596E2587E25E454E25BBE25B6X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZaudioQ qfnZtopicQUqfnTopicZmultimediaQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ
User Rating: qfnZuserE5FratingQNx5E2E0000X