Cherie CH.
Cherie CH.

Reputation: 171

How to stop Text to Speech when Voiceover is speaking, or vice versa in Swift?

Right now, my app implements AVSpeechSynthesizer to read out instructions for each screen. The app also takes into consideration when Voiceover accessibility feature is enabled.

The problem I'm facing now is that the text to speech feature overlaps with the voiceover feature. Is there a solution to detect that when a user navigates to another element on the screen, TTS stops speaking, or when TTS is speaking, voiceover doesn't speak until TTS finishes (the former is preferred though).

The current development is on iOS 8, using Swift.

Upvotes: 6

Views: 6468

Answers (1)

MobA11y
MobA11y

Reputation: 18900

OPTION 1

You could listen for when new elements are focused by observing UIAccessibilityElementFocusedNotification notifications

[[NSNotificationCenter defaultCenter] addObserver:yourTTSManager
                                         selector:@selector(interruptTTSFunction:)
                                             name:UIAccessibilityElementFocusedNotification 
                                           object:nil];

and interrupt your custom speech synthesis announcements when they are received.

Pros: Gives the VoiceOver user a lot of control.

Cons: You don't know when VoiceOver is done reading off the newly focused control, so you can't use this to interrupt and restart announcements.

OPTION 2

You can tell VoiceOver to pause and restart by posting

UIAccessibilityPostNotification(UIAccessibilityPauseAssistiveTechnologyNotification, nil); 

before your announcement and

UIAccessibilityPostNotification(UIAccessibilityResumeAssistiveTechnologyNotification, nil); 

after it is complete.

Pros: Your announcement will get read off in it's entirety.

Cons: You take control out of the users hands when you pause VoiceOver.

RECOMENDATION

If your announcements are short, pausing and resuming the AT isn't a terrible solution. Otherwise, I would recommend allowing VoiceOver users to interrupt/cancel your announcements by listening for UIAccessibilityElementFocusedNotification events, and canceling any active announcements when they are received.

Upvotes: 5

Related Questions