大佬教程收集整理的这篇文章主要介绍了ios – 如何在Swift中的while循环的每次迭代中立即发生语音?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
import AVFoundation var speechsynth: AVSpeechSynthesizer = AVSpeechSynthesizer() let wordsToSpeak = ["word one","word two","word three","word four"] let endTime = NSDate().dateByAddingTimeInterval(10) while endTime.timeIntervalSinceNow > 0 { //workaround for iOS8 Bug var beforeSpeechString : String = " " var beforeSpeech:AVSpeechUtterance = AVSpeechUtterance(String: beforeSpeechString) speechsynth.speakUtterance(beforeSpeech) //realString to speak var speechString: String = wordsToSpeak[0] var nextSpeech:AVSpeechUtterance = AVSpeechUtterance(String: speechString) nextSpeech.voice = AVSpeechSynthesisVoice(language: "en-US") nextSpeech.rate = AVSpeechUtteranceMinimumSpeechRate speechsynth.speakUtterance(nextSpeech) }
目前,讲话在while循环完成后开始.
如何在每次迭代过程中说出并完成说话,然后再进行循环的下一次迭代?
import UIKit import AVFoundation class ViewController: UIViewController,AVSpeechSynthesizerDelegate { var queue: dispatch_queue_t = dispatch_queue_create( "com.test.whatever.queue",DISPATCH_QUEUE_seriaL) var index: Int = 0 let words: [String] = ["word one","word four"] var speechsynth: AVSpeechSynthesizer = AVSpeechSynthesizer() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view,typically from a nib. speechsynth.delegate = self self.speechCurrentWord() } func speechCurrentWord(){ dispatch_async(queue,{ () -> Void in var beforeSpeechString : String = " " var beforeSpeech:AVSpeechUtterance = AVSpeechUtterance(String: beforeSpeechString) self.speechsynth.speakUtterance(beforeSpeech) var nextSpeech:AVSpeechUtterance = AVSpeechUtterance(String: self.words[self.index]) nextSpeech.voice = AVSpeechSynthesisVoice(language: "en-US") nextSpeech.rate = AVSpeechUtteranceMinimumSpeechRate self.speechsynth.speakUtterance(nextSpeech) }) } func speechSynthesizer(synthesizer: AVSpeechSynthesizer!,didFinishSpeechUtterance utterance: AVSpeechUtterance!) { index++ if index < self.words.count { self.speechCurrentWord() } } }
以上是大佬教程为你收集整理的ios – 如何在Swift中的while循环的每次迭代中立即发生语音?全部内容,希望文章能够帮你解决ios – 如何在Swift中的while循环的每次迭代中立即发生语音?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。