【问题标题】:AVSpeechSynthesizer didFinishSpeechUtterance not being calledAVSpeechSynthesizer didFinishSpeechUtterance 未被调用
【发布时间】:2016-08-18 18:08:28
【问题描述】:

我正在创建一个图片宾果游戏。我有一个收藏视图,其中显示了十二张图片中的九张。我创建了一个 for in 循环来说出九个话语,每个话语对应于集合视图中显示的一个图像。问题是所有九个话语都一个接一个地说出,没有停顿。在我按下前一个故事板中的播放按钮后,这些话语在控制台中一个接一个地“吐出”。我需要说出一个话语并暂停循环,直到玩家点击相应的图像。然后我需要循环恢复并说出下一个话语,直到玩家获得宾果游戏。没有调用 didFinishSpeechUtterance。在模拟器中测试时,控制台中没有任何内容。我曾提到“如何让 Swift 中的 while 循环的每次迭代立即发生语音?”和“AVSpeechSynthesizer 的问题,有什么解决方法吗?”我还提到了“没有调用 AVSpeechSynthesizer 委托方法 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)

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

【问题讨论】:

    标签: ios swift avspeechsynthesizer


    【解决方案1】:

    问题是您在 viewDidLoad 方法中编写了委托方法。你应该把它写在外面,它会起作用。如下:

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

    【讨论】:

    • 这对 Midhun MP 有效!然而,话语仍在循环中没有停顿,并且在所有九个话语都被说出之后调用 didFinishSpeechUtterance 方法,而不是在每个单独的话语之后暂停。
    【解决方案2】:

    我在苹果开发者论坛的这个帖子中找到了解决方案:link

    AVSpeechSynthesizer 实例必须是您的 ViewController 的强引用属性(即使我不太清楚为什么)。

    @interface MYViewController ()
    
    @property (strong, nonatomic) AVSpeechSynthesizer *synthesizer;
    
    @end
    
    
    @implementation MYViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        _synthesizer = [[AVSpeechSynthesizer alloc]init];
        [_synthesizer setDelegate:self];
        AVSpeechUtterance *speechutt = [AVSpeechUtterance speechUtteranceWithString:_textToSpeech];
        speechutt.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
        [_synthesizer speakUtterance:speechutt];
    }
    
    -(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
    
        // your code here
    }
    
    @end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-07
      • 2016-05-21
      • 2012-02-15
      • 2011-11-17
      • 2019-10-22
      相关资源
      最近更新 更多