Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 987
Cherie CH. Avatar asked Oct 05 '15 07:10

Cherie CH.


1 Answers

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.

like image 78
ChrisCM Avatar answered Sep 27 '22 20:09

ChrisCM