当前位置:   article > 正文

C++ SAPI5设置输出设备(声卡)_linux下使用microsoft sapi5进行输出声音

linux下使用microsoft sapi5进行输出声音
  1. 微软官方例子,枚举所有输出设备,并且设置给ISpVoice:
  2. // Declare local identifiers:
  3. HRESULT hr = S_OK;
  4. CComPtr<ISpObjectToken> cpAudioOutToken;
  5. CComPtr<IEnumSpObjectTokens> cpEnum;
  6. CComPtr<ISpVoice> cpVoice;
  7. ULONG ulCount = 0;
  8. // Create the SAPI voice.
  9. hr = cpVoice.CoCreateInstance(CLSID_SpVoice);
  10. if (SUCCEEDED (hr))
  11. {
  12. // Enumerate the available audio output devices.
  13. hr = SpEnumTokens( SPCAT_AUDIOOUT, NULL, NULL, &cpEnum; );
  14. }
  15. if (SUCCEEDED (hr))
  16. {
  17. // Get the number of audio output devices.
  18. hr = cpEnum->GetCount( &ulCount; );
  19. }
  20. // Obtain a list of available audio output tokens,
  21. // set the output to the token, and call Speak.
  22. while (SUCCEEDED(hr) && ulCount--)
  23. {
  24. if (SUCCEEDED (hr))
  25. {
  26. hr = cpEnum->Next( 1, &cpAudioOutToken;, NULL );
  27. }
  28. if (SUCCEEDED (hr))
  29. {
  30. hr = cpVoice->SetOutput( cpAudioOutToken, TRUE );
  31. }
  32. if (SUCCEEDED (hr))
  33. {
  34. hr = cpVoice->Speak( L"How are you?", SPF_DEFAULT, NULL );
  35. }
  36. }
  37. if (SUCCEEDED (hr))
  38. {
  39. // Do more stuff here.
  40. }
  1. 把输出设备显示到ComboBox里:
  2. HRESULT hr = SpInitTokenComboBox( hWndComboAudioOut , SPCAT_AUDIOOUT);
  3. VERIFY(SUCCEEDED(hr))
  4. CComPtr<ISpVoice> cpVoice;
  5. cpVoice.CoCreateInstance( CLSID_SpVoice );
  6. VERIFY(SUCCEEDED(hr))
  7. ISpObjectToken *pTokenAudioOut=nullptr;
  8. VERIFY(SUCCEEDED(new_voice.cpVoice->GetOutputObjectToken(&pTokenAudioOut)));
  9. CSpDynamicString dstrDesc;
  10. VERIFY(SUCCEEDED(SpGetDescription(pTokenAudioOut, &dstrDesc)));
  11. ::SendMessageW(hWndComboAudioOut, CB_SELECTSTRING, 0, (LPARAM)dstrDesc.m_psz);
  12. 从ComboBox里的选中文字设置TTS输出设备
  13. ISpObjectToken* pToken = SpGetCurSelComboBoxToken( hWndCombo );
  14. ISpVoice->SetOutPut(pToken)直接使用
  15. 还可以通过Token获得ID
  16. WCHAR wszTolenName[256] = L"";
  17. WCHAR *pWsz = NULL ;
  18. pToken->GetId( &pWsz ) ;
  19. //再把ID还原成Token
  20. CComPtr<IEnumSpObjectTokens> cpEnum;
  21. CStringW strIdW( pWsz ) ;
  22. CComPtr<ISpObjectToken> cpVoiceToken
  23. HRESULT hr = SpGetTokenFromId(strIdW, &cpVoiceToken);
  24. ISpVoice->SetOutPut(cpVoiceToken)直接使用

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/326021
推荐阅读
相关标签
  

闽ICP备14008679号