iOS中的语音播报

偶然发现的原生的语音播报,简单的看一下:

注:原生的语音播报功能是在7.0以后才出现的,在使用时要注意判断当前版本号。

语音播报的三个类: AVSpeechSynthesizerAVSpeechUtteranceAVSpeechSynthesisVoice

AVSpeechSynthesizer : 用于控制语音播报的开始、暂停、继续和停止 —— 有代理,监听播报状态;
AVSpeechUtterance : 播报声音的对象 : 指定播报内容,语速设置 、 声音大小设置等;
AVSpeechSynthesisVoice : 设置声音的属性 : 语言类型、播报者名称等;

1、首先导入头文件:

#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVSpeechSynthesizerDelegate>{
    
//    用于控制语音播报的开始、暂停、继续和停止 —— 有代理,监听播报状态
    AVSpeechSynthesizer *_synthesizer;

//    播报声音的对象 : 指定播报内容,语速设置 、 声音大小设置等
    AVSpeechUtterance *_utterance;
    
    //    设置声音的属性 : 语言类型、播报者名称等
    AVSpeechSynthesisVoice *_voiceType;
    
}

2、设置播报的内容:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.playContentStr = @"自然地理是研究自然地理环境的组成、结构、功能、动态及其空间分异规律的学科,是地理学的一个重要分支学科。综合性的分支科学有综合自然地理学、区域自然地理学、古地理学等。部门性的分支科学有地貌学、气候学、水文地理学、生物地理学、冰川学等。狭义的自然地理学仅指综合自然地理学,部门自然地理学已逐步发展成为一门独立的学科。狭义的自然地理学仅指综合自然地理学。";
     
    //初始化语音播报,控制播放、暂停
    _synthesizer = [[AVSpeechSynthesizer alloc]init];
    _synthesizer.delegate = self;//注意:代理方法写在启动前
    
    //语音对象,说中文(zh_CN),英文(en-US)
    _voiceType = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh_CN"];
    
    // 实例化发声的对象,及朗读的内容 ---  发声对象
    _utterance = [AVSpeechUtterance speechUtteranceWithString:self.playContentStr];
    
    //指定语音类型
    _utterance.voice = _voiceType;
    //     语音的播放速度
    //    utterance.rate = 1;
    
    _utterance.volume = 1.0;
    
}

3、开始播报:

 - (IBAction)starPlay:(id)sender {

    if ([[[UIDevice currentDevice]systemVersion]integerValue]>=7.0) {
        
        [_synthesizer speakUtterance:_utterance];//启动

    }

}

4、暂停播报:

/*
 *   AVSpeechBoundaryImmediate,  立即停止
 *
 *
 *   AVSpeechBoundaryWord  说完一个整词再停
 *
 */

- (IBAction)pausePlay:(id)sender {
    
    [_synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
    
}

5、继续播报和停止播报:

 - (IBAction)resumePlay:(id)sender {
    
    [_synthesizer continueSpeaking];
    
}
- (IBAction)stopPlay:(id)sender {
    [_synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
    
}

代理方法 —— 监听 播报状态

#pragma mark - AVSpeechSynthesizer的代理方法 - 监听状态
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance {
    
    NSLog(@"已经开始播报");
    //1,0
    NSLog(@"判断播报状态%u\n%u",_synthesizer.isSpeaking,_synthesizer.isPaused);
    
}

-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance{
    
    NSLog(@"暂停播报");
    
    NSLog(@"判断播报状态%u\n%u",_synthesizer.isSpeaking,_synthesizer.isPaused);//1,1
    
}

-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance
{
    
    NSLog(@"完成播报");
    
    //0,0
    NSLog(@"判断播报状态%u\n%u",_synthesizer.isSpeaking,_synthesizer.isPaused);
    
}

-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance
{
    
    NSLog(@"已经停止播报");
    
}

-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance
{
    
    NSLog(@"继续播报");
    
    NSLog(@"判断播报状态%u\n%u",_synthesizer.isSpeaking,_synthesizer.isPaused);//1,0
    
}

-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance
{
    
}

PS:AVSpeechSynthesisVoice *_voiceType; 支持的声音类型如下:

AVSpeechSynthesisVoice *_voiceType;  支持的声音类型如下:
 
 <__NSArrayM 0x1742487c0>(
 
 [AVSpeechSynthesisVoice 0x174013040] Language: ar-SA, Name: Maged, Quality: Default [com.apple.ttsbundle.Maged-compact],
 
 [AVSpeechSynthesisVoice 0x174010210] Language: cs-CZ, Name: Zuzana, Quality: Default [com.apple.ttsbundle.Zuzana-compact],
 
 [AVSpeechSynthesisVoice 0x174013400] Language: da-DK, Name: Sara, Quality: Default [com.apple.ttsbundle.Sara-compact],
 
 [AVSpeechSynthesisVoice 0x174012b00] Language: de-DE, Name: Anna, Quality: Default [com.apple.ttsbundle.Anna-compact],
 
 [AVSpeechSynthesisVoice 0x174012dc0] Language: de-DE, Name: Helena, Quality: Default [com.apple.ttsbundle.siri_female_de-DE_compact],
 
 [AVSpeechSynthesisVoice 0x174013140] Language: de-DE, Name: Martin, Quality: Default [com.apple.ttsbundle.siri_male_de-DE_compact],
 
 [AVSpeechSynthesisVoice 0x1740131c0] Language: el-GR, Name: Melina, Quality: Default [com.apple.ttsbundle.Melina-compact],
 
 [AVSpeechSynthesisVoice 0x174012bc0] Language: en-AU, Name: Catherine, Quality: Default [com.apple.ttsbundle.siri_female_en-AU_compact],
 
 [AVSpeechSynthesisVoice 0x174012d40] Language: en-AU, Name: Gordon, Quality: Default [com.apple.ttsbundle.siri_male_en-AU_compact],
 
 [AVSpeechSynthesisVoice 0x174012ec0] Language: en-AU, Name: Karen, Quality: Default [com.apple.ttsbundle.Karen-compact],
 
 [AVSpeechSynthesisVoice 0x174012b40] Language: en-GB, Name: Arthur, Quality: Default [com.apple.ttsbundle.siri_male_en-GB_compact],
 
 [AVSpeechSynthesisVoice 0x174012c40] Language: en-GB, Name: Daniel, Quality: Default [com.apple.ttsbundle.Daniel-compact],
 
 [AVSpeechSynthesisVoice 0x174013100] Language: en-GB, Name: Martha, Quality: Default [com.apple.ttsbundle.siri_female_en-GB_compact],
 
 [AVSpeechSynthesisVoice 0x174013240] Language: en-IE, Name: Moira, Quality: Default [com.apple.ttsbundle.Moira-compact],
 
 [AVSpeechSynthesisVoice 0x174012730] Language: en-US, Name: Aaron, Quality: Default [com.apple.ttsbundle.siri_male_en-US_compact],
 
 [AVSpeechSynthesisVoice 0x174012d00] Language: en-US, Name: Fred, Quality: Default [com.apple.speech.synthesis.voice.Fred],
 
 [AVSpeechSynthesisVoice 0x1740132c0] Language: en-US, Name: Nicky, Quality: Default [com.apple.ttsbundle.siri_female_en-US_compact],
 
 [AVSpeechSynthesisVoice 0x1740133c0] Language: en-US, Name: Samantha, Quality: Default [com.apple.ttsbundle.Samantha-compact],
 
 [AVSpeechSynthesisVoice 0x1740134b0] Language: en-ZA, Name: Tessa, Quality: Default [com.apple.ttsbundle.Tessa-compact],
 
 [AVSpeechSynthesisVoice 0x174013280] Language: es-ES, Name: Monica, Quality: Default [com.apple.ttsbundle.Monica-compact],
 
 [AVSpeechSynthesisVoice 0x174013380] Language: es-MX, Name: Paulina, Quality: Default [com.apple.ttsbundle.Paulina-compact],
 
 [AVSpeechSynthesisVoice 0x174013440] Language: fi-FI, Name: Satu, Quality: Default [com.apple.ttsbundle.Satu-compact],
 
 [AVSpeechSynthesisVoice 0x174012ac0] Language: fr-CA, Name: Amelie, Quality: Default [com.apple.ttsbundle.Amelie-compact],
 
 [AVSpeechSynthesisVoice 0x174012c80] Language: fr-FR, Name: Daniel, Quality: Default [com.apple.ttsbundle.siri_male_fr-FR_compact],
 
 [AVSpeechSynthesisVoice 0x174013080] Language: fr-FR, Name: Marie, Quality: Default [com.apple.ttsbundle.siri_female_fr-FR_compact],
 
 [AVSpeechSynthesisVoice 0x1740134f0] Language: fr-FR, Name: Thomas, Quality: Default [com.apple.ttsbundle.Thomas-compact],
 
 [AVSpeechSynthesisVoice 0x174012b80] Language: he-IL, Name: Carmit, Quality: Default [com.apple.ttsbundle.Carmit-compact],
 
 [AVSpeechSynthesisVoice 0x174012f80] Language: hi-IN, Name: Lekha, Quality: Default [com.apple.ttsbundle.Lekha-compact],
 
 [AVSpeechSynthesisVoice 0x1740130c0] Language: hu-HU, Name: Mariska, Quality: Default [com.apple.ttsbundle.Mariska-compact],
 
 [AVSpeechSynthesisVoice 0x174012c00] Language: id-ID, Name: Damayanti, Quality: Default [com.apple.ttsbundle.Damayanti-compact],
 
 [AVSpeechSynthesisVoice 0x174012a40] Language: it-IT, Name: Alice, Quality: Default [com.apple.ttsbundle.Alice-compact],
 
 [AVSpeechSynthesisVoice 0x174012d80] Language: ja-JP, Name: Hattori, Quality: Default [com.apple.ttsbundle.siri_male_ja-JP_compact],
 
 [AVSpeechSynthesisVoice 0x174012f00] Language: ja-JP, Name: Kyoko, Quality: Default [com.apple.ttsbundle.Kyoko-compact],
 
 [AVSpeechSynthesisVoice 0x174013340] Language: ja-JP, Name: O-ren, Quality: Default [com.apple.ttsbundle.siri_female_ja-JP_compact],
 
 [AVSpeechSynthesisVoice 0x1740135f0] Language: ko-KR, Name: Yuna, Quality: Default [com.apple.ttsbundle.Yuna-compact],
 
 [AVSpeechSynthesisVoice 0x174012cc0] Language: nl-BE, Name: Ellen, Quality: Default [com.apple.ttsbundle.Ellen-compact],
 
 [AVSpeechSynthesisVoice 0x174010060] Language: nl-NL, Name: Xander, Quality: Default [com.apple.ttsbundle.Xander-compact],
 
 [AVSpeechSynthesisVoice 0x174013300] Language: no-NO, Name: Nora, Quality: Default [com.apple.ttsbundle.Nora-compact],
 
 [AVSpeechSynthesisVoice 0x174012930] Language: pl-PL, Name: Zosia, Quality: Default [com.apple.ttsbundle.Zosia-compact],
 
 [AVSpeechSynthesisVoice 0x174013000] Language: pt-BR, Name: Luciana, Quality: Default [com.apple.ttsbundle.Luciana-compact],
 
 [AVSpeechSynthesisVoice 0x174012e40] Language: pt-PT, Name: Joana, Quality: Default [com.apple.ttsbundle.Joana-compact],
 
 [AVSpeechSynthesisVoice 0x174012e00] Language: ro-RO, Name: Ioana, Quality: Default [com.apple.ttsbundle.Ioana-compact],
 
 [AVSpeechSynthesisVoice 0x174013200] Language: ru-RU, Name: Milena, Quality: Default [com.apple.ttsbundle.Milena-compact],
 
 [AVSpeechSynthesisVoice 0x174012f40] Language: sk-SK, Name: Laura, Quality: Default [com.apple.ttsbundle.Laura-compact],
 
 [AVSpeechSynthesisVoice 0x174012a80] Language: sv-SE, Name: Alva, Quality: Default [com.apple.ttsbundle.Alva-compact],
 
 [AVSpeechSynthesisVoice 0x174012e80] Language: th-TH, Name: Kanya, Quality: Default [com.apple.ttsbundle.Kanya-compact],
 
 [AVSpeechSynthesisVoice 0x174013570] Language: tr-TR, Name: Yelda, Quality: Default [com.apple.ttsbundle.Yelda-compact],
 
 [AVSpeechSynthesisVoice 0x174012fc0] Language: zh-CN, Name: Li-mu, Quality: Default [com.apple.ttsbundle.siri_male_zh-CN_compact],
 
 [AVSpeechSynthesisVoice 0x174012950] Language: zh-CN, Name: Ting-Ting, Quality: Default [com.apple.ttsbundle.Ting-Ting-compact],
 
 [AVSpeechSynthesisVoice 0x1740135b0] Language: zh-CN, Name: Yu-shu, Quality: Default [com.apple.ttsbundle.siri_female_zh-CN_compact],
 
 [AVSpeechSynthesisVoice 0x1740129a0] Language: zh-HK, Name: Sin-Ji, Quality: Default [com.apple.ttsbundle.Sin-Ji-compact],
 
 [AVSpeechSynthesisVoice 0x174013180] Language: zh-TW, Name: Mei-Jia, Quality: Default [com.apple.ttsbundle.Mei-Jia-compact]
 
 )
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,657评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,662评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,143评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,732评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,837评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,036评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,126评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,868评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,315评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,641评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,773评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,470评论 4 333
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,126评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,859评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,095评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,584评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,676评论 2 351

推荐阅读更多精彩内容