2015-10-04 122 views
4

我正在處理iOS文本到語音應用程序,並嘗試添加一個選項以使用Alex語音,這是iOS 9的新增功能。我需要確定用戶是否已使用設置下載了Alex語音 - >可訪問性。我似乎無法找到如何做到這一點。如何檢測iOS設備是否下載了語音文件?

if ([AVSpeechSynthesisVoice voiceWithIdentifier:AVSpeechSynthesisVoiceIdentifierAlex] == "Not Found") { 
    // Do something... 
} 

原因是其他語言的聲音是標準的,以某種速度播放,不同於Alex的聲音。所以我有一個工作的應用程序,但如果用戶沒有下載語音,iOS會自動默認爲基本語音,但播放速度不正確。如果我能檢測到語音尚未下載,我可以彌補差異和/或提醒用戶。

+0

一個有趣而且很好格式化的問題+1 – Cesare

回答

3

好的,所以我想我是在過度這個想法,並認爲它更復雜。解決方案很簡單。

if (![AVSpeechSynthesisVoice voiceWithIdentifier:AVSpeechSynthesisVoiceIdentifierAlex]) { 
     // Normalize the speech rate since the user hasn't downloaded the voice and/or trigger a notification that they need to go into settings and download the voice. 
    } 

感謝大家誰看着這個和@CeceXX的編輯。希望這可以幫助別人。

+0

不幸的是,'[AVSpeechSynthesisVoice voiceWithIdentifier:]'方法似乎從iOS 9.1+中缺失。用不太有用的'voiceWithLanguage'代替。 – axello

0

下面介紹一種方法。讓我們與亞歷克斯堅持爲例:

- (void)checkForAlex { 

     // is Alex installed? 
     BOOL alexInstalled = NO; 
     NSArray *voices = [AVSpeechSynthesisVoice speechVoices]; 

     for (id voiceName in voices) { 

      if ([[voiceName valueForKey:@"name"] isEqualToString:@"Alex"]) { 
       alexInstalled = YES; 
      } 
     } 

     // react accordingly 
     if (alexInstalled) { 
      NSLog(@"Alex is installed on this device."); 
     } else { 
      NSLog(@"Alex is not installed on this device."); 
     } 
    } 

此方法遍歷所有已安裝的聲音和查詢每個聲音的名字。如果亞歷克斯在他們之中,他就安裝好了。

您可以查詢的其他值是「語言」(返回語言代碼,如en-US)和質量(1 =標準,2 =增強)。

相關問題