我正在使用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];发布于 2014-08-15 15:20:27
在代码之前添加以下代码。您也可以在appDelegate中编写相同的代码。
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];发布于 2016-10-10 20:49:04
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)")
}发布于 2020-10-25 05:36:59
我也遇到了类似的问题,我通过添加AVAudio session方法修复了这个问题。Swift 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)
}https://stackoverflow.com/questions/25105283
复制相似问题