2017-02-03 65 views
2

我有一段代碼無法工作,但也沒有給我一個運行時錯誤。發言者沒有發言。快速文本到語音

let synth = AVSpeechSynthesizer() 
var myUtterance = AVSpeechUtterance(string: audioTextField.text) 
myUtterance.rate = 0.3 
synth.speak(myUtterance) 

是否有任何代碼我錯過了或是其他東西?幫助將不勝感激。

編輯:這不是在任何@IBActions工作,而是工作在視圖中細做負載功能....

override func viewDidLoad() { 
    super.viewDidLoad() 
    speechRecognizer?.delegate = self 
    timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(tick), userInfo: nil, repeats: true) 
    tick() 
    requestSpeechAuth() 
//WORKS HERE 
} 

@IBAction func audioButtonPressed(_ sender: Any) { 
//DOESN"T WORK HERE  
    if isRecording { 
     stopRecording() 
    } else { 
     startRecording() 
    } 
} 

回答

7

此代碼工作(從蘋果文檔)

let string = "Hello, World!" 
let utterance = AVSpeechUtterance(string: string) 
utterance.voice = AVSpeechSynthesisVoice(language: "en-US") 

let synth = AVSpeechSynthesizer() 
synth.speak(utterance) 

記得導入AVFoundation

import AVFoundation 
+0

它支持ssml嗎? –

+0

我的意思是ssml到語音(而不是文本到語音) –