方案一:CMdaAudioPlayerUtility播放,支持英文。
#ifndef __TTSPLAYER_H__
#define __TTSPLAYER_H__
头文件
// Include Files
#include <MdaAudioSamplePlayer.h>
#include <e32base.h>
class CTtsPlayer : public CBase, public MMdaAudioPlayerCallback
{
public:
// construction
static CTtsPlayer* NewLC();
~CTtsPlayer();
/** Simplified call'n'forget interface.
* Synchronous function
* @leave System-wide error codes. @see MMdaAudioPlayerCallback errors
*/
void PlayTextL( TDesC& aText );
// From the observer interface
public:
virtual void MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds &aDuration);
virtual void MapcPlayComplete(TInt aError);
private:
void ConstructL();
private:
CMdaAudioPlayerUtility* iPlayer;
TBool iPlaybackInProgress;
CActiveSchedulerWait* iWaiter;
// Playback error code
TInt iErr;
};
#endif // __TTSPLAYER_H__
源文件
#include <e32base.h>
#include <e32std.h>
#include <e32def.h>
#include "TtsPlayer.h"
// Prefix telling the audio utility that TTS should be used
_LIT( KTtsPrefix, "(tts)" );
CTtsPlayer* CTtsPlayer::NewLC()
{
CTtsPlayer* self = new (ELeave) CTtsPlayer;
CleanupStack::PushL( self );
self->ConstructL();
return self;
}
CTtsPlayer::~CTtsPlayer()
{
delete iWaiter;
delete iPlayer;
}
void CTtsPlayer::ConstructL()
{
iPlayer = CMdaAudioPlayerUtility::NewL( *this );
iWaiter = new (ELeave) CActiveSchedulerWait;
}
void CTtsPlayer::PlayTextL( TDesC& aText )
{
__ASSERT_ALWAYS( iPlaybackInProgress == EFalse, User::Leave( KErrNotReady ) );
HBufC8* playableText = HBufC8::NewLC( aText.Length() + KTtsPrefix().Length() );
playableText->Des().Append( KTtsPrefix );
playableText->Des().Append( aText );
iPlayer->OpenDesL( *playableText );
iPlaybackInProgress = ETrue;
iWaiter->Start();
// At this point playback is already completed or failed
User::LeaveIfError( iErr );
CleanupStack::PopAndDestroy( playableText );
}
void CTtsPlayer::MapcInitComplete( TInt aError, const TTimeIntervalMicroSeconds& /*aDuration*/ )
{
iErr = aError;
if( aError != KErrNone )
{
iPlaybackInProgress = EFalse;
// Let the paused PlayTextL complete
iWaiter->AsyncStop();
}
else
{
iPlayer->Play();
}
}
void CTtsPlayer::MapcPlayComplete( TInt aError )
{
iErr = aError;
iPlaybackInProgress = EFalse;
// Let the paused PlayTextL complete
iWaiter->AsyncStop();
}
// End of file
调用代码
#include "TtsPlayer.h"
CTtsPlayer* player = CTtsPlayer::NewLC();
_LIT( KTextToSay, "All your voices are belong to us! we are the hero, the true hero is stack." );
TBuf<100> bTextToSay( KTextToSay );
TRAPD( err, player->PlayTextL( bTextToSay ) );
if( err != KErrNone )
{
// TBuf<200> message;
// message.Format( _L( "TTS playback failed with [%d]" ), err );
// iAppView->AddMessageL( message );
}
else
{
// iAppView->AddMessageL( _L( "TTS playback completed successfully" ) );
}
CleanupStack::PopAndDestroy( player );
方案二:调用symbian 插件 tts 库 支持多国语言
#include <nssttsutility.h>
继承 MTtsClientUtilityObserver
实现纯虚函数
#include <nssttsutilityobserver.h>
#include <nssttscommon.h>