欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

IOS Swift 5.0 文字转语音

程序员文章站 2022-04-11 16:25:57
...

一、项目创建

二、效果展示

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("阅读过程中")
    }


}

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