2016-08-18 1 views
-1

Je crée un jeu de bingo photo. J'ai une vue de collection avec neuf images sur douze affichées. J'ai créé une boucle in pour parler neuf énoncés, chaque énoncé correspondant à une image affichée dans la vue de collection. Le problème est que les neuf énoncés sont prononcés les uns après les autres sans pause. Les énoncés sont "crachés" l'un après l'autre dans la console immédiatement après avoir appuyé sur le bouton de lecture dans le storyboard précédent. J'ai besoin d'un énoncé pour être prononcé et de la boucle pour faire une pause jusqu'à ce que le joueur appuie sur l'image correspondante. Ensuite, j'ai besoin de la boucle pour reprendre et parler l'énoncé suivant jusqu'à ce que le joueur obtienne le bingo. Le didFinishSpeechUtterance n'est pas appelé. Il n'y a rien dans la console lors du test dans le simulateur. Je me suis référé à "Comment puis-je faire apparaître la parole immédiatement pour chaque itération d'une boucle while dans Swift?", Et "Un problème avec AVSpeechSynthesizer, Any solutions de contournement?" Je me suis également référé à "méthode de délégué de AVSpeechSynthesizer didStartSpeechUtterance ne s'appelant pas", mais je suis toujours perplexe.AVSpeechSynthesizer didFinishSpeechUtterance n'est pas appelé

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) 

override 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") 
    }** 
} 
} 

Répondre

0

La question est que vous avez écrit la méthode déléguée dans la méthode viewDidLoad. Vous devriez l'écrire dehors et cela fonctionnera. Comme ci-dessous:

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") 
} 
+0

Cela a travaillé Midhun MP! Cependant, les énoncés sont toujours prononcés sans interruption dans la boucle et la méthode didFinishSpeechUtterance est appelée après que les neuf énoncés aient été prononcés au lieu de s'arrêter après chaque énoncé individuel. – Alicia