C++桌面应用中文语音包功能接口-创新互联

使用之前需要先安装中文语音包(男声+女声安装包下载)
Voice.h文件
(头文件包含的lib库资源如果缺失,百度下载或我的资源)

成都创新互联一直在为企业提供服务,多年的磨炼,使我们在创意设计,网络营销推广到技术研发拥有了开发经验。我们擅长倾听企业需求,挖掘用户对产品需求服务价值,为企业制作有用的创意设计体验。核心团队拥有超过10多年以上行业经验,涵盖创意,策化,开发等专业领域,公司涉及领域有基础互联网服务服务器托管雅安成都app软件开发公司、手机移动建站、网页设计、网络整合营销。
#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。内容未经允许不得转载,或转载时需注明来源: 创新互联

营销型网站建设