IOS Swift 5.0 文字转语音

一、项目创建

二、效果展示

三、代码实现

import UIKit
import AVFoundation
class TextTurnVoiceController: UIViewController{

    let syntesizer = AVSpeechSynthesizer()
    var utterance = AVSpeechUtterance()

    //开始播放
    @IBAction func btnPlay(_ sender: UIButton) {
        //先停止当前播放的
        stopPlay()
        if text.text == ""{
            play(text: "请输入内容")
        }else{
            play(text: text.text!)
        }
    }
    //暂停
    @IBAction func btnSuspended(_ sender: UIButton) {
        pause()
    }
    //继续
    @IBAction func btnContinue(_ sender: UIButton) {
        continuePlay()
    }
    //停止
    @IBAction func btnStop(_ sender: UIButton) {
        stopPlay()
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    //转换播放语音
    func play(text:String) {
        // 设置将要语音的文字
        utterance = AVSpeechUtterance(string: text)
        //设置语言,这里是中文
        //utterance.voice = AVSpeechSynthesisVoice.init(language: "zh_CN")
        // 语音的速度
        utterance.rate = 0.1
        // 开始播放
        syntesizer.speak(utterance)
    }
    
    // 暂停-点击暂停能够接着继续播放
    func pause() {
        syntesizer.pauseSpeaking(at: .immediate)
    }
    
    //继续播放
    func continuePlay() {
        syntesizer.continueSpeaking()
    }
    
    //停止播放-点击了停止播放就不能够接着继续播放了
    func stopPlay() {
        syntesizer.stopSpeaking(at: .immediate)
    }
}


参考其他的类似文章时,很多都提到了  AVSpeechSynthesizerDelegate 代理,通过实现代理监听播放状态,但是在 Swift 5 中我试了一下,发现没有效果,代码如下:

import UIKit
import AVFoundation
class TextTurnVoiceController: UIViewController, AVSpeechSynthesizerDelegate{
    
    let syntesizer = AVSpeechSynthesizer()
    var utterance = AVSpeechUtterance()
    
    @IBOutlet weak var text: UITextField!

    //开始播放
    @IBAction func btnPlay(_ sender: UIButton) {
        //先停止当前播放的
        stopPlay()
        if text.text == ""{
            play(text: "请输入内容")
        }else{
            play(text: text.text!)
        }
    }
    //暂停
    @IBAction func btnSuspended(_ sender: UIButton) {
        pause()
    }
    //继续
    @IBAction func btnContinue(_ sender: UIButton) {
        continuePlay()
    }
    //停止
    @IBAction func btnStop(_ sender: UIButton) {
        stopPlay()
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    //转换播放语音
    func play(text:String) {
        // 设置将要语音的文字
        utterance = AVSpeechUtterance(string: text)
        //设置语言,这里是中文
        //utterance.voice = AVSpeechSynthesisVoice.init(language: "zh_CN")
        // 语音的速度
        utterance.rate = 0.1
        // 开始播放
        syntesizer.speak(utterance)
    }
    
    // 暂停-点击暂停能够接着继续播放
    func pause() {
        syntesizer.pauseSpeaking(at: .immediate)
    }
    
    //继续播放
    func continuePlay() {
        syntesizer.continueSpeaking()
    }
    
    //停止播放-点击了停止播放就不能够接着继续播放了
    func stopPlay() {
        syntesizer.stopSpeaking(at: .immediate)
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
    
    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didStart utterance: AVSpeechUtterance) {
        print("开始")
    }
    
    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
         print("完成")
    }
    
    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didPause utterance: AVSpeechUtterance) {
         print("暂停")
    }
    
    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didContinue utterance: AVSpeechUtterance) {
         print("继续")
    }
    
    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didCancel utterance: AVSpeechUtterance) {
        print("停止")
    }
    
    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance) {
        print("阅读过程中")
    }


}

我不知道是否是我代码的问题,希望有了解的大佬给我留言

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值