2014-09-25 19 views
8

我已經在我的應用程序中實現了文本到語音轉換,並且它可以正常使用我當前使用的代碼。基本上算法創建一個文本,然後如果用戶點擊UIButton文本正在講話。如何在swift中暫停和恢復AVSpeechSynthesizer/AVSpeechUtterance?

挑戰:我想啓用相同的UIButton暫停合成,如果按鈕已經被敲擊(即文本目前正在使用),然後繼續說離開的地方,如果按鈕被竊聽再次。

我知道AVFoundation Reference中有幾個函數,但我無法正確實現它們。

有誰知道如何在Swift中做到這一點?

import UIKit 
import AVFoundation 


    @IBOutlet var generatedText: UILabel! 

@IBAction func buttonSpeakClicked(sender: UIButton){ 
    var mySpeechSynthesizer:AVSpeechSynthesizer = AVSpeechSynthesizer() 
    var mySpeechUtterance:AVSpeechUtterance = AVSpeechUtterance(string:generatedText.text) 
    mySpeechUtterance.rate = 0.075 

mySpeechSynthesizer .speakUtterance(mySpeechUtterance) 
} 
+0

你知道嗎?我也需要幫助。 – webmagnets 2015-04-08 21:01:16

+0

不用擔心,如果你知道,請告訴我 – KML 2015-04-08 21:03:46

+0

@webmagnets我目前專注於其他事項並在最後期限之內。你有沒有,你可以請測試下面的建議解決方案嗎? – KML 2015-04-10 06:58:26

回答

4

AVSpeechSynthesizer Class Reference

您是否嘗試過這些方法? - pauseSpeakingAtBoundary:- continueSpeaking

而且這些都是一些性能,(pausedspeaking),可以幫助您確定合成的狀態。

這樣的代碼應該可以工作:mySpeechSynthesizer.pauseSpeakingAtBoundary(AVSpeechBoundary.Immediate)

+0

如果任何人都可以編輯答案,理想情況下在Swift中有一個完整的例子,我會接受答案。 – KML 2015-04-16 08:43:23