Hi,

I am recording the audio with Audio Streaming using CMdaAudioInputStream with KMMFFourCCCodePCM16 and writing the WAV header on the top of written file. The problem is with last three seconds. The Windows MediaPlayer shows plug-in error in last three seconds, the message is "Windows Media Player cannot play the file. The Player might not support the file type or might not support the codec that was used to compress the file." But the same file is able to be playback in other media players. Also, if we encode the file in mp4 it doesn't work. May be I am not writing the correct WAV header. Following is the code.

void CAudioStreamRecorder::Record(TFileName aFileName)
{

iReady = EFalse;
iPlayError = KErrNone;
iFileName.Copy(aFileName);

if(iSoundData)
{
delete iSoundData;
iSoundData = NULL;
}

iSoundData = HBufC8::NewL(KMaxFileSize);

if(iInputStream)
{
iInputStream->Stop();
delete iInputStream;
iInputStream = NULL;
}

iRfSession.Connect();

User::LeaveIfError(iStreamWriter.Create(iRfSession, iFileName, EFileWrite));

iInputStream = CMdaAudioInputStream::NewL(*this);
iInputStream->SetPriority(EPriorityAbsoluteRealTime8, EMdaPriorityPreferenceNone);

iCurrentEncoding = KMMFFourCCCodePCM16;
TRAPD(err,iInputStream->SetDataTypeL(iCurrentEncoding));

if (err != KErrNone)
{
}

iNumOfSamples = 0;

//reset buffer size
iInputStream->Open(&iStreamSettings);




}

void CAudioStreamRecorder::Stop()
{

iReady = EFalse;


if(iInputStream)
{

iInputStream->Stop();
}

WriteWavHeaderL();

iStreamWriter.Close();



iRfSession.Close();

// we can't delete iInputStream yet!
// it will get cleaned up on destruction,
// or when we start recording something new.
//delete iInputStream;
//iInputStream = NULL;

}

void CAudioStreamRecorder::MaiscOpenComplete(TInt aError)
{
iPlayError = aError;
if (aError==KErrNone && iInputStream)
{
iReady = ETrue;

iStreamSettings.iSampleRate = TMdaAudioDataSettings::ESampleRate8000Hz;
iStreamSettings.iChannels = TMdaAudioDataSettings::EChannelsMono;

iInputStream->SetAudioPropertiesL(iStreamSettings.iSampleRate,iStreamSettings.iChannels);
iInputStream->SetGain(iInputStream->MaxGain());

iStreamIdx=0;
iInputStream->ReadL(*iStreamBuffer[iStreamIdx]);
}
else if (aError != KErrNone)
{
iReady = EFalse;
}
}


void CAudioStreamRecorder::MaiscBufferCopied(TInt aError, const TDesC8& aBuffer)
{
iPlayError = aError;
if (aError == KErrNone && iInputStream)
{
iNumOfSamples += aBuffer.Length() / 2; //for 16 bit PCM a sample consists of 2 bytes.

if (&aBuffer==iStreamBuffer[iStreamBuffer.Count()-1])
iStreamIdx=0;
else
{
//check buffer overflow before incrementing
if (iStreamIdx < KStreamBufferCount-1)
iStreamIdx++;
else
{

iStreamIdx = 0; //reset iStreamIdx to zero. This will repleace the previous data

}

}

iInputStream->ReadL(*iStreamBuffer[iStreamIdx]);
if(aBuffer.Length())
{
iStreamWriter.WriteL(aBuffer);

}


}
else if (aError != KErrNone)
{
iReady = EFalse;
}
}


void CAudioStreamRecorder::MaiscRecordComplete(TInt aError)
{
iPlayError = aError;
iReady = EFalse;
}

void CAudioStreamRecorder::WriteWavHeaderL()
{

TBuf8<44> header;
TUint16 channels = 1;//1 or 2
TUint16 bitsPerSample = 16; // 8 or 16
TUint32 sampleRate = 8000;
TUint32 subChunk1Size = 16; //16 for PCM
TUint16 blockAlign = channels * bitsPerSample/8;
TUint32 byteRate = sampleRate * channels * bitsPerSample/8;
TUint32 subChunk2Size = iNumOfSamples * channels * bitsPerSample/8 ; //iStreamWriter.Sink()->SizeL()
TUint32 chunkSize = 36 + subChunk2Size;//4 + ( 8 + subChunk1Size) + ( 8 + subChunk2Size);
TUint16 audioFormat = 1;


RDesWriteStream stream(header);
stream.WriteL(KRiff);
stream.WriteInt32L(chunkSize);
stream.WriteL(KWav);
stream.WriteL(KFmt);
stream.WriteInt32L(subChunk1Size);
stream.WriteInt16L(audioFormat);
stream.WriteInt16L(channels);
stream.WriteInt32L(sampleRate);
stream.WriteInt32L(byteRate);
stream.WriteInt16L(blockAlign);
stream.WriteInt16L(bitsPerSample);
stream.WriteL(KData);
stream.WriteInt32L(subChunk2Size);
stream.CommitL();
stream.Close();

iStreamWriter.Sink()->SeekL(MStreamBuf::EWrite,-(iStreamWriter.Sink()->SizeL()));
iStreamWriter.WriteL(header);

}