ios - How to make speech synthesiser speak a bunch of utterances, one after another? -
i building app speak sentences/words stored in playlist 1 one. both playlists , items in them stored inside core data.
i added play/stop button in storyboard. here event handler it:
@ibaction func playclick(sender: uibarbuttonitem) { if isplaying { synthesizer.stopspeakingatboundary(.immediate) isplaying = false sender.image = uiimage(named: "play") } else { sender.image = uiimage(named: "stop") isplaying = true string in playlist.utterrances! { let utterance = usersettings.getprefutterance((string as! utterance).string!) utterance.postutterancedelay = 2 synthesizer.speakutterance(utterance) } isplaying = false sender.image = uiimage(named: "play") } }
isplaying
(type bool
), synthesizer
(type avspeechsynthesizer
) , playlist
(type playlists
, entity description) class-level variables.
i think code here pretty clear. first decide whether button should stop or start synthesizer. if latter, loop through items in playlist , synthesise them. switch images , mutate isplaying
but when run app , press button, nothing @ happens. no sound, no image change, no nothing. think because of last 2 lines in method, deleted them. time, image of button changes still no sound.
i think must because telling synthesiser start speaking before last utterance finished. or need know when synthesiser stops speaking , start utterance.
"but why don't join strings , synthesise once?", asked. because want have two-second delay between each item in playlist!
question:
how can play contents of playlist 1 after another? (with noticeable amount of delay in between)
or
what characters need use create avspeechutterance
can pause noticeable amount of time?
the best way using avspeechsynthesizerdelegate
.
it has method... speechsynthesizer(_:didfinishspeechutterance:)
called when utterance finishes.
you can keep track of utterance being spoken , when delegate gets informed of being finished start next one.
if want them play when button clicked disable button when delegate gets informed of didstartspeechutterance
, enable button on didfinish...
.
Comments
Post a Comment