C++桌面应用中文语音包功能接口-创新互联
使用之前需要先安装中文语音包(男声+女声安装包下载)
Voice.h文件
(头文件包含的lib库资源如果缺失,百度下载或我的资源)
#pragma once
#ifdef _MSC_VER
#pragma warning (push)
#pragma warning (disable : 4005)
#include#pragma warning (pop)
#pragma warning (default : 4005)
#endif
#include#include#include#pragma comment(lib,"ole32.lib")
#pragma comment(lib,"sapi.lib")
class CVoice
{public:
CVoice(void);
~CVoice(void);
private:
ISpVoice * pVoice ;
bool m_State;
void InitVoice();
void CloseVoice();
public:
void Speech(CString str,bool flag =true);
void SetRate(int rate);
void SetVoiceType(int voicetype); //设置1:男声;2:女声
};
Voice.cpp文件
#include "StdAfx.h"
#include "Voice.h"
CVoice::CVoice(void)
{m_State =false;
InitVoice();
}
CVoice::~CVoice(void)
{CloseVoice();
}
void CVoice::InitVoice()
{pVoice =NULL;
if (FAILED(::CoInitialize(NULL)))
return ;
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if( SUCCEEDED( hr ) )
{ISpObjectToken* pChineseToken;
WCHAR pszTokenId[] =L"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Speech\\Voices\\Tokens\\VW Liang";
SpGetTokenFromId(pszTokenId , &pChineseToken);
pVoice->SetVoice(pChineseToken);
pVoice->SetVolume(100);
pVoice->SetRate(1);
}
m_State =true;
}
void CVoice::CloseVoice()
{if(pVoice !=NULL)
{pVoice->Release();
pVoice = NULL;
}
if(m_State ==true)
::CoUninitialize();
}
void CVoice::SetRate(int rate)
{pVoice->SetRate(rate);
}
void CVoice::SetVoiceType(int voicetype)
{if (voicetype == 1)
{ISpObjectToken* pChineseToken;
// WCHAR pszTokenId[] =L"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Speech\\Voices\\Tokens\\VW Lily";
WCHAR pszTokenId[] =L"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Speech\\Voices\\Tokens\\VW Liang";
SpGetTokenFromId(pszTokenId , &pChineseToken);
pVoice->SetVoice(pChineseToken);
}
else
{ISpObjectToken* pChineseToken;
WCHAR pszTokenId[] =L"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Speech\\Voices\\Tokens\\VW Lily";
SpGetTokenFromId(pszTokenId , &pChineseToken);
pVoice->SetVoice(pChineseToken);
}
}
void CVoice::Speech(CString str,bool flag)
{USES_CONVERSION;
LPWSTR pwStr=new wchar_t[str.GetLength()+1];
wcscpy_s(pwStr,str.GetLength()+1,T2W((LPCTSTR)str));
if(flag ==true)
pVoice->Speak( pwStr, SPF_ASYNC, NULL);
else
pVoice->Speak( pwStr, SPF_DEFAULT, NULL);
delete[] pwStr;
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
网页标题:C++桌面应用中文语音包功能接口-创新互联
URL地址:http://ybzwz.com/article/ggeie.html