如何在PowerShell 7中将Windows System.Speech用于TTS(或有替代方法) [英] How to use Windows System.Speech for TTS in PowerShell 7 (or is there an alternative)

查看:353
本文介绍了如何在PowerShell 7中将Windows System.Speech用于TTS(或有替代方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WindowsPowerShell和PowerShell中,我具有相同的profile.ps1. 它包括调用Windows Text-To-Speech的命令 但是,这些命令在PowerShell 7中运行时会失败.

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.

当我尝试使用通过以下代码创建的$ PomrptTTS对象时发生错误:

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

在PowerShell 7中,任何访问或使用我的$ PormptTTS对象的尝试都会产生以下结果:

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."

推荐答案

从PowerShell 7.0/.NET Core 3.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.

  • 此GitHub问题中正在进行有关此问题的讨论;由于底层API是Windows特定于Windows的,因此问题在于是否值得通过跨平台.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.

解决方法是使用 SAPI.SpVoice COM对象(我认为,.NET Framework API最终基于该对象):

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")


相关问题询问有关更改口语语音的问题,不幸的是,由于对COM的支持有限,PowerShell Core 7.0至今似乎不支持该功能-请参见


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.

这篇关于如何在PowerShell 7中将Windows System.Speech用于TTS(或有替代方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆