【AVFoundation】AVSpeechSynthesizer文字转语音播放

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第1天,点击查看活动详情

AVFoundation 是Apple iOS和OS X系统中用于处理基于时间的媒体数据的高级框架,通过开发所需的工具提供了强大的功能集,让开发者能够基于苹果平台创建当下最先进的媒体应用程序,其针对64位处理器设计,充分利用了多核硬件优势,会自动提供硬件加速操作,确保大部分设备能以最佳性能运行,是iOS开发接触音视频开发必学的框架之一

参与掘金日新计划,持续记录AVFoundation学习,Demo学习地址

AVSpeechSynthesizer

  • 可以轻松实现文字转语音功能

例如播放"Hello AV Foundation"

let synthesizer: AVSpeechSynthesizer = AVSpeechSynthesizer()
// 创建播放话语
let utterance = AVSpeechUtterance(string: "Hello AV Foundation")
// 播放
speechSynthesizer.speak(utterance)
复制代码

AVSpeechUtterance其他相关设置

  • 如果你想换个声音,设置utterance.voice
  • AVSpeechSynthesisVoice可以用语言编码创建,也可以用标识符创建
utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")  // 英式英语
utterance.voice = AVSpeechSynthesisVoice(identifier: "com.apple.ttsbundle.Daniel-compact")
复制代码
  • 查看AVSpeechSynthesisVoice实例的相关属性,以下属性都是只读
// 标识符
CQLog(utterance.voice?.identifier)
// 语音
CQLog(utterance.voice?.language)
// 性别,返回枚举0-未知,1-男性,2女性
CQLog(utterance.voice?.gender)
// 姓名
CQLog(utterance.voice?.name)
// 声音质量, 1默认质量,2增强质量
CQLog(utterance.voice?.quality)
复制代码
  • 查看用户当前选择的语言编码
CQLog(AVSpeechSynthesisVoice.currentLanguageCode())
复制代码
  • 查看所有的语音播放声音
CQLog(AVSpeechSynthesisVoice.speechVoices())
复制代码
  • 调整话语播放速率,范围0-1
  • AVSpeechUtteranceMaximumSpeechRate最大速率
  • AVSpeechUtteranceMinimumSpeechRate最小速率
  • AVSpeechUtteranceDefaultSpeechRate默认速率
utterance.rate = AVSpeechUtteranceMinimumSpeechRate
utterance.rate = 0.4
复制代码
  • 调整话语音调,范围0.5(低音调) - 2.0(高音调)
utterance.pitchMultiplier = 0.8
复制代码
  • 播放下一句前暂停,可以让语言看起来不那么不自然
// 播放下一句前暂停0.1秒
utterance.postUtteranceDelay = 0.1

复制代码
  • 上一句播放完暂停,
utterance.preUtteranceDelay = 0.1
复制代码
  • 调整话语音量 范围0-1
utterance.volume = 1
复制代码
  • 查看当前话语内容
CQLog(utterance.speechString)
复制代码

AVSpeechSynthesizer其他用法

  • write 依次回调buffer,会触发代理函数,不可和speak同时调用
// 读取播放时的AVAudioBuffer write和speak不可同时调用
synthesizer.write(utterance) { audioBuffer in
    CQLog(audioBuffer)
}
复制代码
  • 暂停播放
// 暂停播放,触发暂停代理回调,可继续播放
synthesizer.pauseSpeaking(at: .immediate)
复制代码
  • 继续播放
synthesizer.continueSpeaking()
复制代码
  • 停止播放
// 停止播放,触发播放完成代理回调,immediatel立即停止,word播放完当前段停止
synthesizer.stopSpeaking(at: .word)
复制代码

AVSpeechSynthesizer代理

    // 开始播放回调
    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didStart utterance: AVSpeechUtterance) {
        CQLog(utterance.speechString + "开始播放")
    }

    // 播放完成回调
    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
        CQLog(utterance.speechString + "播放完成")
    }

    // 暂停播放回调
    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didPause utterance: AVSpeechUtterance) {
        CQLog(utterance.speechString + "暂停播放")
    }

    // 继续播放回调
    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didContinue utterance: AVSpeechUtterance) {
        CQLog(utterance.speechString + "继续播放")
    }

    // 取消播放回调
    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didCancel utterance: AVSpeechUtterance) {
        CQLog(utterance.speechString + "取消播放")
    }

    // 实时播放位置回调
    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance) {
        CQLog(utterance.speechString + "播放位置\(characterRange.location)")
    }
复制代码

猜你喜欢

转载自juejin.im/post/7082322297133989901
今日推荐