2013-12-11 36 views
0

我瞭解TTS僅適用於iOS 7在投票前請通讀。將iOS 7代碼添加到iOS 6項目

我的一位客戶希望在基於iOS 6的現有項目中添加文本到語音轉換功能。我瞭解iOS 7中我們已經內置了TTS,因此我想使用它。當項目是基於iOS上的7

AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init]; 
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"Please raise my salary!!!"]; 
[synthesizer speakUtterance:utterance]; 

下面的代碼工作正常,但我不能給這個代碼添加到基於在iOS 6

我使用這個現有項目thread的方式。我錯過了什麼嗎?或者這個內置在TTS中的東西是不同的東西?

+2

「適用於iOS 7.0及更高版本。」 –

+0

如何在iOS 6.0中使用iOS 7.0代碼? – suhit

+2

夥計們,他希望將代碼包含在應用程序中,但在運行iOS 6.0時在_inaccessible_中生成 – cjwirth

回答

2

由於您試圖運行無法在iOS 6上運行的代碼,因此您需要進行運行時檢查,以確保您可以運行試圖運行的代碼。一個快速的方法是檢查類是否可用:

if([AVSpeechSynthesizer class]) { 
    AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init]; 
    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"Please raise my salary!!!"]; 
    [synthesizer speakUtterance:utterance]; 
} else { 
    // Feature not available, do something else 
} 

無論代碼是否編譯,您都需要這樣做。

如果你的代碼甚至沒有正確編譯,然後有一些事情你可以檢查:

  • 是你的基地SDK設置到iOS 6.0?
  • AVSpeechSynthesizer是AVFoundation的一部分 - 您是否將您的項目鏈接到它?
+0

opps!,是的,你是對的。我做了#import ,但忘了將AVFoundation鏈接添加到我的項目中。 – brianLikeApple

1
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f){ 

    AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init]; 
    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"Please raise my salary!!!"]; 
    [synthesizer speakUtterance:utterance]; 
} 
//And an else block if you have a back-up plan 
+0

你在你身邊試過嗎?是的我知道。它在我身邊不起作用。否則我不會問這樣愚蠢的問題。 – brianLikeApple

+2

此代碼無法工作,因爲AVSpeechSynthesizer需要iOS7(https://developer.apple.com/library/IOs/documentation/AVFoundation/Reference/AVSpeechSynthesizer_Ref/Reference/Reference.html)。這隻會解決iOS6上的編譯錯誤。你需要找到OS超過7的替代方式。 –

+0

你的代碼是正確的。所以我給你投票。但奧蒂斯發現了這一點,所以我給了他接受的答案:) – brianLikeApple