源碼地址:https://github.com/chenfanfang/CollectionsOfExample
下面先來(lái)一張UI布局圖
下面附上代碼(代碼中有詳細(xì)的注釋)
//
// FFVoicePlayTextController.m
// CollectionsOfExample
//
// Created by mac on 16/7/30.
// Copyright ? 2016年 chenfanfang. All rights reserved.
//
#import "FFVoicePlayTextController.h"
//system
#import <AVFoundation/AVFoundation.h>
@interface FFVoicePlayTextController ()<AVSpeechSynthesizerDelegate>
/** 文本輸入框 */
@property (weak, nonatomic) IBOutlet UITextView *textView;
/** 語(yǔ)言數(shù)組 */
@property (nonatomic, strong) NSArray<AVSpeechSynthesisVoice *> *laungeVoices;
/** 語(yǔ)音合成器 */
@property (nonatomic, strong) AVSpeechSynthesizer *synthesizer;
@end
@implementation FFVoicePlayTextController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"語(yǔ)音播放文字內(nèi)容";
}
/***********************************懶加載***********************************/
#pragma mark - 懶加載
- (NSArray<AVSpeechSynthesisVoice *> *)laungeVoices {
if (_laungeVoices == nil) {
_laungeVoices = @[
//美式英語(yǔ)
[AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"],
//英式英語(yǔ)
[AVSpeechSynthesisVoice voiceWithLanguage:@"en-GB"],
//中文
[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"]
];
}
return _laungeVoices;
}
- (AVSpeechSynthesizer *)synthesizer {
if (_synthesizer == nil) {
_synthesizer = [[AVSpeechSynthesizer alloc] init];
_synthesizer.delegate = self;
}
return _synthesizer;
}
/***********************************事件處理***********************************/
#pragma mark - 事件處理
/** 語(yǔ)音播放 */
- (IBAction)voiceBtnClick:(UIButton *)sender {
//創(chuàng)建一個(gè)會(huì)話
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:self.textView.text];
//選擇語(yǔ)言發(fā)音的類別牙瓢,如果有中文,一定要選擇中文,要不然無(wú)法播放語(yǔ)音
utterance.voice = self.laungeVoices[2];
//播放語(yǔ)言的速率庄蹋,值越大赠法,播放速率越快
utterance.rate = 0.4f;
//音調(diào) -- 為語(yǔ)句指定pitchMultiplier 镰踏,可以在播放特定語(yǔ)句時(shí)改變聲音的音調(diào)唯鸭、pitchMultiplier的允許值一般介于0.5(低音調(diào))和2.0(高音調(diào))之間
utterance.pitchMultiplier = 0.8f;
//讓語(yǔ)音合成器在播放下一句之前有短暫時(shí)間的暫停哑了,也可以類似的設(shè)置preUtteranceDelay
utterance.postUtteranceDelay = 0.1f;
//播放語(yǔ)言
[self.synthesizer speakUtterance:utterance];
}
/** 暫停語(yǔ)音播放/回復(fù)語(yǔ)音播放 */
- (IBAction)playAndPauseBtnClick:(UIButton *)sender {
if (self.synthesizer.isPaused == YES) { //暫停狀態(tài)
//繼續(xù)播放
[self.synthesizer continueSpeaking];
}
else { //現(xiàn)在在播放
//立即暫停播放
[self.synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
}
/** 停止播放語(yǔ)音 */
- (void)stopPlayVoice {
if (self.synthesizer.isSpeaking) { //正在語(yǔ)音播放
//立即停止播放語(yǔ)音
[self.synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
}
/**********************AVSpeechSynthesizerDelegate(代理方法)***********************/
#pragma mark - AVSpeechSynthesizerDelegate(代理方法)
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance {
NSLog(@"開(kāi)始播放語(yǔ)音的時(shí)候調(diào)用");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
NSLog(@"語(yǔ)音播放結(jié)束的時(shí)候調(diào)用");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance {
NSLog(@"暫停語(yǔ)音播放的時(shí)候調(diào)用");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance {
NSLog(@"繼續(xù)播放語(yǔ)音的時(shí)候調(diào)用");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance {
NSLog(@"取消語(yǔ)音播放的時(shí)候調(diào)用");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance {
/** 將要播放的語(yǔ)音文字 */
NSString *willSpeakRangeOfSpeechString = [utterance.speechString substringWithRange:characterRange];
NSLog(@"即將播放的語(yǔ)音文字:%@",willSpeakRangeOfSpeechString);
}
@end
支持的語(yǔ)言語(yǔ)音如下:
"[AVSpeechSynthesisVoice 0x978a0b0] Language: th-TH",
"[AVSpeechSynthesisVoice 0x977a450] Language: pt-BR",
"[AVSpeechSynthesisVoice 0x977a480] Language: sk-SK",
"[AVSpeechSynthesisVoice 0x978ad50] Language: fr-CA",
"[AVSpeechSynthesisVoice 0x978ada0] Language: ro-RO",
"[AVSpeechSynthesisVoice 0x97823f0] Language: no-NO",
"[AVSpeechSynthesisVoice 0x978e7b0] Language: fi-FI",
"[AVSpeechSynthesisVoice 0x978af50] Language: pl-PL",
"[AVSpeechSynthesisVoice 0x978afa0] Language: de-DE",
"[AVSpeechSynthesisVoice 0x978e390] Language: nl-NL",
"[AVSpeechSynthesisVoice 0x978b030] Language: id-ID",
"[AVSpeechSynthesisVoice 0x978b080] Language: tr-TR",
"[AVSpeechSynthesisVoice 0x978b0d0] Language: it-IT",
"[AVSpeechSynthesisVoice 0x978b120] Language: pt-PT",
"[AVSpeechSynthesisVoice 0x978b170] Language: fr-FR",
"[AVSpeechSynthesisVoice 0x978b1c0] Language: ru-RU",
"[AVSpeechSynthesisVoice 0x978b210] Language: es-MX",
"[AVSpeechSynthesisVoice 0x978b2d0] Language: zh-HK", 中文(香港) 粵語(yǔ)
"[AVSpeechSynthesisVoice 0x978b320] Language: sv-SE",
"[AVSpeechSynthesisVoice 0x978b010] Language: hu-HU",
"[AVSpeechSynthesisVoice 0x978b440] Language: zh-TW", 中文(臺(tái)灣)
"[AVSpeechSynthesisVoice 0x978b490] Language: es-ES",
"[AVSpeechSynthesisVoice 0x978b4e0] Language: zh-CN", 中文(普通話)
"[AVSpeechSynthesisVoice 0x978b530] Language: nl-BE",
"[AVSpeechSynthesisVoice 0x978b580] Language: en-GB", 英式英語(yǔ)
"[AVSpeechSynthesisVoice 0x978b5d0] Language: ar-SA",
"[AVSpeechSynthesisVoice 0x978b620] Language: ko-KR",
"[AVSpeechSynthesisVoice 0x978b670] Language: cs-CZ",
"[AVSpeechSynthesisVoice 0x978b6c0] Language: en-ZA",
"[AVSpeechSynthesisVoice 0x978aed0] Language: en-AU",
"[AVSpeechSynthesisVoice 0x978af20] Language: da-DK",
"[AVSpeechSynthesisVoice 0x978b810] Language: en-US", 美式英語(yǔ)
"[AVSpeechSynthesisVoice 0x978b860] Language: en-IE",
"[AVSpeechSynthesisVoice 0x978b8b0] Language: hi-IN",
"[AVSpeechSynthesisVoice 0x978b900] Language: el-GR",
"[AVSpeechSynthesisVoice 0x978b950] Language: ja-JP" )