我正在創建一個圖片賓果遊戲。我有一個收集視圖,顯示十二個圖像中的九個。我創建了一個for循環以說出九個話語,每個話語對應於集合視圖中顯示的一個圖像。問題是,所有九個話語都是一個接一個地說出來而沒有停頓。在按下上一個故事板中的播放按鈕後,話筒中的話語一個接一個地「吐出」。我需要說出一個話語,然後循環暫停,直到玩家點擊相應的圖像。然後,我需要循環恢復並講下一個話語,直到玩家獲得賓果。 didFinishSpeechUtterance沒有被調用。在模擬器中進行測試時,控制檯中沒有任何內容。我曾提到「Swift中的每個迭代循環都會立即發生言論?」和「AVSpeechSynthesizer問題,任何解決方法?」。我還提到「AVSpeechSynthesizer委託方法didStartSpeechUtterance沒有被調用」,但我仍然難倒了。AVSpeechSynthesizer didFinishSpeechUtterance沒有被調用
class FarmViewController: UIViewController, UICollectionViewDataSource,
UICollectionViewDelegate, AVSpeechSynthesizerDelegate {
var arrayOfImages = ["pig", "horse", "dog", "cow", "duck", "cat", "sheep", "chicken", "rooster", "goat", "mouse", "donkey"]
var arrayOfSpeechUtterances = ["pig", "horse", "dog", "cow", "duck", "cat", "sheep", "chicken", "rooster", "goat", "mouse", "donkey"]
var arrayOfSU = [String]()
var speechSynthesizer = AVSpeechSynthesizer()
var images = ["pig", "horse", "dog", "cow", "duck", "cat", "sheep", "chicken", "rooster", "goat", "mouse", "donkey"]
var speechUtterances = [AVSpeechUtterance(string: "pig"), AVSpeechUtterance(string: "horse"), AVSpeechUtterance(string: "dog"), AVSpeechUtterance(string: "cow"), AVSpeechUtterance(string: "duck"), AVSpeechUtterance(string: "cat"), AVSpeechUtterance(string: "sheep"), AVSpeechUtterance(string: "chicken"), AVSpeechUtterance(string: "rooster"), AVSpeechUtterance(string: "goat"), AVSpeechUtterance(string: "mouse"), AVSpeechUtterance(string: "donkey")]
var dict = [AVSpeechUtterance(string: "pig"): "pig", AVSpeechUtterance(string: "horse"): "horse", AVSpeechUtterance (string: "dog"): "dog", AVSpeechUtterance(string: "cow"): "cow", AVSpeechUtterance(string: "duck"): "duck", AVSpeechUtterance(string: "cat"): "cat", AVSpeechUtterance(string: "sheep"): "sheep", AVSpeechUtterance(string: "chicken"): "chicken", AVSpeechUtterance(string: "rooster"): "rooster", AVSpeechUtterance(string: "goat"): "goat", AVSpeechUtterance(string: "mouse"): "mouse", AVSpeechUtterance(string: "donkey"): "donkey"]
var currentName = ""
var queue = dispatch_queue_create("com.speechUtterances.serialqueue", DISPATCH_QUEUE_SERIAL)
覆蓋FUNC viewDidLoad中(){ super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
speechSynthesizer.delegate = self
self.speechSynthesizer.delegate = self
arrayOfImages.shuffle()
for image in arrayOfImages [0...8] {
arrayOfSU.append(image)
}
arrayOfSU.shuffle()
**dispatch_sync(queue) {() -> Void in
for name in self.arrayOfSU {
let speechUtterances = AVSpeechUtterance (string: name)
var beforeSpeechString = ""
var beforeSpeech = AVSpeechUtterance (string: beforeSpeechString)
self.speechSynthesizer.speakUtterance(beforeSpeech)
print("before speech")**
var currentName = AVSpeechUtterance (string: name)
**print("current name")
speechUtterance.rate = 0.50
speechUtterance.pitchMultiplier = 2.0
speechUtterance.volume = 1.0**
**self.speechSynthesizer.speakUtterance(currentName)
}
func speechSynthesizer(synthesizer: AVSpeechSynthesizer!, didFinishSpeechUtterance utterance: AVSpeechUtterance!){
print("finish")
}**
}
}
這工作的Midhun MP!然而,話語仍然在循環中沒有停頓地被說出,並且在所有九個話語被說完之後,didFinishSpeechUtterance方法被調用,而不是在每個單獨的話語之後暫停。 – Alicia