我正在为我的应用程序添加语音提示,并在iOS 7中测试AVSpeechUtterance,但默认语音速率非常快。最低语速更容易理解。但最大音量值1太安静了!我在iPhone 4上测试了它,音量一直在变高。某些东西一定是错的,否则它将如何可用。
AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc]init];
NSString *mystring = [NSString stringWithFormat:@"Talk String Here %@",myObject.name];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:mystring];
[utterance setRate:AVSpeechUtteranceMinimumSpeechRate];
[utterance setVolume:1];
[synthesizer speakUtterance:utterance];
答案 0 :(得分:5)
这对我有用(Swift 3.0)
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.defaultToSpeaker)
} catch {
print("audioSession properties weren't set because of an error.")
}
答案 1 :(得分:4)
@ tmr的帖子here为我解决了这个问题:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker
error:nil];
答案 2 :(得分:1)
对于Swift 5,您可以尝试一下(在合成器初始化之前添加):
let audioSession = AVAudioSession.sharedInstance()
try? audioSession.setCategory(AVAudioSession.Category.playAndRecord, mode: AVAudioSession.Mode.default, options: AVAudioSession.CategoryOptions.defaultToSpeaker)
try? audioSession.setMode(AVAudioSession.Mode.spokenAudio)
答案 3 :(得分:0)
iOS 8与iOS 9不同:
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:utteranceString];
if([iOSVersionDetector isiOS9OrHigher])
utterance.rate = AVSpeechUtteranceDefaultSpeechRate;
else
utterance.rate = 0.1f;
答案 4 :(得分:0)
对于 Swift 3 执行(以解决卷问题)
let audioSession = AVAudioSession.sharedInstance()
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.defaultToSpeaker)
答案 5 :(得分:0)
我的问题已修复,将模式设置为AVAudioSessionModeDefault
或AVAudioSessionModeSpokenAudio
:
let audioSession = AVAudioSession.sharedInstance()
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: .defaultToSpeaker)
try audioSession.setMode(AVAudioSessionModeSpokenAudio)
答案 6 :(得分:-2)
速率只是一个浮点值,为方便起见提供常量。 尝试使用[utterance setRate:AVSpeechUtteranceMaximumSpeechRate * .25];