SpVoice GetVoices method (SAPI 5.3)
The GetVoices method returns a selection of voices available to the voice.
Selection criteria may be applied optionally. In the absence of selection criteria, all voices are returned in the selection, ordered alphabetically by the voice name. If no voices match the criteria, GetVoices returns an empty selection, that is, an ISpeechObjectTokens collection with a Count of zero.
See Object Tokens and Registry Settings White Paper for a list of SAPI 5-defined attributes.
SpVoice.GetVoices( [RequiredAttributes As String = ""], [OptionalAttributes As String = ""] ) As ISpeechObjectTokens
- [Optional] Specifies the RequiredAttributes. All voices selected will match these specifications. If no voices match the selection, the selection returned will contain no voices. By default, no attributes are required and so the list returns all the tokens discovered.
- [Optional] Specifies the OptionalAttributes. Voices which match these specifications will be returned at the front of the selection. By default, no attribute is specified and the list returned from the speech configuration database is in the order that attributes were discovered.
An ISpeechObjectTokens variable containing the collection of voice tokens selected.
The format of selection criteria is "Attribute = Value" and "Attribute != Value." Voice attributes include "Gender," "Age," "Name," "Language," and "Vendor."
The following Visual Basic form code demonstrates the use of the GetVoices method and the Voice property. To run this code, create a form with the following controls:
- A command button called Command1
- A list box called List1
Paste this code into the Declarations section of the form.
The Form_Load procedure creates a voice object and displays the names of all available voices in the list box. Select a voice name in the list box and then click Command1. The Command1 procedure sets the voice object's Voice property to the selected name and causes the voice to speak its new name.
Option Explicit Private V As SpeechLib.SpVoice Private T As SpeechLib.ISpeechObjectToken Private Sub Command1_Click() On Error GoTo EH If List1.ListIndex > -1 Then ' Set voice object to voice name selected in ' list box (new voice speaks its own name): Set V.Voice = V.GetVoices().Item(List1.ListIndex) V.Speak V.Voice.GetDescription Else MsgBox "Please select a voice from the list box." End If EH: If Err.Number Then ShowErrMsg End Sub Private Sub Form_Load() On Error GoTo EH Dim strVoice As String Set V = New SpVoice 'Get each token in the collection returned by GetVoices For Each T In V.GetVoices strVoice = T.GetDescription 'The token's name List1.AddItem strVoice 'Add to listbox Next EH: If Err.Number Then ShowErrMsg End Sub Private Sub ShowErrMsg() ' Declare identifiers: Dim T As String T = "Desc: " & Err.Description & vbNewLine T = T & "Err #: " & Err.Number MsgBox T, vbExclamation, "Run-Time Error" End End Sub