Speech Synthesis(文字转语音)

将文字转换成语音.

Speech Synthesis是作为一个管理iOS、iPadOS、tvOS、watchOS的语音和语音合成框架.如果要在macOS上使用文字转语音,需要使用NSSpeechSynthesizer.

使用步骤:

  • 1.创建AVSpeechUtterance对象.(可以通过此定义一些比如说话速率之类的东西)
  • 2.将文字传递给AVSpeechSynthesizer对象来产生语音.

AVSpeechUtterance

可以理解为一句话.

AVSpeechSynthesisVoice

声音有关(语言设置).

AVSpeechSynthesizer

可以理解为讲述者.

示例

AVSpeechUtterance *speechUtterance = [[AVSpeechUtterance alloc] initWithString:@"what the fuck?"];
/// 播放语音速率(AVSpeechUtteranceMinimumSpeechRate - AVSpeechUtteranceMaximumSpeechRate)
speechUtterance.rate = AVSpeechUtteranceDefaultSpeechRate;
/// 基线音高(0.5 - 2. default 1.0)
speechUtterance.pitchMultiplier = 1.0;
/// 音量(0 - 1. default 1.0)
speechUtterance.volume = 1.0;
/// 下面两个属性是在一个`AVSpeechSynthesizer`实例的情况下,如果有多个`AVSpeechUtterance`实例在播放.每个`AVSpeechUtterance`实例播放间隔是`pre`+`post`之和.default 0
speechUtterance.preUtteranceDelay = 1.0;
speechUtterance.postUtteranceDelay = 1.0;
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-Hant-CN"];
/// 设置发音
speechUtterance.voice = voice;
AVSpeechSynthesizer *speechSynthesizer = [[AVSpeechSynthesizer alloc] init];
[speechSynthesizer speakUtterance:speechUtterance];

AVSpeechSynthesizerDelegate

@protocol AVSpeechSynthesizerDelegate <NSObject>

@optional

/// 开始语音
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance API_AVAILABLE(ios(7.0), watchos(1.0), tvos(7.0), macos(10.14));

/// 语音结束
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance API_AVAILABLE(ios(7.0), watchos(1.0), tvos(7.0), macos(10.14));

/// 语音暂停
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance API_AVAILABLE(ios(7.0), watchos(1.0), tvos(7.0), macos(10.14));

/// 语音继续
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance API_AVAILABLE(ios(7.0), watchos(1.0), tvos(7.0), macos(10.14));

/// 语音取消
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance API_AVAILABLE(ios(7.0), watchos(1.0), tvos(7.0), macos(10.14));

/// 语音将要播放范围
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance API_AVAILABLE(ios(7.0), watchos(1.0), tvos(7.0), macos(10.14));
@end

语音控制

typedef NS_ENUM(NSInteger, AVSpeechBoundary) {
    /// 立即停止
    AVSpeechBoundaryImmediate,
    /// 说完一整个单词再停止
    AVSpeechBoundaryWord
} NS_ENUM_AVAILABLE(10_14, 7_0);

/// 停止
- (BOOL)stopSpeakingAtBoundary:(AVSpeechBoundary)boundary;
/// 暂停
- (BOOL)pauseSpeakingAtBoundary:(AVSpeechBoundary)boundary;
/// 继续 	
- (BOOL)continueSpeaking;
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值