美文网首页
Swift-系统读文字

Swift-系统读文字

作者: waytheway | 来源:发表于2023-04-18 16:18 被阅读0次

    func playAudio(audioString:String){

        do {
            
            let se = AVAudioSession.sharedInstance()
            try se.setActive(true, options: .notifyOthersOnDeactivation)
            try se.setCategory(.playback, options: .allowAirPlay)
            
        } catch  {
            
        }
    
        
        speaker = AVSpeechSynthesizer.init()
        let detailTerance = AVSpeechUtterance.init(string: audioString)
        ///语速
        detailTerance.rate = 0.5
        detailTerance.pitchMultiplier = 1//音高
        detailTerance.volume = 1
        
        detailTerance.preUtteranceDelay = 1
        
        detailTerance.postUtteranceDelay = 1
        
        
        let voice = AVSpeechSynthesisVoice.init(language: "zh-CN")
        
        
        detailTerance.voice = voice
        
        
        speaker.speak(detailTerance)
        
        
    }

    相关文章

      网友评论

          本文标题:Swift-系统读文字

          本文链接:https://www.haomeiwen.com/subject/zsahjdtx.html