方案一: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 );
mmp中添加
LIBRARY mediaclientaudio.lib
方案二:调用symbian 插件 tts 库 支持多国语言
#include <nssttsutility.h>
#include <nssttsutilityobserver.h>
#include <nssttscommon.h>
继承 MTtsClientUtilityObserver
实现纯虚函数
private:
void MapcCustomCommandEvent( TInt aEvent, TInt aError );
void MapcInitComplete( TInt aError, const TTimeIntervalMicroSeconds& aDuration );
void MapcPlayComplete( TInt aError );
调用代码
TTtsStyle iStyle;
TTtsStyleID iStyleId;
// HBufC* aDes = GetSkinMgr()->GetDescriptionLC(R_MAIN_NDATE);
// CTtsParsedText* iParsedText = CTtsParsedText::NewL(*aDes);
// CleanupStack::PopAndDestroy();
CTtsUtility* iTtsUtility = CTtsUtility::NewL( *this );
// if ( iTtsUtility->NumberOfStyles() > 0 )
// {
// // Delete style if exists
// iTtsUtility->DeleteStyle( iStyleId );
// }
// if ( iParsedText->NumberOfSegments() > 0 )
// {
// // Remove existing segment from parsed text structure
// iParsedText->DeleteSegmentL( 0 );
// }
// // Use high quality TTS
iStyle.iQuality = ETtsQualityHighOnly;
// Use language and speaker set in Speech application.
// Also speaking rate and volume is set according to Speech app.
iStyle.iLanguage = ELangPrcChinese;
// Add style
iStyleId = iTtsUtility->AddStyleL( iStyle );
HBufC* aDes = GetSkinMgr()->GetDescriptionLC(R_MENU_DESCRIBLE_TIPS_4);
// Initialise with current text and settings.
// iTtsUtility->OpenDesL(_L("hello this is test"));
iTtsUtility->OpenDesL(*aDes);
CleanupStack::PopAndDestroy();
// iTtsUtility->OpenParsedTextL( *iParsedText );
//The command to start playing
iTtsUtility->Play();
// TInt volume;
// iTtsUtility->GetVolume( volume );
// iTtsUtility->SetVolume( volume );
// iTtsUtility->Stop();
mmp中添加
LIBRARY nssttsutility.lib nssttscommon.lib
方案三:科大讯飞语音云,注册appid,下载SDK后根据demo制作。