Platforms to show: All Mac Windows Linux Cross-Platform
/MacCocoa/Speech/NSSpeechSynthesizer
Required plugins for this example: MBS MacCocoa Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCocoa/Speech/NSSpeechSynthesizer
This example is the version from Fri, 5th Jan 2023.
Project "NSSpeechSynthesizer.xojo_binary_project"
FileTypes
Filetype text
End FileTypes
Class MainWindow Inherits Window
Control List Inherits ListBox
ControlInstance List Inherits ListBox
EventHandler Sub DoubleClick()
if me.ListIndex<me.ListCount and me.ListIndex>-1 then
SaySomething
end if
End EventHandler
End Control
Control Liste Inherits ListBox
ControlInstance Liste Inherits ListBox
End Control
Control Timer1 Inherits Timer
ControlInstance Timer1 Inherits Timer
EventHandler Sub Action()
if c.isAnyApplicationSpeaking then
Title="Voices: Computer is speaking"
else
Title="Voices"
end if
End EventHandler
End Control
Control CheckBox1 Inherits CheckBox
ControlInstance CheckBox1 Inherits CheckBox
End Control
EventHandler Function KeyDown(Key As String) As Boolean
if asc(key)=27 then
if s.IsSpeaking then
s.StopSpeaking
end if
Return true
end if
End EventHandler
EventHandler Sub Open()
dim i as integer
dim v as NSVoiceMBS
dim n as string
c=new NSSpeechSynthesizerMBS
// availableVoicesCount is a shared method and needs RB 2006r4 or newer
for i=0 to c.availableVoicesCount-1
n=c.availableVoice(i)
v=c.attributesForVoice(n)
List.AddRow v.Identifier, v.Name
if v.Age > 0 then
List.Cell(List.LastIndex,2)=str(v.Age)
end if
List.Cell(List.LastIndex,3)=v.Gender
List.Cell(List.LastIndex,4)=v.Demotext
List.Cell(List.LastIndex,5)=v.Language
next
End EventHandler
Protected Sub SaySomething()
dim text as string
s=new MyNSSpeechSynthesizerMBS(List.Cell(List.ListIndex,0))
s.UsesFeedbackWindow = CheckBox1.Value
Liste.DeleteAllRows
text=List.Cell(List.ListIndex,4)
if Keyboard.AsyncOptionKey then
text="Hello World!"
end if
if not s.StartSpeakingString(text) then
MsgBox "Failed to speak!"
end if
End Sub
Property Protected c As NSSpeechSynthesizerMBS
Property Protected s As MyNSSpeechSynthesizerMBS
End Class
MenuBar MenuBar1
MenuItem UntitledMenu1 = ""
MenuItem FileMenu = "&File"
MenuItem FileQuit = "Quit"
MenuItem UntitledMenu5 = ""
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu0 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "Clear"
MenuItem UntitledMenu4 = ""
MenuItem UntitledMenu3 = ""
MenuItem UntitledMenu2 = ""
End MenuBar
Class App Inherits Application
EventHandler Sub Open()
if TargetMachO=false then
MsgBox "This example needs a MachO target running on Mac OS X."
quit
end if
End EventHandler
End Class
Class MyNSSpeechSynthesizerMBS Inherits NSSpeechSynthesizerMBS
EventHandler Sub didFinishSpeaking(finishedSpeaking as boolean)
if finishedSpeaking then
MainWindow.Liste.AddRow "Speech finished."
else
MainWindow.Liste.AddRow "Speech cancelled."
end if
End EventHandler
End Class
End Project
See also:
The items on this page are in the following plugins: MBS MacCocoa Plugin.