-
文字阅读例子
AVSpeechSynthesizer * synthesizer = [[AVSpeechSynthesizer alloc] init];//语音合成器 AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:@"string1"];//说话 utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];//发音 utterance.rate = 0.5f;//速率 utterance.pitchMultiplier = 0.8f;//音调 utterance.postUtteranceDelay = 0.1f;//延迟 [synthesizer speakUtterance:utterance];//添加到语音合成器,不会等待,程序继续执行 此时语音开始合成 AVSpeechUtterance *utterance2 = [[AVSpeechUtterance alloc] initWithString:@"string2"]; utterance2.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-GB"]; utterance2.rate = 0.5f; utterance2.pitchMultiplier = 0.8f; utterance2.postUtteranceDelay = 0.1f; [synthesizer speakUtterance:utterance2];//此时会等第一句结束后才会播放这句。程序不会阻塞。
-
发音(AVSpeechSynthesisVoice)
@interface AVSpeechSynthesisVoice : NSObject<NSSecureCoding> + (NSArray<AVSpeechSynthesisVoice *> *)speechVoices;//系统支持的列表 + (NSString *)currentLanguageCode;//系统当前语言代码 + (nullable AVSpeechSynthesisVoice *)voiceWithLanguage:(nullable NSString *)languageCode;//通过语言代码创建实例 使用BCP-47语言标记 + (nullable AVSpeechSynthesisVoice *)voiceWithIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(9_0);//通过identifier创建实例 @property(nonatomic, readonly) NSString *language;//语言 @property(nonatomic, readonly) NSString *identifier NS_AVAILABLE_IOS(9_0); @property(nonatomic, readonly) NSString *name NS_AVAILABLE_IOS(9_0); @property(nonatomic, readonly) AVSpeechSynthesisVoiceQuality quality NS_AVAILABLE_IOS(9_0); @end
-
语句(AVSpeechUtterance)
@interface AVSpeechUtterance : NSObject<NSCopying, NSSecureCoding>//语句 + (instancetype)speechUtteranceWithString:(NSString *)string; + (instancetype)speechUtteranceWithAttributedString:(NSAttributedString *)string API_AVAILABLE(ios(10.0), watchos(3.0), tvos(10.0)); - (instancetype)initWithString:(NSString *)string; - (instancetype)initWithAttributedString:(NSAttributedString *)string API_AVAILABLE(ios(10.0), watchos(3.0), tvos(10.0)); @property(nonatomic, retain, nullable) AVSpeechSynthesisVoice *voice;//发音 @property(nonatomic, readonly) NSString *speechString;//阅读的文字 @property(nonatomic, readonly) NSAttributedString *attributedSpeechString API_AVAILABLE(ios(10.0), watchos(3.0), tvos(10.0));//阅读的属性字符串 @property(nonatomic) float rate; // 速率 AVSpeechUtteranceMinimumSpeechRate and AVSpeechUtteranceMaximumSpeechRate. @property(nonatomic) float pitchMultiplier; // [0.5 - 2] Default = 1 音高 @property(nonatomic) float volume; // [0-1] Default = 1 音量 @property(nonatomic) NSTimeInterval preUtteranceDelay; // Default is 0.0 提前等待 @property(nonatomic) NSTimeInterval postUtteranceDelay; // Default is 0.0 之后等待 @end
-
语音合成器(AVSpeechSynthesizer)
@interface AVSpeechSynthesizer : NSObject @property(nonatomic, weak, nullable) id<AVSpeechSynthesizerDelegate> delegate; @property(nonatomic, readonly, getter=isSpeaking) BOOL speaking;//状态 是否在阅读 @property(nonatomic, readonly, getter=isPaused) BOOL paused;//状态 是否暂停 - (void)speakUtterance:(AVSpeechUtterance *)utterance; //utterance默认进入队列。加入同一个在队里的utterace会异常 - (BOOL)stopSpeakingAtBoundary:(AVSpeechBoundary)boundary;//停止并清空阅读队列。YES成功 - (BOOL)pauseSpeakingAtBoundary:(AVSpeechBoundary)boundary;//暂停阅读。AVSpeechBoundaryImmediate 立即停止,AVSpeechBoundaryWord 按单词停止(把最后一个词读完) - (BOOL)continueSpeaking;//继续 @property(nonatomic, retain, nullable) NSArray<AVAudioSessionChannelDescription *> *outputChannels API_AVAILABLE(ios(10.0), watchos(3.0), tvos(10.0));//指定输出频道 用于精确调整比如外放或是耳机。默认nil系统处理 @end
-
代理(AVSpeechSynthesizerDelegate)
@protocol AVSpeechSynthesizerDelegate <NSObject> @optional - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance;//已经开始 - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance;//已经完成 - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance;//已经停止 - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance;//已经继续 - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance;//已经取消 - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance;//将要开始读某一段文字 @end
总结下来能够实现,完整句子的阅读。能够为每一句单独指定速度、音调、音量、音高、发音、前后等待或延迟时间??梢园凑盏ゴ释V够蚴橇⒓赐V?。将要读取某一段(无法指定大小)或是已经开始或是已经完成,或是已经停止已经继续或是已经取消都会有回调方法。无法导出音频文件。
iOS 语音合成
最后编辑于 :
?著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前言 最先接触编程的知识是在大学里面,大学里面学了一些基础的知识,c语言,java语言,单片机的汇编语言等;大学毕...