添加头文件
声明变量
pSpVoice = NULL;
bool CChKBarSCCodeApp::InitSpVoice()
{
HRESULT hr = ::CoInitialize(NULL);
if (!SUCCEEDED(hr))
{
AfxMessageBox(_T("声音环境初始化失败!"));
return false;
}
if (FAILED(CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_INPROC_SERVER, IID_ISpVoice, (void**)&pSpVoice)))
{
AfxMessageBox(_T(" 获取ISpVoice接口失败!"));
return -1;
}
if (SUCCEEDED(SpFindBestToken(SPCAT_VOICES, L"language=804", NULL, &pSpObjectToken)))
{
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);
}
int CChKBarSCCodeApp::ExitInstance()
{
if (pSpVoice)
{
pSpVoice->Release();
}
::CoUninitialize();
return CWinApp::ExitInstance();
}