iOS 原生語音播報(bào)的使用

注:原生的語音播報(bào)功能是在7.0以后才出現(xiàn)的呻疹,在使用時(shí)要注意判斷當(dāng)前版本號(hào)吃引。

一、實(shí)現(xiàn)語音播報(bào)相關(guān)方法

1刽锤、在使用播報(bào)功能的類中引入“頭文件”(required)或“代理”(optional)

#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVSpeechSynthesizerDelegate>{

AVSpeechSynthesizer *_synthesizer;

}

2镊尺、開始語音播報(bào)

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

NSString *broadCastStr = @"承德避暑山莊又名“承德離宮”或“熱河行宮”,位于河北省承德市中心北部并思,武烈河西岸一帶狹長(zhǎng)的谷地上庐氮,是清代皇帝夏天避暑和處理政務(wù)的場(chǎng)避暑山莊始建于1703年,歷經(jīng)清康熙宋彼、雍正弄砍、乾隆三朝,耗時(shí)89年建成输涕。避暑山莊以樸素淡雅的山村野趣為格調(diào)音婶,取自然山水之本色,吸收江南塞北之風(fēng)光占贫,成為中國(guó)現(xiàn)存占地最大的古代帝王宮苑";

[self voiceBroadCastStr:broadCastStr];

}

//content代表要播報(bào)的內(nèi)容

-(void)voiceBroadCastStr:(NSString *)content

{

//初始化語音播報(bào)桃熄,控制播放先口、暫停

_synthesizer = [[AVSpeechSynthesizer alloc]init];

//語音對(duì)象型奥,說中文(zh_CN)瞳收,英文(en-US)

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

// 實(shí)例化發(fā)聲的對(duì)象,及朗讀的內(nèi)容

AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:content];

//指定語音類型

utterance.voice = voiceType;

utterance.rate = 0.4;

utterance.volume = 1.0;

_synthesizer.delegate = self;//注意:代理方法寫在啟動(dòng)前

[_synthesizer speakUtterance:utterance];//啟動(dòng)

}

3厢汹、暫停語音播報(bào)

[_synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];

4螟深、繼續(xù)語音播報(bào)

[_synthesizer continueSpeaking];

5、取消或停止語音播報(bào)

/**

*AVSpeechBoundaryImmediate,? 立即停

AVSpeechBoundaryWord? 說完一個(gè)整詞再停

*/

[_synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];


二烫葬、語音播報(bào)相關(guān)類

1界弧、AVSPeechSynthesizer:用于控制語音播報(bào)的“開始”,“暫痛钭郏”垢箕,“繼續(xù)”,“停止,”;且含有代理兑巾,幫助我們監(jiān)聽當(dāng)前語音的播報(bào)狀態(tài)条获。

2、AVSpeechUtterance(發(fā)聲的對(duì)象):用于指定播報(bào)的內(nèi)容蒋歌,語言的屬性\voice帅掘,播報(bào)的速率\rate,播報(bào)的音量\voiume堂油,以及音高\(yùn)pitchMultiplier

3修档、AVSpeechSynthesisVoice: 用于設(shè)置語音的屬性,包括語言類型/language,語音播報(bào)者名稱/name,語音的標(biāo)識(shí)/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:用于監(jiān)聽語音播報(bào)狀態(tài)

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

{

NSLog(@"已經(jīng)開始播報(bào)");NSLog(@"判斷播報(bào)狀態(tài)%u\n%u",_synthesizer.isSpeaking,_synthesizer.isPaused);//1,0

}

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

{

NSLog(@"已經(jīng)暫停播報(bào)");

NSLog(@"判斷播報(bào)狀態(tài)%u\n%u",_synthesizer.isSpeaking,_synthesizer.isPaused);//1,1

}

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

{

NSLog(@"已經(jīng)完成播報(bào)");

NSLog(@"判斷播報(bào)狀態(tài)%u\n%u",_synthesizer.isSpeaking,_synthesizer.isPaused);//0,0

}

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

{

NSLog(@"已經(jīng)停止播報(bào)");

}

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

{

NSLog(@"繼續(xù)播報(bào)");

NSLog(@"判斷播報(bào)狀態(tài)%u\n%u",_synthesizer.isSpeaking,_synthesizer.isPaused);//1,0

}

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

{

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市迫靖,隨后出現(xiàn)的幾起案子癣诱,更是在濱河造成了極大的恐慌,老刑警劉巖袜香,帶你破解...
    沈念sama閱讀 217,542評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件撕予,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡蜈首,警方通過查閱死者的電腦和手機(jī)实抡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來欢策,“玉大人吆寨,你說我怎么就攤上這事〔瓤埽” “怎么了啄清?”我有些...
    開封第一講書人閱讀 163,912評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)俺孙。 經(jīng)常有香客問我辣卒,道長(zhǎng)掷贾,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,449評(píng)論 1 293
  • 正文 為了忘掉前任荣茫,我火速辦了婚禮想帅,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘啡莉。我一直安慰自己港准,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評(píng)論 6 392
  • 文/花漫 我一把揭開白布咧欣。 她就那樣靜靜地躺著浅缸,像睡著了一般。 火紅的嫁衣襯著肌膚如雪魄咕。 梳的紋絲不亂的頭發(fā)上疗杉,一...
    開封第一講書人閱讀 51,370評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音蚕礼,去河邊找鬼烟具。 笑死,一個(gè)胖子當(dāng)著我的面吹牛奠蹬,可吹牛的內(nèi)容都是我干的朝聋。 我是一名探鬼主播,決...
    沈念sama閱讀 40,193評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼囤躁,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼冀痕!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起狸演,我...
    開封第一講書人閱讀 39,074評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤言蛇,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后宵距,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體腊尚,經(jīng)...
    沈念sama閱讀 45,505評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評(píng)論 3 335
  • 正文 我和宋清朗相戀三年满哪,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了婿斥。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,841評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡哨鸭,死狀恐怖民宿,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情像鸡,我是刑警寧澤活鹰,帶...
    沈念sama閱讀 35,569評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響志群,放射性物質(zhì)發(fā)生泄漏着绷。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評(píng)論 3 328
  • 文/蒙蒙 一赖舟、第九天 我趴在偏房一處隱蔽的房頂上張望蓬戚。 院中可真熱鬧夸楣,春花似錦宾抓、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至紧显,卻和暖如春讲衫,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背孵班。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工涉兽, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人篙程。 一個(gè)月前我還...
    沈念sama閱讀 47,962評(píng)論 2 370
  • 正文 我出身青樓枷畏,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親虱饿。 傳聞我的和親對(duì)象是個(gè)殘疾皇子拥诡,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容