首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVSpeechUtterance无声语音

AVSpeechUtterance无声语音
EN

Stack Overflow用户
提问于 2014-08-03 13:47:13
回答 3查看 3.2K关注 0票数 11

我正在使用AVSpeechUtterance来讲给定的文本。我正在使用下面的代码,并在'iPhone林格模式‘工作良好,但当我将iPhone改为’沉默模式‘时,声音变得静音。当它是“沉默模式”时,我无法听到它的声音。我该怎么做才能听到“沉默模式”中的声音呢?

代码语言:javascript
复制
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:@"Hello World"];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
[utterance setRate:AVSpeechUtteranceMinimumSpeechRate];
[utterance setVolume:1];
[self.speechSynthesizer speakUtterance:utterance];
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-08-15 15:20:27

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

代码语言:javascript
复制
NSError *setCategoryErr = nil;
NSError *activationErr  = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
票数 16
EN

Stack Overflow用户

发布于 2016-10-10 20:49:04

Swift 3.0答案

代码语言:javascript
复制
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)")
}
票数 10
EN

Stack Overflow用户

发布于 2020-10-25 05:36:59

我也遇到了类似的问题,我通过添加AVAudio session方法修复了这个问题。Swift 5.0

代码语言:javascript
复制
    // 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)
    
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25105283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档