×
Namespaces

Variants
Actions

录制音频文件

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

平台安全性
能力: UserEnvironment

文章
关键词: CAudioRecorder, CMdaAudioRecorderUtility
hoolee 在 28 Sep 2009 创建
最后由 hamishwillee 在 21 Nov 2011 编辑

CAudioRecorder类演示了如何使用CMdaAudioRecorderUtility来录制wav文件CAudioRecorder需要由调用函数所完成的MMdaObjectStateChangeObserver回调接口。接口中定义的唯一函数用来更新文件播放时的不同状态。文件的录音可以在该接口函数被调用为EOpen后开始。注意EOpen也指明音频录制的结束。因此你也需要检查前一个状态的标记。

这个示例需要调用类来完成自己的回调接口函数,它用来更新处理的状态。这个状态的更新根据处理的状态更新,播放状态使用timer来完成更新。

头文件:

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


链接库:

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

This page was last modified on 21 November 2011, at 03:30.
83 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×