【www.bbyears.com--同学】
swift开发之文字内容转语音,只是将网上oc版本转化为swift版本而已,有需要的同学拿去。
xcode7
代码如下swift2.0
import UIKit
import AVFoundation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
}catch let error as NSError{
print(error.code)
}
let av = AVSpeechSynthesizer.init()
let utterance1 = AVSpeechUtterance.init(string: "合肥论坛是合肥热线网站开辟的
门户社区,同时也是安徽人气第一的生活社区。合肥热线由合肥肥肥网络科技有限公司始创于2003年,
是安徽第一的城市门户网站。合肥论坛历经10年的运作,目前已成为省内论坛规模最大、用户数量最多、
口碑最好、社区服务最完善的综合社区。论坛拥有近230万的正式注册会员,日浏览量超过400万次
,同时在线人数超过70000人。2006年,合肥论坛已经成为中国百强网络社区之一。")
utterance1.postUtteranceDelay = 0.5
utterance1.rate = 0.1
av.speakUtterance(utterance1)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}