我正在创建一个图片宾果游戏。我有一个 Collection View ,其中显示了十二张图片中的九张。我创建了一个 for in 循环来说出九个话语,每个话语对应于 Collection View 中显示的一张图像。问题是九个字都一个接一个的说,没有停顿。在我按下前一个 Storyboard中的播放按钮后,这些话语会在控制台中一个接一个地“吐出”。我需要说出一句话并暂停循环,直到玩家点击相应的图像。然后我需要循环恢复并说出下一句话,直到玩家获得宾果游戏。未调用 didFinishSpeechUtterance。在模拟器中测试时,控制台中没有任何内容。我已经提到“如何在 Swift 中的 while 循环的每次迭代中立即发出语音?”和“AVSpeechSynthesizer 的问题,有什么解决方法吗?”我还提到了“未调用 AVSpeechSynthesizer 委托(delegate)方法 didStartSpeechUtterance”,但我仍然感到困惑。
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)
覆盖函数 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")
}**
}
}
最佳答案
问题是您在 viewDidLoad 方法中编写了委托(delegate)方法。你应该把它写在外面,它会起作用。如下所示:
override func viewDidLoad()
{
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = 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")
}
关于ios - AVSpeechSynthesizer didFinishSpeechUtterance 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39024835/