Discussion Board
Timer delay between playbacks???
2005-06-30, 12:03
#1
Registered User
Hi All!
I am working on a translation application. I have successfully implemented
the AUDIO REPETITION feature in that. Now I have to insert a time delay(1-5
seconds) between each repetition of the playback. I tried to implement that
by using the RTIMER and it is working fine.
In the current implementation when the audio is playing in a repetitive mode
and DELAY TIMER is running , when I try to Cancel the timer through a Stop
command it does not CANCEL the TIMER, it first completes the TIMER and then
CANCELS the TIMER.
I want the TIMER DELAY to cancel when I click on the stop button.
What is best way to implement the TIMER DELAY in an application?? Whether I
should use RTIMER or CTIMER or something else???
IS there any other way to do this???
And one more thing that I would like to ask, I am making an object of RTIMER
in AppUi Class and it remains persistent throughout the application.
Thanks & Regards,
Lalit
Re: Timer delay between playbacks???
2005-09-09, 10:39
#2
Regular Contributor
Hi Lalit,
It would really very kind if your share your source code for audio repetition and timer stuff. I am having the same problem for making audio repetition.
Regards,
shagor
Re: Timer delay between playbacks???
2005-09-09, 11:19
#3
Registered User
Sure, the timer completes if you stop it, but you will get a KErrCancel instead of KErrNone when you cancel it, so just check the completion status of the TRequestStatus object you pass to your RTimer object.
Re: Timer delay between playbacks???
2005-09-09, 11:45
#4
Regular Contributor
Hi Lalit,
I implemented timer for repetition of CMdaOutputStream. However my application crashes with App. closed. I am giving my source code below:
N.B. Can you also suggest me where to call the timeout after method for repeating.
Regards,
shagor
#include "Streamplayengine.h"
#include <amrcodec.h>
#include <aknnotewrappers.h>
const TInt KPcmBufferSize = 320;
const TInt KAmrBufferSize = 32;
CStreamPlayEngine::CStreamPlayEngine(MRectPlayCallBack* aCallBack)
:iCallBack(aCallBack),iVolume(10),iReadSize(0)
,iPlayError(KErrNone),iStatus(EEngineReady),iPlayBuffer(NULL)
{
}
CStreamPlayEngine::~CStreamPlayEngine()
{
Stop();
if(iStream)
{
delete iStream;
iStream = NULL;
}
if(iOCodec)
{
delete iOCodec;
iOCodec = NULL;
}
if(iPcm16Buffer)
{
delete iPcm16Buffer;
iPcm16Buffer = NULL;
}
if(iAmrBuffer)
{
delete iAmrBuffer;
iAmrBuffer = NULL;
}
if(iTmpBuffer)
{
delete iTmpBuffer;
iTmpBuffer = NULL;
}
if(iPlayBuffer)
{
delete iPlayBuffer;
iPlayBuffer = NULL;
}
iBuffer.ResetAndDestroy();
if(iRepeatTimer)
{
iRepeatTimer->Cancel();
delete iRepeatTimer;
iRepeatTimer = NULL;
}
}
CStreamPlayEngine* CStreamPlayEngine::NewL(MRectPlayCallBack* aCallBack)
{
CStreamPlayEngine* self = new (ELeave) CStreamPlayEngine(aCallBack);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(); // self
return self;
}
const TInt KAdvancedUidCodecAMRToPCM16 = 0x101FAF67;
void CStreamPlayEngine::ConstructL()
{
iRepeat = false;
iRepeatTimer = CTimeOutTimer::NewL(EPriorityHigh, *this);
}
void CStreamPlayEngine::SetengineState(TInt aNewState)
{
if(iPlayBuffer && iCallBack)
{
TPoint AudioLength(iReadSize,iPlayBuffer->Des().Length());
TPoint PlayState(iStatus,aNewState);
iStatus = aNewState;
// iCallBack->StateChange(PlayState,iPlayError,AudioLength,this,iPlayBuffer);
}
}
void CStreamPlayEngine::ReStartL()
{
Stop();
delete iStream;
iStream = NULL;
delete iTmpBuffer;
iTmpBuffer = NULL;
iTmpBuffer = HBufC8::NewL(KPcmBufferSize);
delete iOCodec;
iOCodec = NULL;
iOCodec = CMMFCodec::NewL(TUid::Uid(KAdvancedUidCodecAMRToPCM16));
delete iPcm16Buffer;
iPcm16Buffer = NULL;
iPcm16Buffer = CMMFDescriptorBuffer::NewL(KPcmBufferSize);
delete iAmrBuffer;
iAmrBuffer = NULL;
iAmrBuffer = CMMFDescriptorBuffer::NewL(KAmrBufferSize);
iPlayError = KErrNone;
SetengineState(EEngineReady);
LoadFileL();
iStream = CMdaAudioOutputStream::NewL(*this);
iStream->Open(&iSettings);
}
void CStreamPlayEngine::RepeatSong()
{
Stop();
//CAknErrorNote* note = new (ELeave) CAknErrorNote;
//note->ExecuteLD(_L("Repeating song"));
User::After(4000000);
//ReStartL();
//delete iStream;
//iStream = NULL;
//iStream = CMdaAudioOutputStream::NewL(*this);
LoadFileL();
iStream->Open(&iSettings);
}
void CStreamPlayEngine::TimedOut()
{
CAknErrorNote* note = new (ELeave) CAknErrorNote;
note->ExecuteLD(_L("Repeating song"));
Stop();
LoadFileL();
iStream->Open(&iSettings);
}
/*
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
*/
void CStreamPlayEngine::MaoscOpenComplete(TInt aError)
{
iPlayError = aError;
if (aError==KErrNone && iStream)
{
iSettings.iSampleRate = TMdaAudioDataSettings::ESampleRate8000Hz;
iSettings.iChannels = TMdaAudioDataSettings::EChannelsMono;
// iSettings.iVolume = ((iStream->MaxVolume() * iVolume) / 10);
iStream->SetAudioPropertiesL(iSettings.iSampleRate,iSettings.iChannels);
SetVolume(iVolume);
iStream->SetPriority(EPriorityNormal, EMdaPriorityPreferenceNone);
SetengineState(EEnginePlaying);
iStream->WriteL(*iBuffer[0]);//iPlayBuffer->Des());//
}
else
{
SetengineState(iStatus);// keep the state
}
}
/*
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
*/
void CStreamPlayEngine::MaoscBufferCopied(TInt aError, const TDesC8& aBuffer)
{
iPlayError = aError;
if(aError == KErrNone && iStream)
{
if (&aBuffer == iBuffer[0])
{
iCurrIndex = 0;
SetengineState(EEnginePlaying);
if((*iBuffer[1]).Length())
{
iStream->WriteL(*iBuffer[1]);
ReadFromFromL(0);
}
else
{
// It goes to MaoscPlayComplete with KErrCancel otherwise MaoscPlayComplete is never called.
iRepeat = true;
if(iStream)
iStream->Stop();
}
}
else// if (&aBuffer == iBuffer[1])
{
iCurrIndex = 1;
SetengineState(EEnginePlaying);
if((*iBuffer[0]).Length())
{
iStream->WriteL(*iBuffer[0]);
ReadFromFromL(1);
}
else
{
SetengineState(EEngineReady);
}
}
}
else
{
SetengineState(EEngineReady);
}
}
/*
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
*/
void CStreamPlayEngine::MaoscPlayComplete(TInt aError)
{
iPlayError = aError;
if (aError == KErrUnderflow)
{
if(iPlayBuffer->Des().Length() > iReadSize)
{
iCurrIndex = 0;
for (TInt index = 0; index < iBuffer.Count(); index++)
{
ReadFromFromL(index);
}
iStream->WriteL(*iBuffer[0]);
}
else
{
SetengineState(EEngineReady);
}
}
else if(aError == KErrCancel)
{
if(iRepeat == true)
{
//After one second TimeOut method is called.
iRepeatTimer->After(1000000);
SetengineState(EEngineRepeating);
}
else
SetengineState(EEngineReady);
}
else
{
SetengineState(EEngineReady);
}
}
void CStreamPlayEngine::PlayL(const TDesC8& aPlayBuffer)
{
iReadSize = 0;
delete iPlayBuffer;
iPlayBuffer = NULL;
iPlayBuffer = HBufC8::NewL(aPlayBuffer.Length());
iPlayBuffer->Des().Copy(aPlayBuffer);
iCurrentPlayBuffer = HBufC8::NewL(aPlayBuffer.Length());
iCurrentPlayBuffer->Des().Copy(aPlayBuffer);
ReStartL();
}
void CStreamPlayEngine::Stop()
{
if(iStatus == EEnginePlaying)
{
iPlayError = KErrNone;
SetengineState(EEngineReady);
iRepeat = false;
if(iStream)
iStream->Stop();
if(iRepeatTimer)
iRepeatTimer->Cancel();
}
else if(iStatus == EEngineRepeating)
{
SetengineState(EEngineRepeating);
if(iStream)
iStream->Stop();
}
}
void CStreamPlayEngine::SetVolume(TInt aVol)
{
iVolume = aVol;
if(iVolume < 0)
iVolume = 0;
else if(iVolume > 10)
iVolume = 10;
if(iStream)
{
iStream->SetVolume(((iStream->MaxVolume() * iVolume) / 10));
}
}
void CStreamPlayEngine::ReadFromFromL(TInt aBufferIndex)
{
if(aBufferIndex ==0 || aBufferIndex == 1 && iPlayBuffer)
{
(*iBuffer[aBufferIndex]).FillZ();
(*iBuffer[aBufferIndex]).Zero();
if((iPlayBuffer->Des().Length() > iReadSize) && iTmpBuffer)
{
TInt ReadFor(KAmrBufferSize);
if(iPlayBuffer->Des().Length() < (iReadSize + ReadFor))
{
ReadFor = iPlayBuffer->Des().Length() - iReadSize;
}
iTmpBuffer->Des().Copy(iPlayBuffer->Des().Mid(iReadSize,ReadFor));
iReadSize = iReadSize + ReadFor;
ConvertAmr2PcmL(iTmpBuffer->Des(),(*iBuffer[aBufferIndex]));
}
}
}
void CStreamPlayEngine::LoadFileL()
{
iBuffer.ResetAndDestroy();
TDes8* buffer = new(ELeave) TBuf8<KPcmBufferSize>;
buffer->SetMax();
CleanupStack::PushL(buffer);
User::LeaveIfError(iBuffer.Append(buffer));
CleanupStack::Pop(buffer);
buffer = new(ELeave) TBuf8<KPcmBufferSize>;
buffer->SetMax();
CleanupStack::PushL(buffer);
User::LeaveIfError(iBuffer.Append(buffer));
CleanupStack::Pop(buffer);
iReadSize = 0;
iCurrIndex = 0;
for (TInt index = 0; index < iBuffer.Count(); index++)
{
ReadFromFromL(index);
}
}
void CStreamPlayEngine::ConvertAmr2PcmL(const TDesC8& aAmrData,TDes8& aDestBuffer)
{
iAmrBuffer->Data().Copy(aAmrData);
TBool OkMai(EFalse);
TCodecProcessResult result = iOCodec->ProcessL(*iAmrBuffer,*iPcm16Buffer);
if((result.iStatus == TCodecProcessResult::EProcessComplete))
{
OkMai = ETrue;
aDestBuffer.Copy(iPcm16Buffer->Data());
}
}
//Implementing timer class
CTimeOutTimer::CTimeOutTimer(const TInt aPriority, MTimeoutNotify& aTimeOutNotify)
: CTimer(aPriority), iNotify(aTimeOutNotify)
{
}
CTimeOutTimer::~CTimeOutTimer()
{
Cancel();
}
CTimeOutTimer* CTimeOutTimer::NewL(const TInt aPriority, MTimeoutNotify& aTimeOutNotify)
{
CTimeOutTimer* self = CTimeOutTimer::NewLC(aPriority, aTimeOutNotify);
CleanupStack::Pop(); // self
return self;
}
CTimeOutTimer* CTimeOutTimer::NewLC(const TInt aPriority, MTimeoutNotify& aTimeOutNotify)
{
CTimeOutTimer *self = new (ELeave) CTimeOutTimer(aPriority, aTimeOutNotify);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
// Construct the actual timer
void CTimeOutTimer::ConstructL()
{
// Call the base class ConstructL
CTimer::ConstructL();
// Add this timer to the active scheduler
CActiveScheduler::Add(this);
}
void CTimeOutTimer::RunL()
{
// When the timer times out notify the object registered with this timer
iNotify.TimedOut();
}
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules