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("閱讀過程中")
    }


}

我不知道是否是我代碼的問題,希望有了解的大佬給我留言

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章