Search code examples
iosspeech

Register for Text-To-Speech callback with AVSpeechSynthesizer


I am using Text to speech in iOS 7 (AVSpeechUtterance).

Is there a way to register a callback which will be invoked upon completion of speech?

I need to know so that I can disable a play button while speech is running and re-enable the button when the speech is done.


Solution

  • According to the Apple Doc, An AVSpeechUtterance is the basic unit of speech synthesis.

    AVSpeechSynthesizer has a delegate : AVSpeechSynthesizerDelegate. This protocol contains interesting methods able to solve your problem. I quote:

    In Swift :

    speechSynthesizer(_:didCancelSpeechUtterance:)
    speechSynthesizer(_:didContinueSpeechUtterance:)
    speechSynthesizer(_:didFinishSpeechUtterance:)
    speechSynthesizer(_:didPauseSpeechUtterance:)
    speechSynthesizer(_:didStartSpeechUtterance:)
    speechSynthesizer(_:willSpeakRangeOfSpeechString:utterance:)
    

    In Objective C

    speechSynthesizer:didCancelSpeechUtterance:
    speechSynthesizer:didContinueSpeechUtterance:
    speechSynthesizer:didFinishSpeechUtterance:
    speechSynthesizer:didPauseSpeechUtterance:
    speechSynthesizer:didStartSpeechUtterance:
    speechSynthesizer:willSpeakRangeOfSpeechString:utterance:
    

    Take a look on the Apple Doc