美文网首页iOS菜鸟级开发
AVSpeechSynthesizer (语音播报)

AVSpeechSynthesizer (语音播报)

作者: 一生信仰 | 来源:发表于2018-04-25 16:20 被阅读1次

    标签: AVFoundation


    AVSpeechSynthesizer

    An object that produces synthesized speech from text utterances and provides controls for monitoring or controlling ongoing speech

    AV Foundation中提供了一个AVSpeechSynthesizer类来向app中添加朗诵文本的功能.
    这个类用来播放一个或者多个语音内容,每条语音内容都是一个AVSpeechUtterance对象.
    (一个语音合成器(SpeechSynthesizer)播放需要一段文本内容(SpeechUtterance))

    AVSpeechSynthesizer 常用函数

    var speech : AVSpeechSynthesizer = AVSpeechSynthesizer.init()
    speech.delegate = self
    
    //播放
    speech.speak(utterance)
    
    //暂停(在某个位置暂停)
    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance) {
            //代理获取当前语音播放位置
            range = characterRange
    }
    speech.pauseSpeaking(at: AVSpeechBoundary.init(rawValue: range.location)! )
    
    //继续播放
    speech.continueSpeaking()
    
    //停止播放、取消播放
     speech.stopSpeaking(at: AVSpeechBoundary.immediate)
    
    //判断当前播放状态
    speech.isPaused
    speech.isSpeaking
    

    AVSpeechSynthesizerDelegate

       //开始播放
       func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didStart utterance: AVSpeechUtterance) {
        }
        
        //取消播放、停止播放
        func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didCancel utterance: AVSpeechUtterance) {
           
        }
        
        //暂停播放
        func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didPause utterance: AVSpeechUtterance) {
        }
        
        //继续播放
        func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didContinue utterance: AVSpeechUtterance) {
        }
        
        //结束播放
        func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
        }
        
        //播放轮询,获取当前播放位置
        func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance) {
    
        }
    

    AVSpeechUtterance(语音内容对象)

    //根据文本初始化
    let utterance = AVSpeechUtterance.init(string: "你好,我是语音播放器")
    //播放速率(// 数值在 AVSpeechUtteranceMinimumSpeechRate 和 AVSpeechUtteranceMaximumSpeechRate 区间)
    utterance.rate = 0.4
    //播放语言
    utterance.voice = AVSpeechSynthesisVoice.init(language: "zh-CN")
    //播放前的过渡时间
    utterance.preUtteranceDelay = 0.5
    //播放结束的过渡时间
    utterance.postUtteranceDelay = 0.5
    

    AVSpeechSynthesisVoice(语音语言)

    AVSpeechSynthesisVoice.init(language: "zh-CN")
    
    • Arabic (ar-SA)
    • Chinese (zh-CN, zh-HK, zh-TW)
    • Czech (cs-CZ)
    • Danish (da-DK)
    • Dutch (nl-BE, nl-NL)
    • English (en-AU, en-GB, en-IE, en-US, en-ZA)
    • Finnish (fi-FI)
    • French (fr-CA, fr-FR)
    • German (de-DE)
    • Greek (el-GR)
    • Hebrew (he-IL)
    • Hindi (hi-IN)
    • Hungarian (hu-HU)
    • Indonesian (id-ID)
    • Italian (it-IT)
    • Japanese (ja-JP)
    • Korean (ko-KR)
    • Norwegian (no-NO)
    • Polish (pl-PL)
    • Portuguese (pt-BR, pt-PT)
    • Romanian (ro-RO)
    • Russian (ru-RU)
    • Slovak (sk-SK)
    • Spanish (es-ES, es-MX)
    • Swedish (sv-SE)
    • Thai (th-TH)
    • Turkish (tr-TR)

    参考链接:AVSpeechSynthesizer

    相关文章

      网友评论

        本文标题:AVSpeechSynthesizer (语音播报)

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