AV Foundationd 学习之(一)

 初次学习这个框架感觉还是挺好玩的。目前市面上有很各种语音播放的。不过很多是通过录制语音,然后放在App里面根据自己业务的需求来播放。既然自己学这个框架,可能不能去录制本地语音,像那种要朗诵一篇文章的,那我不是要被玩死了。
 废话不多说,先来玩玩这个AVSpeedSynthesizer。这个类是干嘛?是用来播放一个或者多个的语音内容。这些语音内容都是名为AVSpeechUtterance的类的实例。如果你想播放一句"老婆,早餐做好了来吃吧!" 这样的语音提示。你可以用这个实现。(不过程序猿,大部分都是单身狗ಥ_ಥ)。
import AVFoundation

class ViewController: UIViewController {
    var synthesizer = AVSpeechSynthesizer()
    var voices =  AVSpeechSynthesisVoice.init(language: "zh-CN")
    var speechStrings = ["嗨!帅哥!"
    ,"嗨!美女!过来玩啊!","好呀!帅哥你想玩啥呀。","一款成人游戏。","帅哥好坏噢!","玩个王者荣耀啊!你想啥啊!"]

    override func viewDidLoad() {
        super.viewDidLoad()
        beginConversation() 
    }

    func beginConversation() {
        for  i in 0...self.speechStrings.count-1 {
            let utterance = AVSpeechUtterance.init(string: self.speechStrings[i])
            utterance.voice = self.voices
            utterance.rate = 0.4
            utterance.pitchMultiplier = 0.8
            utterance.postUtteranceDelay = 1.0
            self.synthesizer.speak(utterance)

        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值