TTS多语言(二)

6:获取本地区域选项ID

 

LANGID lid = GetUserDefaultLangID();

返回值是一个16进制表示的区域值, 比如: 0x409是English, 0x804是中文。

 

7:根据区域选项选择TTS的Voice (ps: 机器上装了支持该区域的TTS语言包)

 

int _tmain(int argc, _TCHAR* argv[]) { ISpVoice * pVoice = NULL; HRESULT hr = S_OK; CComPtr cpTokenKey; CComPtr cpVoiceToken; CComPtr cpEnum; ULONG ulCount = 0; if (FAILED(::CoInitialize(NULL))) return FALSE; hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice); if( SUCCEEDED( hr ) ) { //Enumerate the available voices if(SUCCEEDED(hr)) hr = SpEnumTokens(SPCAT_VOICES, NULL, NULL, &cpEnum); //Get the number of voices if(SUCCEEDED(hr)) hr = cpEnum->GetCount(&ulCount); BOOL bFind = FALSE; LANGID lid = GetUserDefaultLangID(); // Obtain a list of available voice tokens, set the voice to the token, and call Speak while (SUCCEEDED(hr) && ulCount -- ) { cpVoiceToken.Release(); if(SUCCEEDED(hr)) hr = cpEnum->Next( 1, &cpVoiceToken, NULL ); WCHAR *strTemp = NULL; int iIndex = 0; cpTokenKey.Release(); HRESULT hr2 = cpVoiceToken->OpenKey(_T("Attributes"), &cpTokenKey); if (SUCCEEDED(hr2)) { cpTokenKey->GetStringValue(_T("Language"), &strTemp); LANGID lTempID = (LANGID)wcstol(strTemp, NULL, 16); if (lTempID == lid) { pVoice->SetVoice(cpVoiceToken); break; } } } hr = pVoice->Speak(L"Hi, 我叫李建功,希望和大家交个朋友!呵呵", 0, NULL); pVoice->Release(); pVoice = NULL; } ::CoUninitialize(); return 0; }

你可能感兴趣的:(学习)