美文网首页IOS_OC_Summarize
iOS 原生语音播报的使用

iOS 原生语音播报的使用

作者: ninazhang | 来源:发表于2017-04-05 15:42 被阅读0次

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

    一、实现语音播报相关方法

    1、在使用播报功能的类中引入“头文件”(required)或“代理”(optional)

    #import <AVFoundation/AVFoundation.h>

    @interface ViewController ()<AVSpeechSynthesizerDelegate>{

    AVSpeechSynthesizer *_synthesizer;

    }

    2、开始语音播报

    if ([[[UIDevice currentDevice]systemVersion]integerValue]>=7.0) {

    NSString *broadCastStr = @"承德避暑山庄又名“承德离宫”或“热河行宫”,位于河北省承德市中心北部,武烈河西岸一带狭长的谷地上,是清代皇帝夏天避暑和处理政务的场避暑山庄始建于1703年,历经清康熙、雍正、乾隆三朝,耗时89年建成。避暑山庄以朴素淡雅的山村野趣为格调,取自然山水之本色,吸收江南塞北之风光,成为中国现存占地最大的古代帝王宫苑";

    [self voiceBroadCastStr:broadCastStr];

    }

    //content代表要播报的内容

    -(void)voiceBroadCastStr:(NSString *)content

    {

    //初始化语音播报,控制播放、暂停

    _synthesizer = [[AVSpeechSynthesizer alloc]init];

    //语音对象,说中文(zh_CN),英文(en-US)

    AVSpeechSynthesisVoice *voiceType = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh_CN"];

    // 实例化发声的对象,及朗读的内容

    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:content];

    //指定语音类型

    utterance.voice = voiceType;

    utterance.rate = 0.4;

    utterance.volume = 1.0;

    _synthesizer.delegate = self;//注意:代理方法写在启动前

    [_synthesizer speakUtterance:utterance];//启动

    }

    3、暂停语音播报

    [_synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];

    4、继续语音播报

    [_synthesizer continueSpeaking];

    5、取消或停止语音播报

    /**

    *AVSpeechBoundaryImmediate,  立即停

    AVSpeechBoundaryWord  说完一个整词再停

    */

    [_synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];

    二、语音播报相关类

    1、AVSPeechSynthesizer:用于控制语音播报的“开始”,“暂停”,“继续”,“停止,”;且含有代理,帮助我们监听当前语音的播报状态。

    2、AVSpeechUtterance(发声的对象):用于指定播报的内容,语言的属性\voice,播报的速率\rate,播报的音量\voiume,以及音高\pitchMultiplier

    3、AVSpeechSynthesisVoice: 用于设置语音的属性,包括语言类型/language,语音播报者名称/name,语音的标识/identifier,以及quality。

    /*例如:language:zh-CN      name:Li-mu      quality:1

    identifier:com.apple.ttsbundle.siri_male_zh-CN_compact*/

    /*支持的所有声音

    <__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]

    )

    */

    三、AVSpeechSynthesizerDelegate:用于监听语音播报状态

    -(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance

    {

    NSLog(@"已经开始播报");NSLog(@"判断播报状态%u\n%u",_synthesizer.isSpeaking,_synthesizer.isPaused);//1,0

    }

    -(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(@"已经完成播报");

    NSLog(@"判断播报状态%u\n%u",_synthesizer.isSpeaking,_synthesizer.isPaused);//0,0

    }

    -(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

    {

    }

    相关文章

      网友评论

        本文标题:iOS 原生语音播报的使用

        本文链接:https://www.haomeiwen.com/subject/fqxhrttx.html