Strange properties of the "speak" class

Viewed 35

I have a code that involves the System.speech assembly in a for loop but the errors thrown at me are exceedingly strange from what I've seen.

I have a code that generates 10 random letters and/or numbers then stores them in an array which I would grab certain elements of and then use the text-to-speech engine. However, I want to have a pause in between each of the speeches but I get thrown the error:

PS Core 7.1 Error

MethodException:
Line |
  13 |          $speak.speak($say)
     |          ~~~~~~~~~~~~~~~~~~
     | Multiple ambiguous overloads found for "speak" and the argument count: "1".

Or

PS 5.1 Error

Multiple ambiguous overloads found for "speak" and the argument count: "1".
At line:13 char:5
+     $speak.speak($say)
+     ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest


My code is

Add-Type -AssemblyName System.speech
$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer
$set = @(48..57 + 65..90)

for($i=0;$i -lt $set.length; $i++){
    [char]$set[$i] = $set[$i] 
}

$values = $set | get-random -count 10

for($i=0; $i -lt $values.length; $i++){
    $say = $values[$i]
    $speak.speak($say)
    timeout /T 1 /NOBREAK > $null
}

I don't understand what the error means in this context, but if I just run

$speak.speak($values)

It will correctly speak, but however if I try to speak each value of the array one by one, the error occurs 10 times or however many times there are values in the array. Any information on why this error occurs, what the error is called, or anything that can give me more information about how to fix this would be greatly appreciated. Thank you!

1 Answers

In your script, $say is of type [char], but none of the implementations of Speak() accept a [char] as a parameter - so PowerShell can't decide which one (which overload) to pick!

Convert $say to a string before passing it:

$speak.Speak("$say")

... and now PowerShell will be able to figure out the correct method implementation to execute :)

Related