如何在信号丢失时播放画外音通知(即没有用户交互)?

How to play a Voiceover notification when signal lost (i.e., no user interaction)?

我构建了一个简单的音频流应用程序,如下所示:

当我失去音频流连接时,我的应用程序将此屏幕覆盖在 UI:

我想让盲人或视力不佳的人也可以使用该应用程序,所以我希望画外音在信号丢失和覆盖屏幕出现时说 "Signal lost" 和 "Reconnecting"。这一切都是在没有任何用户交互的情况下发生的(类似于屏幕超时时发生的情况,VO 说 'screen dimmed')。如果 VO 能说话就好了,"Signal regained" 在重新连接时也一样,但不是绝对必要的。

更棘手的是,当 'signal lost' 屏幕打开时,用户应该无法操作下面主 UI 中的任何其他字段。

我该如何完成?

在此先感谢您的帮助!

好吧,您可以创建语音的 mp3 录音,然后在屏幕出现时使用 AVAudio 播放它。

如果您打算播放音频但没有 MP3,则可以使用 AVFoundation:

import AVFoundation

创建一个综合警告等的函数,例如

/// Reads A Word Using Speech Syntheseis
///
/// - Parameters:
///   - word: String
func readWordAloud(_ word: String){

    //1. Create The Word To Be Read Aloud
    let wordReader = AVSpeechUtterance(string: word)
    wordReader.volume = 1

    //2. Pass It To The Speech Synthesizer
    let speechSynthesizer = AVSpeechSynthesizer()
    speechSynthesizer.speak(wordReader)

}

像这样调用方法:

readWordAloud("No Audio Available")

要禁用与 UIView 的交互,只需使用:

self.view.isUserInteractionEnabled = false

您可以使用 UIAccessibilityPostNotification 制作一个 "announcement",这将导致它大声朗读(仅当启用 VoiceOver 时):

UIAccessibilityPostNotification(
    UIAccessibilityAnnouncementNotification, 
    "Signal Lost" as NSString
)

documentation for UIAccessibilityAnnouncementNotification 解释了 NSString 参数以及何时适合使用:

This notification includes a parameter, which is an NSString object that contains the announcement. An assistive technology outputs the announcement string contained in the parameter.

Use this notification to provide accessibility information about events that do not update the application user interface (UI), or that update the UI only briefly.

我的建议是谨慎使用公告,但我要说的是,在没有任何用户交互的情况下发生的事件通常——但并非总是——很好地使用它们。