屏幕快照 2017-01-11 上午12.22.24.png
思路:
這里主要使用系統(tǒng)自帶的AVFoundation框架,利用語音合成器AVSpeechSynthesizer 的實例對象相關方法實現(xiàn)相關操作:
一:導入框架AVFoundation
在項目的TARGETS下的linked Frameworks and Libraries 點擊+
屏幕快照 2017-01-10 下午11.54.16.png
在系統(tǒng)頂部導入頭文件
#import <AVFoundation/AVFoundation.h>
二:利用懶加載生成語音合成器和語音數(shù)組
// 懶加載
-(NSArray<AVSpeechSynthesisVoice *> *)laungeVoices{
if (_laungeVoices == nil) {
_laungeVoices = @[
// 美式英語
[AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"],
// 英式英語
[AVSpeechSynthesisVoice voiceWithLanguage:@"en-GB"],
// 中文
[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"]
];
}
return _laungeVoices;
}
-(AVSpeechSynthesizer *)synthesizer{
if (_synthesizer == nil) {
_synthesizer = [[AVSpeechSynthesizer alloc]init];
_synthesizer.delegate = self;
}
return _synthesizer;
}
三:實現(xiàn)語音的開始和停止的操作
這里一定注意,選擇語音發(fā)音的類別,如果有中文,一定選擇中文,如果在有中文的情況下沒有選擇中文,那么中文一定不會被讀出,英文是在什么情況下都可以被讀出的,只是美式和英式的差別,因此這里最好設置為中文
utterance.voice = self.laungeVoices[2];
#pragma mark 語音處理
- (IBAction)voiceBtnClick:(UIButton *)sender {
// 創(chuàng)建一個對話
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:self.textView.text];
// 選擇語音發(fā)音的類別,如果有中文,一定選擇中文
utterance.voice = self.laungeVoices[2];
// 播放語音的速錄,值越大,速度越快
utterance.rate = 0.4f;
//音調 -- 為語句指定pitchMultiplier 耕渴,可以在播放特定語句時改變聲音的音調颁股、pitchMultiplier的允許值一般介于0.5(低音調)和2.0(高音調)之間
utterance.pitchMultiplier = 0.8f;
//讓語音合成器在播放下一句之前有短暫時間的暫停派昧,也可以類似的設置preUtteranceDelay
utterance.postUtteranceDelay = 0.1f;
if (self.synthesizer.isSpeaking) { //正在語音播放
//立即停止播放語音
[self.synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
else{
//播放語言
[self.synthesizer speakUtterance:utterance];
}
}
四:實現(xiàn)暫停和繼續(xù)的語音播放
/** 暫停語音播放/回復語音播放 */
- (IBAction)playAndPauseBtnClick:(UIButton *)sender {
if (self.synthesizer.isPaused == YES) { //暫停狀態(tài)
//繼續(xù)播放
[self.synthesizer continueSpeaking];
}
else { //現(xiàn)在在播放
//立即暫停播放
[self.synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
}
五:注意一下幾個代理方法的使用場景
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance {
NSLog(@"開始播放語音的時候調用");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
NSLog(@"語音播放結束的時候調用");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance {
NSLog(@"暫停語音播放的時候調用");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance {
NSLog(@"繼續(xù)播放語音的時候調用");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance {
NSLog(@"取消語音播放的時候調用");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance {
/** 將要播放的語音文字 */
NSString *willSpeakRangeOfSpeechString = [utterance.speechString substringWithRange:characterRange];
NSLog(@"即將播放的語音文字:%@",willSpeakRangeOfSpeechString);
}
注意:
https://github.com/chenfanfang/CollectionsOfExample
里面有正則表達式的使用方法以及一下效果的實現(xiàn)源碼
不同寬度的標簽.png
下拉菜單的基本使用.png
雪花飄落效果.png
自定義菜單樣式1.png
自定義菜單cell(非xib).png
自定義下拉菜單cell(xib).png