Skip to content

Instantly share code, notes, and snippets.

@jsbain
Created September 15, 2016 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsbain/8381f7a28dbfade5ce1e1a69329ac4e5 to your computer and use it in GitHub Desktop.
Save jsbain/8381f7a28dbfade5ce1e1a69329ac4e5 to your computer and use it in GitHub Desktop.
objc_speech.py
from objc_util import *
AVSpeechUtterance=ObjCClass('AVSpeechUtterance')
AVSpeechSynthesizer=ObjCClass('AVSpeechSynthesizer')
AVSpeechSynthesisVoice=ObjCClass('AVSpeechSynthesisVoice')
# it seems in ios8.3 name and quality are not present...
voices=AVSpeechSynthesisVoice.speechVoices()
print(voices)
#on ios9, there should be a name and quality field.. but not on ios8. so i cant test, but you could try sesrching through voicces to see if quality() is different
voice=AVSpeechSynthesisVoice.voiceWithLanguage_('en-US')
#if you find what you need in voices, use that instead.
#I am not sure of the exact code, but something like the following
# though maybe v.identifier() or v.name()
# for v in voices:
# if 'Enhanced' in str( v.description()):
# voice=v
# break
synthesizer=AVSpeechSynthesizer.new()
utterance=AVSpeechUtterance.speechUtteranceWithString_("I'm sorry Dave, I'm afraid I can't do that")
#the value that sounds good apparantly depends on ios version
utterance.rate=0.1
utterance.voice=voice
utterance.useCompactVoice=False
synthesizer.speakUtterance_(utterance)
#you will want to read about AVSpeechSynthesizer if you want to queue, pause, etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment