【问题标题】:AVSpeechUtterance utterance voice in silent modeAVSpeechUtterance 静音模式下的语音
【发布时间】:2014-09-26 03:15:51
【问题描述】:

我正在使用 AVSpeechUtterance 朗读给定的文本。我正在使用下面的代码,并且在“iPhone 铃声模式”下工作正常,但是当我将 iPhone 更改为“静音模式”时,语音变得静音。当它是“静音模式”时,我无法听到说话的声音。我应该怎么做才能听到“静音模式”中的话语声音。

AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:@"Hello World"];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
[utterance setRate:AVSpeechUtteranceMinimumSpeechRate];
[utterance setVolume:1];
[self.speechSynthesizer speakUtterance:utterance];

【问题讨论】:

    标签: ios objective-c iphone avspeechsynthesizer onutterancecompleted


    【解决方案1】:

    在您的代码之前添加以下代码。您也可以在 appDelegate 中编写相同的代码。

    NSError *setCategoryErr = nil;
    NSError *activationErr  = nil;
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
    [[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
    

    【讨论】:

    • 提示:您似乎必须在分配语音合成器之前设置音频会话类别并激活会话。
    【解决方案2】:

    Swift 3.0 答案

    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
    }
    catch let error as NSError {
        print("Error: Could not set audio category: \(error), \(error.userInfo)")
    }
    
    do {
        try AVAudioSession.sharedInstance().setActive(true)
    }
    catch let error as NSError {
        print("Error: Could not setActive to true: \(error), \(error.userInfo)")
    }
    

    【讨论】:

      【解决方案3】:

      我遇到了类似的问题,我通过添加 AVAudio 会话方法进行了修复。斯威夫特 5.0

          // this  AVAudioSession method is for speak text when device is in silent mode
          do {
              try AVAudioSession.sharedInstance().setCategory(.playback,mode: .default)
      
          } catch let error {
              print("This error message from SpeechSynthesizer \(error.localizedDescription)")
          }
          
          let speechSynthesizer = AVSpeechSynthesizer()
          let speechUtterance: AVSpeechUtterance = AVSpeechUtterance(string: text)
          
          speechUtterance.rate = AVSpeechUtteranceMaximumSpeechRate / 2.3
          speechUtterance.voice = AVSpeechSynthesisVoice(language: "en-US")
          speechSynthesizer.speak(speechUtterance)
          
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-21
        • 2017-09-24
        • 1970-01-01
        相关资源
        最近更新 更多