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 

@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;

你可能感兴趣的:(音视频)