• 语音播报-文字转系统声音


    一段文字,让系统用语音播出,使用AVFoundation框架下的AVSpeechSynthesizer即可,非常简单。

    步骤1,导入AVFoundation框架:

    1 import AVFoundation

    步骤2,创建语音合成器:

    1     /// 语音合成器
    2     private lazy var speechSynthesizer:AVSpeechSynthesizer = {
    3         let speech = AVSpeechSynthesizer()
    4         return speech
    5     }()

    步骤3,播放语音:

     1     /// 语音播报方法
     2     ///
     3     /// - Parameter text: 文本内容
     4     private func playVoiceWithText(text:String){
     5         
     6         // 停止之前没有播放完的声音(防止:语音过长,上次没有播放完)
     7         // immediate:立刻
     8         // word:播放完上次,再播放这次
     9         speechSynthesizer.stopSpeaking(at: .immediate)
    10         
    11         // 实例化语言 - 默认朗读英文
    12         let speechUtterance = AVSpeechUtterance(string: text)
    13         
    14         // 指定播放语言
    15         let voice = AVSpeechSynthesisVoice(language: "zh-CN")
    16         speechUtterance.voice = voice
    17         
    18         // 播报
    19         speechSynthesizer.speak(speechUtterance)
    20     }
  • 相关阅读:
    HDOJ.1263
    另一种跳转actvity方式
    [转]Android中用Java获取时间实例
    获得当前时间的方法
    3d动画切换
    登录跳转效果
    activity 成popupwindow效果
    自定义preference的使用等等
    edittext输入框的背景效果
    自定义ListPreference
  • 原文地址:https://www.cnblogs.com/panda1024/p/6241234.html
Copyright © 2020-2023  润新知