How to stop text in speech when voiced, or vice versa in Swift?

My app currently implements AVSpeechSynthesizer to read instructions for each screen. The app also takes into account when the voice control availability feature is enabled.

The problem that I am currently facing is that the text-to-speech function overlaps with the audio function. Is there a solution for detecting that when a user moves to another item on the screen, the TTS stops talking or when the TTS speaks, the voice acting does not speak until the TTS ends (preferably the first).

The current development is on iOS 8 using Swift.

+5
source share
1 answer

OPTION 1

You can listen 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 messages when they are received.

Pros: gives VoiceOver user a lot of control.

Cons: You don’t know when VoiceOver finished reading a recently focused control, so you cannot 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 finishing.

Pros: Your ad will be fully read.

Cons: you take control of the hands of users when pausing VoiceOver.

Recommendations for

If your ads are short, pausing and resuming AT is not a terrible decision. Otherwise, I would recommend VoiceOver users to interrupt / cancel your ads by listening to UIAccessibilityElementFocusedNotification events and canceling any active announcements when they are received.

+4
source

Source: https://habr.com/ru/post/1232940/


All Articles