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

删除回忆录丶 提交于 2020-07-05 09:18:17

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/32943355/how-to-stop-text-to-speech-when-voiceover-is-speaking-or-vice-versa-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!