Platforms to show: All Mac Windows Linux Cross-Platform

/AVFoundation/AVSpeechSynthesis


Required plugins for this example: MBS MacBase Plugin, MBS AVFoundation Plugin, MBS Main Plugin, MBS MacCocoa Plugin

Last modified Fri, 20th Feb 2025.

You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /AVFoundation/AVSpeechSynthesis

Download this example: AVSpeechSynthesis.zip

Project "AVSpeechSynthesis.xojo_binary_project"
FileTypes
Filetype Text
End FileTypes
Class MainWindow Inherits Window
Const kColumnGender = 2
Const kColumnIdentifier = 0
Const kColumnLanguage = 4
Const kColumnName = 1
Const kColumnTraits = 3
Control List Inherits ListBox
ControlInstance List Inherits ListBox
EventHandler Sub DoubleClick() If Me.ListIndex < Me.ListCount And Me.ListIndex >= 0 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 NSSpeechSynthesizerMBS.isAnyApplicationSpeaking then Title="Voices: Computer is speaking" else Title="Voices" end if End EventHandler
End Control
EventHandler Function KeyDown(Key As String) As Boolean if asc(key)=27 then if s.IsSpeaking then Call s.stopSpeakingAtBoundary(s.SpeechBoundaryImmediate) End If Return true End If End EventHandler
EventHandler Sub Open() c=New AVSpeechSynthesizerMBS LoadVoiceList End EventHandler
Sub LoadVoiceList() Dim c As Integer = List.ListCount List.DeleteAllRows Var voices() As AVSpeechSynthesisVoiceMBS = AVSpeechSynthesisVoiceMBS.speechVoices For Each voice As AVSpeechSynthesisVoiceMBS In voices List.AddRow voice.identifier, voice.name Select Case voice.gender Case voice.SpeechSynthesisVoiceGenderFemale List.Cell(List.LastIndex, kColumnGender)="female" Case voice.SpeechSynthesisVoiceGenderMale List.Cell(List.LastIndex, kColumnGender)="Male" Case voice.SpeechSynthesisVoiceGenderUnspecified List.Cell(List.LastIndex, kColumnGender)="None" End Select Select Case voice.voiceTraits Case voice.SpeechSynthesisVoiceTraitNone List.Cell(List.LastIndex, kColumnTraits)="None" Case voice.SpeechSynthesisVoiceTraitIsNoveltyVoice List.Cell(List.LastIndex, kColumnTraits)="NoveltyVoice" Case voice.SpeechSynthesisVoiceTraitIsPersonalVoice List.Cell(List.LastIndex, kColumnTraits)="PersonalVoice" End Select List.Cell(List.LastIndex, kColumnLanguage)=voice.language Next System.DebugLog "Voices count: "+List.ListCount.ToString+", was "+c.ToString End Sub
Protected Sub SaySomething() Var Text As String s = New MyAVSpeechSynthesizerMBS Var VoiceIdentiifer As String = List.Cell(List.ListIndex, kColumnIdentifier) Var voice As AVSpeechSynthesisVoiceMBS = AVSpeechSynthesisVoiceMBS.voiceWithIdentifier(VoiceIdentiifer) If voice <> Nil Then Text = "Hello World. My name is "+voice.name if Keyboard.AsyncOptionKey then text="Hello World!" end if Var u As New AVSpeechUtteranceMBS(Text) u.voice = voice s.speakUtterance u End If End Sub
Property Protected c As AVSpeechSynthesizerMBS
Property Protected s As MyAVSpeechSynthesizerMBS
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "Quit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu0 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "Clear"
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 MyAVSpeechSynthesizerMBS Inherits AVSpeechSynthesizerMBS
EventHandler Sub availableVoicesDidChange() MainWindow.Liste.AddRow "New voices found." MainWindow.LoadVoiceList End EventHandler
EventHandler Sub didCancelSpeechUtterance(utterance as AVSpeechUtteranceMBS) MainWindow.Liste.AddRow "Speech cancelled." End EventHandler
EventHandler Sub didContinueSpeechUtterance(utterance as AVSpeechUtteranceMBS) MainWindow.Liste.AddRow "Speech continued." End EventHandler
EventHandler Sub didFinishSpeechUtterance(utterance as AVSpeechUtteranceMBS) MainWindow.Liste.AddRow "Speech finished." End EventHandler
EventHandler Sub didPauseSpeechUtterance(utterance as AVSpeechUtteranceMBS) MainWindow.Liste.AddRow "Speech paused." End EventHandler
EventHandler Sub didStartSpeechUtterance(utterance as AVSpeechUtteranceMBS) MainWindow.Liste.AddRow "Speech started." End EventHandler
End Class
Sign
End Sign
End Project

Download this example: AVSpeechSynthesis.zip

The items on this page are in the following plugins: MBS AVFoundation Plugin.


The biggest plugin in space...