2016-11-17 27 views
2

我使用AVFoundation語音合成器,創建這樣的聲音的一個實例:語音合成在iOS上裝載奇怪的錯誤,並沒有併發

import AVFoundation 

class CanSpeak { 

    let voices = AVSpeechSynthesisVoice.speechVoices() 
    let voiceSynth = AVSpeechSynthesizer() 
    var voiceToUse: AVSpeechSynthesisVoice? 

    init(){ 
     for voice in voices { 
      if voice.name == "Arthur" 
      { 
       voiceToUse = voice 
      } 
     } 
    } 

    func sayThis(_ phrase: String){ 
     let utterance = AVSpeechUtterance(string: phrase) 
     utterance.voice = voiceToUse 
     utterance.rate = 0.5 
     voiceSynth.speak(utterance) 
    } 
} 

我有兩個問題。

  1. 沒有併發性。多次調用此函數會導致字符串的排隊。我不想那樣。如果有多次打電話給這個功能,我想要立即開始說話,即使這意味着自己說話。我如何做到這一點?

2 - 一些奇怪的錯誤,我不上裝載明白:

2016-11-18 03:03:07.103349 mySKtest[687:87489] 0x17415ee50 Copy matching assets reply: XPC_TYPE_DICTIONARY { count = 2, transaction: 0, voucher = 0x0, contents = "Assets" => : { length = 3620 bytes, contents = 0x62706c6973743030d4000100020003000400050006012d01... } "Result" => : 0 } 2016-11-18 03:03:07.109254 mySKtest[687:87489] 0x17015e610 Copy assets attributes reply: XPC_TYPE_DICTIONARY { count = 1, transaction: 0, voucher = 0x0, contents = "Result" => : 1 } 2016-11-18 03:03:07.109547 mySKtest[687:87489] [MobileAssetError:1] Unable to copy asset attributes 2016-11-18 03:03:07.110080 mySKtest[687:87489] Could not get attribute 'LocalURL': Error Domain=MobileAssetError Code=1 "Unable to copy asset attributes" UserInfo={NSDescription=Unable to copy asset attributes} 2016-11-18 03:03:07.112341 mySKtest[687:87489] 0x17015e610 Copy assets attributes reply: XPC_TYPE_DICTIONARY { count = 1, transaction: 0, voucher = 0x0, contents = "Result" => : 1 } 2016-11-18 03:03:07.112416 mySKtest[687:87489] [MobileAssetError:1] Unable to copy asset attributes 2016-11-18 03:03:07.112523 mySKtest[687:87489] Could not get attribute 'LocalURL': Error Domain=MobileAssetError Code=1 "Unable to copy asset attributes" UserInfo={NSDescription=Unable to copy asset attributes} 2016-11-18 03:03:07.145658 mySKtest[687:87489] 0x174341c30 Copy matching assets reply: XPC_TYPE_DICTIONARY { count = 2, transaction: 0, voucher = 0x0, contents = "Assets" => : { length = 4198 bytes, contents = 0x62706c6973743030d4000100020003000400050006016f01... } "Result" => : 0 } 2016-11-18 03:03:07.148403 mySKtest[687:87489] 0x17015e610 Copy assets attributes reply: XPC_TYPE_DICTIONARY { count = 3, transaction: 0, voucher = 0x0, contents = "Attributes" => : { length = 526 bytes, contents = 0x62706c6973743030d4010203040506232458247665727369... } "Result" => : 0 "SandboxExtension" => { length = 269, contents = "b72954a376beb759be03a6411c3e2649f9845fd1;00000000;00000000;0000000000000015;com.apple.assets.read;00000001;01000003;00000000000ca4fc;/private/var/MobileAsset/Assets/com_apple_MobileAsset_VoiceServices_CustomVoice/54ffb86ce0ecd2c5bf871303b5690d327a571428.asset/AssetData" } } 2016-11-18 03:03:07.149858 mySKtest[687:87489] 0x1743414a0 Copy assets attributes reply: XPC_TYPE_DICTIONARY { count = 2, transaction: 0, voucher = 0x0, contents = "Attributes" => : { length = 526 bytes, contents = 0x62706c6973743030d4010203040506232458247665727369... } "Result" => : 0 }

回答

1

至於#1,它可能不會發生的。語音合成器是一個系統共享資源,因此係統如何處理調度多個請求不在我們的控制範圍之內,因爲它是API的客戶端。 (請注意,如果您重複使用相同的合成器,它會排隊多餘的話語,但是如果您創建多個合成器,則無法說出另一個合成器發聲時請求的話語。)

不知道關於#2,抱歉。看起來像診斷文本,不一定是錯誤。可能價值filing a bug,因爲他們可能不希望在沒有實際問題時進行日誌診斷。

獎金回答:您可以使用函數編程,使聲音有點短的選擇:

let voice = AVSpeechSynthesisVoice.speechVoices().first(where: { $0.name == "Arthur" }) 
+0

我真的需要有點了解過濾器,地圖,關閉和...好,編程,真的。這是一條更好的路線。 – Confused

+0

我很貪心:http://stackoverflow.com/questions/40665160/filter-closure-functional-syntax-version-of-for-loop-with-multiple-conditions我想做更多的;) – Confused

+0

我有一個懷疑這個語音合成器的加載錯誤會導致在加載時任何和所有的音頻播放出現可怕的結局。我已經消除了其他一切......這可能嗎? – Confused