蘋果原生文字轉語音播報
阿新 • • 發佈:2018-12-08
speech idc all prop 文字轉語音 default 配置 The class
1、CHiOSSpeech.h
// // 文 件 名:CHiOSSpeech.h // // 版權所有:Copyright ? 2018年 leLight. All rights reserved. // 創 建 者:leLight // 創建日期:2018/7/30. // 文檔說明:蘋果原生文字轉語音播報. // 修 改 人: // 修改日期: // #import <Foundation/Foundation.h> #import <AVFoundation/AVFoundation.h> @protocol CHiOSSpeechDelegate <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; @end @interface CHiOSSpeech : NSObject /** 文字播報代理 */ @property (nonatomic, weak) id<CHiOSSpeechDelegate> delegate; /************ 單例對象 *****************************/ + (CHiOSSpeech *)sharedInstance; /************ 開始播放 *****************************/ - (void)startSpeekWithString:(NSString *)string; /************ 暫停播放 *****************************/ - (void)pauseSpeaking; /************ 繼續播放 *****************************/ - (void)continueSpeaking; // 初始化配置:這裏不設置則使用默認參數 /** * 設置播放的聲音參數 如果選擇默認請傳入 -1.0 * * @param aVolume 音量(0.0~1.0)默認為1.0 * @param aRate 語速(0.0~1.0)默認為1.0 * @param aPitchMultiplier 語調 (0.5-2.0)默認為1.0 * @param languageCode 語言 默認為 中文普通話:@"zh-CN" */ - (void)setDefaultWithVolume:(float)aVolume rate:(CGFloat)aRate pitchMultiplier:(CGFloat)aPitchMultiplier languageCode:(NSString *)languageCode; @end
2、CHiOSSpeech.m
// // 文 件 名:CHiOSSpeech.m // // 版權所有:Copyright ? 2018年 leLight. All rights reserved. // 創 建 者:leLight // 創建日期:2018/7/30. // 文檔說明:蘋果原生文字轉語音播報. // 修 改 人: // 修改日期: // #import "CHiOSSpeech.h" @interface CHiOSSpeech () <AVSpeechSynthesizerDelegate> { AVSpeechSynthesizer *av; AVSpeechUtterance *utterance; } /** 語速 */ @property(nonatomic, assign) float rate; /** 音量 */ @property(nonatomic, assign) float volume; /** 音調 */ @property(nonatomic, assign) float pitchMultiplier; /** 音調 */ @property(nonatomic, copy) NSString *languageCode; @end @implementation CHiOSSpeech /************ 單例對象 *****************************/ + (CHiOSSpeech *)sharedInstance { static CHiOSSpeech *sharedClient = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedClient = [[CHiOSSpeech alloc] init]; }); return sharedClient; } /************ 初始化創建 *****************************/ - (instancetype)init { self = [super init]; if (self) { // 初始化對象 av = [[AVSpeechSynthesizer alloc] init]; // 掛上代理 av.delegate = self; // 初始化配置 [self setDefaultWithVolume:-1.0 rate:-1.0 pitchMultiplier:-1.0 languageCode:@"zh-CN"]; }; return self; } /************ 開始播放 *****************************/ - (void)startSpeekWithString:(NSString *)string { utterance = [[AVSpeechUtterance alloc] initWithString:string]; // 設置語速,範圍0-1,註意0最慢,1最快;AVSpeechUtteranceMinimumSpeechRate最慢,AVSpeechUtteranceMaximumSpeechRate最快 utterance.rate = 0.5; //設置發音,這是中文普通話:@"zh-CN" AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:self.languageCode]; utterance.voice = voice; // 設置語速 utterance.rate = self.rate; // 設置音量(0.0~1.0)默認為1.0 utterance.volume = self.volume; // 設置語調 (0.5-2.0) utterance.pitchMultiplier = self.pitchMultiplier; // 目的是讓語音合成器播放下一語句前有短暫的暫停 utterance.postUtteranceDelay = 1; // 開始 [av speakUtterance:utterance]; } /************ 暫停播放 *****************************/ - (void)pauseSpeaking { [av pauseSpeakingAtBoundary:AVSpeechBoundaryWord]; } /************ 繼續播放 *****************************/ - (void)continueSpeaking { // 如果暫停則恢復,會從暫停的地方繼續 [av continueSpeaking]; } #pragma mark ***************************** AVSpeechSynthesizerDelegate *********************************************** /************ 開始播放 *****************************/ - (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didStartSpeechUtterance:(AVSpeechUtterance*)utterance { CHLog(@"是否正在播放:%d", synthesizer.isSpeaking); CHLog(@"是否處於播放:%d", synthesizer.isPaused); CHLog(@"播放的內容:%@", utterance.speechString); CHLog(@"---開始播放"); if([self.delegate respondsToSelector:@selector(speechSynthesizer: didStartSpeechUtterance:)]) { [self.delegate speechSynthesizer:synthesizer didStartSpeechUtterance:utterance]; } } /************ 完成播放 *****************************/ - (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance*)utterance { CHLog(@"---完成播放"); if([self.delegate respondsToSelector:@selector(speechSynthesizer: didFinishSpeechUtterance:)]) { [self.delegate speechSynthesizer:synthesizer didFinishSpeechUtterance:utterance]; } } /************ 播放中止 *****************************/ - (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance*)utterance { CHLog(@"---播放中止"); if([self.delegate respondsToSelector:@selector(speechSynthesizer: didPauseSpeechUtterance:)]) { [self.delegate speechSynthesizer:synthesizer didPauseSpeechUtterance:utterance]; } } /************ 恢復播放 *****************************/ - (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance*)utterance { CHLog(@"---恢復播放"); if([self.delegate respondsToSelector:@selector(speechSynthesizer: didContinueSpeechUtterance:)]) { [self.delegate speechSynthesizer:synthesizer didContinueSpeechUtterance:utterance]; } } /************ 播放取消 *****************************/ - (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance*)utterance { CHLog(@"---播放取消"); if([self.delegate respondsToSelector:@selector(speechSynthesizer: didCancelSpeechUtterance:)]) { [self.delegate speechSynthesizer:synthesizer didCancelSpeechUtterance:utterance]; } } // 初始化配置 /** * 設置播放的聲音參數 如果選擇默認請傳入 -1.0 * * @param aVolume 音量(0.0~1.0)默認為1.0 * @param aRate 語速(0.0~1.0)默認為1.0 * @param aPitchMultiplier 語調 (0.5-2.0)默認為1.0 * @param languageCode 語言 默認為 中文普通話:@"zh-CN" */ - (void)setDefaultWithVolume:(float)aVolume rate:(CGFloat)aRate pitchMultiplier:(CGFloat)aPitchMultiplier languageCode:(NSString *)languageCode { self.rate = aRate; self.volume = aVolume; self.pitchMultiplier = aPitchMultiplier; self.languageCode = languageCode; if (aRate == -1.0) { self.rate = 0.5; } if (aVolume == -1.0) { self.volume = 1.0; } if (aPitchMultiplier == -1.0) { self.pitchMultiplier = 1; } } @end
蘋果原生文字轉語音播報