【问题标题】:Announce text using AVSpeechSynthesizer after VoiceOver stops speaking textVoiceOver 停止朗读文本后使用 AVSpeechSynthesizer 宣布文本
【发布时间】:2021-07-01 04:03:31
【问题描述】:

当我的应用程序的所有用户执行某些操作时,我需要向他们宣布一些文本。为此,我使用AVSpeechSynthesizer。这很好用,除非您使用 VoiceOver 来执行操作。因为VoiceOver是在向用户公布一些系统提供的信息,所以我的AVSpeechUtterance同时播放,所以声音重叠。如何将我的语音进行排队,以便在 VoiceOver 说完之后才播放?

【问题讨论】:

    标签: ios voiceover uiaccessibility avspeechsynthesizer


    【解决方案1】:

    您可以通过观察 VoiceOver 活动来实现这一点。首先添加一个 Voiceover 通知观察者:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(voiceOverDidStop:) name:UIAccessibilityAnnouncementDidFinishNotification object:nil];
    

    那么,在指定的方法中:

    -(void)voiceOverDidStop:(NSNotification*)n {
        NSString* msg;
        NSNumber* finished;
    
        msg = [[n userInfo] objectForKey:UIAccessibilityAnnouncementKeyStringValue];
        finished = [[n userInfo] objectForKey:UIAccessibilityAnnouncementKeyWasSuccessful];
    
        if(finished) {
            // send the AVSpeechSynthsizer message
        }
    }
    

    请记住在处置您的应用之前删除观察!

    您可以使用的另一种方法(如果适用)是编辑用户正在使用的对象的accessibilityLabelaccessibilityHint 属性。将这些属性设置为 @"",以便 VoiceOver 知道该对象无话可说。

    希望这对您有所帮助,即使我的回答来晚了: )

    【讨论】:

      【解决方案2】:

      检查 VoiceOver 是否正在运行,并在您的消息中发布UIAccessibilityAnnouncementNotification,而不是使用AVSpeechSynthesizer

      【讨论】:

      • 这在这种情况下是行不通的,因为我需要控制这个公告的语调和语速。
      • 在这种情况下,我认为现有的任何 API 都不支持您的要求。
      【解决方案3】:

      我已经用asyncAfter 完成了这个,代码是 SwiftUI 但我希望你明白:

      Button {
          if !UIAccessibility.isVoiceOverRunning {
              readOutLoud(translation)
          }
      } label: {
          Image(systemName: "speaker.wave.3.fill")
      }
      .accessibilityElement()
      .accessibilityLabel("Listen")
      .accessibilityAction {
          DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
              readOutLoud(translation)
          })
      }
      

      当按下按钮且 VoiceOver 未激活时,该按钮会读取文本。

      激活 VoiceOver 后,文本将通过 accessibilityAction 以所需的延迟朗读。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多