使用之前需要先安装中文语音包(男声+女声安装包下载)
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++桌面应用中文语音包功能接口-创新互联
本文网址:https://www.cdcxhl.com/article4/ggeie.html
成都网站建设公司_创新互联,为您提供品牌网站设计、网站建设、关键词优化、网站内链、企业建站、品牌网站建设
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联