使用之前需要先安裝中文語音包(男聲+女聲安裝包下載)
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++桌面應用中文語音包功能接口-創新互聯
本文網址:http://vcdvsql.cn/article4/ggeie.html
成都網站建設公司_創新互聯,為您提供品牌網站設計、網站建設、關鍵詞優化、網站內鏈、企業建站、品牌網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯