iOS 如何使用 Nuance Vocalizer Speechkit 2 检测语音结束

How to detect end of speech with Nuance Vocalizer, Speechkit 2, for iOS

我在 swift、iOS 9.3 中使用 Nuance 的 Speechkit 2。 根据此文档,需要检测 TTS 何时完成:

https://developer.nuance.com/public/Help/DragonMobileSDKReference_iOS/Speech-synthesis.html

...这些是可用于 TTS 的 3 种委托方法:

// SKTransactionDelegate
func transaction(transaction: SKTransaction!, didReceiveAudio audio: SKAudio!) { ... }
func transaction(transaction: SKTransaction!, didFinishWithSuggestion suggestion: String!) { ... }
func transaction(transaction: SKTransaction!, didFailWithError error: NSError!, suggestion: String!) { ... }

此外,这是一个示例 Swift 项目 (https://developer.nuance.com/public/index.php?task=prodDev),其中 TTS 示例使用了一个额外的 SKAudioPlayerDelegate,在代码中我看到了这个委托方法,但它永远不会触发:

func audioPlayer(player: SKAudioPlayer!, willBeginPlaying audio: SKAudio!) {
        log("willBeginPlaying")
        // The TTS Audio will begin playing.
    }

我用以下方式调用 TTS:

var skTransaction = skSession!.speakString("a long sentence here",withLanguage: "eng-USA",delegate: self)

然而,"didFinishWithSuggestion" 总是在 TTS 语音结束之前很久就触发,这对我来说似乎是一个错误。 Nuance 技术支持不会回复,有人可以帮助我吗?谢谢。

我找到了解决方案。事实证明,第二个代表负责检测 TTS 播放的开始和结束:

class ViewController: UIViewController, SKAudioPlayerDelegate{
    //All your view controller code here...

    func viewDidLoad(){
       //more init. code 
       skSession!.audioPlayer.delegate=self
   }
}

这些是委托方法:

func audioPlayer(player: SKAudioPlayer!, willBeginPlaying audio: SKAudio!){
  print("Starting TTS")
}
func audioPlayer(player: SKAudioPlayer!, didFinishPlaying audio: SKAudio!) {
   print("TTS FINISHED")
}