How to use Windows System.Speech for TTS in PowerShell 7 (or is there an alternative)

感情迁移 提交于 2020-05-23 10:24:32

问题


I have the same profile.ps1 in both WindowsPowerShell and PowerShell. It includes commands that invoke Windows Text-To-Speech However, these commands fail when run in PowerShell 7.

The errors occur when I try to use the $PomrptTTS object I create with the following code:

Add-Type -AssemblyName System.speech
$PromptTTS = New-Object System.Speech.Synthesis.SpeechSynthesizer

In PowerShell 7, any attempt to access or use my $PormptTTS object, produces the following:

SetValueInvocationException: ....\profile.ps1:82
Line |
  82 |  $PromptTTS.Rate = 0 ; $PromptTTS.Speak("Time for the $((Get-Date).DayofWeek) shuffle")
     |  ~~~~~~~~~~~~~~~~~~~
     | Exception setting "Rate": "Object reference not set to an instance of an object."

MethodInvocationException: ....\profile.ps1:82
Line |
  82 |  … e = 0 ; $PromptTTS.Speak("Time for the $((Get-Date).DayofWeek) shuffle")
     |                                             ~~~~~~~~~~~~~~~~~~~~
     | Exception calling "Speak" with "1" argument(s): "Object reference not set to an instance of an object."

回答1:


As of PowerShell 7.0 / .NET Core 3.1, System.Speech.Synthesis.SpeechSynthesizer is considered a .NET Framework-only API and therefore not supported in .NET Core.

  • A discussion about this is ongoing in this GitHub issue; since the underlying API is specific to Windows, the question is whether it's worth exposing via the cross-platform .NET Core framework.

The workaround is to use the SAPI.SpVoice COM object (which the .NET Framework API is ultimately based on, I presume):

$sp = New-Object -ComObject SAPI.SpVoice
$sp.Speak("Time for the $((Get-Date).DayOfWeek) shuffle")

A related question asks about changing the speaking voice, which, unfortunately doesn't seem to be supported in PowerShell Core as of PowerShell 7.0, due to limited COM support - see this answer.




回答2:


mklement0's answer is better, but if you want a slightly hacky Windows 10+ option that works with version 7 (but not version 5) you can use the Windows Media Foundation speech api instead.

Here's a gist that uses this option.

The nice thing about using the Windows Media Foundation is you can use all of the "OneCore" voices that are supported in Windows Narrator, whereas SAPI only supports the three default voices



来源:https://stackoverflow.com/questions/61294347/how-to-use-windows-system-speech-for-tts-in-powershell-7-or-is-there-an-alterna

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!