c++字符串语音播放 您所在的位置:网站首页 important怎么读语音播放 c++字符串语音播放

c++字符串语音播放

2024-06-29 07:41| 来源: 网络整理| 查看: 265

C++语音识别接口快速入门(Microsoft Speech SDK)

vs2013上亲测有效

C++语音识别接口快速入门(Microsoft Speech SDK) 尤其注意其中的宽字符串转化

 

直接调用read(“  ”)就可以朗读了

#includeusing namespace std;#include //导入语音头文件#include #pragma comment(lib,"sapi.lib") //导入语音头文件库void  MSSSpeak(LPCTSTR speakContent)// speakContent为LPCTSTR型的字符串,调用此函数即可将文字转为语音{ ISpVoice *pVoice = NULL; //初始化COM接口 if (FAILED(::CoInitialize(NULL))) MessageBox(NULL, (LPCWSTR)L"COM接口初始化失败!", (LPCWSTR)L"提示", MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2); //获取SpVoice接口 HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void**)&pVoice); if (SUCCEEDED(hr)) { pVoice->SetVolume((USHORT)100); //设置音量,范围是 0 -100 pVoice->SetRate(0); //设置速度,范围是 -10 - 10 hr = pVoice->Speak(speakContent, 0, NULL); pVoice->Release(); pVoice = NULL; } //释放com资源 ::CoUninitialize();}//string转换车wstring  std::wstring  StringToWString(const std::string& s){ std::wstring wszStr; int nLength = MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, NULL, NULL); wszStr.resize(nLength); LPWSTR lpwszStr = new wchar_t[nLength]; MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, lpwszStr, nLength); wszStr = lpwszStr; delete[] lpwszStr; return wszStr;}void read(string temp){ wstring a = StringToWString(temp); LPCWSTR str = a.c_str(); /*不知道为什么Cstr不行*/ MSSSpeak(str); cout



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有