2016-05-23 50 views
1

我正在使用AVSpeechUtterance來做一些文本到語音轉換。對於iOS的每個版本,AVSpeechUtterance播放速度不同

當我第一次爲iOS7寫這篇文章時,所有工作都很好,但使用iOS8時,默認速度變得更快。 我解決了這個問題的解決方法與代碼:

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1) 
    speechUtterance.rate = 0.15; 
else 
    speechUtterance.rate = 0.3; 

看來,有不同版本的iOS中使用不同的費率。

處理這種情況的最佳方法是什麼?

在iOS9中,您會收到新的聲音,但您無法再像< 9.0那樣檢索它們。 9.0增加了一種獲取語音的新方法:AVSpeechSynthesisVoice voiceWithIdentifier。而三位新成員:

@property(nonatomic, readonly) NSString *identifier NS_AVAILABLE_IOS(9_0); 
@property(nonatomic, readonly) NSString *name NS_AVAILABLE_IOS(9_0); 
@property(nonatomic, readonly) AVSpeechSynthesisVoiceQuality quality NS_AVAILABLE_IOS(9_0); 

回答

1

隨着iOS9你需要使用亞歷克斯,請執行下列操作:

if (![AVSpeechSynthesisVoice voiceWithIdentifier:AVSpeechSynthesisVoiceIdentifierAlex]) { 
     AVSpeechSynthesisVoice *alex = [AVSpeechSynthesisVoice voiceWithIdentifier:AVSpeechSynthesisVoiceIdentifierAlex]; 
    } 

我想這reference(爲我工作):

的iOS 8:

- (void)speechText:(NSString *)text 
{ 
    AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init]; 
    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:text]; 
    utterance.rate = AVSpeechUtteranceMinimumSpeechRate; 
    utterance.volume = 1; 
    // initialize voice with language code 
    AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:[AVSpeechSynthesisVoice currentLanguageCode]]; 
    utterance.voice = voice; 
    [synthesizer speakUtterance:utterance]; 
} 

iOS 9:

- (void)speechText:(NSString *)text 
{ 
    AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init]; 
    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:text]; 
    utterance.rate = AVSpeechUtteranceMinimumSpeechRate; 
    utterance.volume = 1; 
    // initialize voice with voice identifier 
    AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithIdentifier:AVSpeechSynthesisVoiceIdentifierAlex]; 
    utterance.voice = voice; 
    [synthesizer speakUtterance:utterance]; 
} 
相關問題