c
复制代码
pSpVoice = NULL; //默认构造函数中初始化为空
bool CChKBarSCCodeApp::InitSpVoice()
{
HRESULT hr = ::CoInitialize(NULL); // COM初始化
if (!SUCCEEDED(hr))
{
AfxMessageBox(_T("声音环境初始化失败!"));
return false;
}
// 获取ISpVoice接口
if (FAILED(CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_INPROC_SERVER, IID_ISpVoice, (void**)&pSpVoice)))
{
AfxMessageBox(_T(" 获取ISpVoice接口失败!"));
return -1; // 当获取ISpVoice接口失败时,返回-1
}
//SpFindBestToken是SAPI中的一个函数,用于根据指定的属性条件查找最佳的语音token
if (SUCCEEDED(SpFindBestToken(SPCAT_VOICES, L"language=804", NULL, &pSpObjectToken)))//804代表中文
{
pSpVoice->SetVoice(pSpObjectToken);//设置中文
pSpVoice->SetVolume(m_oHardPara.m_nVolumeSize);//设置声音大小
pSpVoice->SetVolume(m_oHardPara.m_nVolumeSpeed);
}
else
{
AfxMessageBox(_T("没有安装微软语音库!"));
return false;
}
return true;
}
void CChKBarSCCodeApp::SpVoiceOK()
{
CString sTxt =_T("扫描成功");
pSpVoice->Speak(sTxt.AllocSysString(), SPF_ASYNC, NULL);//异步读取
}
void CChKBarSCCodeApp::SpVoiceNG()
{
CString sTxt = _T("扫描失败");
pSpVoice->Speak(sTxt.AllocSysString(), SPF_ASYNC, NULL);//异步读取
}