我正在使用AVSpeechUtterance来讲述给定的文字。我正在使用下面的代码并在“iPhone Ringer模式”中正常工作,但当我将iPhone更改为“静音模式”时,话语声音变得无声。当它是“静音模式”时,我无法听到发声的声音。我该怎么做才能听到“静音模式”中的话语声音。
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:@"Hello World"];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
[utterance setRate:AVSpeechUtteranceMinimumSpeechRate];
[utterance setVolume:1];
[self.speechSynthesizer speakUtterance:utterance];
答案 0 :(得分:14)
在代码之前添加以下代码。您也可以在appDelegate中编写相同的代码。
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
答案 1 :(得分:7)
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)")
}
答案 2 :(得分:0)
我有一个类似的问题,我通过添加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)
}