iOS 語音播放文字內(nèi)容--制作簡易聽書軟件(AVSpeechSynthesizer)

聽書.png

源碼地址:https://github.com/chenfanfang/CollectionsOfExample

下面先來一張UI布局圖

Snip20160730_3.png

下面附上代碼(代碼中有詳細(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;

/** 語言數(shù)組 */
@property (nonatomic, strong) NSArray<AVSpeechSynthesisVoice *> *laungeVoices;

/** 語音合成器 */
@property (nonatomic, strong) AVSpeechSynthesizer *synthesizer;

@end

@implementation FFVoicePlayTextController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"語音播放文字內(nèi)容";
    
}

/***********************************懶加載***********************************/
#pragma mark - 懶加載
- (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;
}

/***********************************事件處理***********************************/
#pragma mark - 事件處理

/** 語音播放 */
- (IBAction)voiceBtnClick:(UIButton *)sender {
    
    //創(chuàng)建一個(gè)會(huì)話
    AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:self.textView.text];
    
    //選擇語言發(fā)音的類別,如果有中文,一定要選擇中文,要不然無法播放語音
    utterance.voice = self.laungeVoices[2];
    
    //播放語言的速率,值越大,播放速率越快
    utterance.rate = 0.4f;
    
    //音調(diào)  --  為語句指定pitchMultiplier ,可以在播放特定語句時(shí)改變聲音的音調(diào)、pitchMultiplier的允許值一般介于0.5(低音調(diào))和2.0(高音調(diào))之間
    utterance.pitchMultiplier = 0.8f;
    
    //讓語音合成器在播放下一句之前有短暫時(shí)間的暫停,也可以類似的設(shè)置preUtteranceDelay
    utterance.postUtteranceDelay = 0.1f;
    
    //播放語言
    [self.synthesizer speakUtterance:utterance];
    
}

/** 暫停語音播放/回復(fù)語音播放 */
- (IBAction)playAndPauseBtnClick:(UIButton *)sender {
    
    if (self.synthesizer.isPaused == YES) { //暫停狀態(tài)
        //繼續(xù)播放
        [self.synthesizer continueSpeaking];
    }
    
    else { //現(xiàn)在在播放
        
        //立即暫停播放
        [self.synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
    }
}

/** 停止播放語音 */
- (void)stopPlayVoice {
    if (self.synthesizer.isSpeaking) { //正在語音播放
        
        //立即停止播放語音
        [self.synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
    }
}

/**********************AVSpeechSynthesizerDelegate(代理方法)***********************/
#pragma mark - AVSpeechSynthesizerDelegate(代理方法)

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"開始播放語音的時(shí)候調(diào)用");
}

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"語音播放結(jié)束的時(shí)候調(diào)用");
}

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"暫停語音播放的時(shí)候調(diào)用");
}

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"繼續(xù)播放語音的時(shí)候調(diào)用");
}

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"取消語音播放的時(shí)候調(diào)用");
}

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance {
    
    /** 將要播放的語音文字 */
    NSString *willSpeakRangeOfSpeechString = [utterance.speechString substringWithRange:characterRange];
    
    NSLog(@"即將播放的語音文字:%@",willSpeakRangeOfSpeechString);
}

@end

支持的語言語音如下:


"[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",  中文(香港) 粵語  
"[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",  英式英語 
"[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",  美式英語
"[AVSpeechSynthesisVoice 0x978b860] Language: en-IE",  
"[AVSpeechSynthesisVoice 0x978b8b0] Language: hi-IN",   
"[AVSpeechSynthesisVoice 0x978b900] Language: el-GR",  
"[AVSpeechSynthesisVoice 0x978b950] Language: ja-JP" ) 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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