iOS 語(yǔ)音播放文字內(nèi)容--制作簡(jiǎn)易聽書軟件(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;

/** 語(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ā)音的類別,如果有中文,一定要選擇中文,要不然無法播放語(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(@"開始播放語(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" ) 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,725評(píng)論 25 709
  • 太長(zhǎng)了,還是轉(zhuǎn)載吧...今天在看博客的時(shí)候,無意中發(fā)現(xiàn)了@Trinea在GitHub上的一個(gè)項(xiàng)目Android開源...
    龐哈哈哈12138閱讀 20,383評(píng)論 3 283
  • 好好想清楚一件事,購(gòu)買他人的時(shí)間真的算是一種個(gè)人商業(yè)模式嗎? 絕大多數(shù)人從未把“花錢購(gòu)買他人的時(shí)間”當(dāng)做自己的個(gè)人...
    萬粒閱讀 291評(píng)論 0 0
  • 每個(gè)Activity都有taskAffinity屬性,這個(gè)屬性指出了它希望進(jìn)入的Task。如果一個(gè)Activity...
    _Ryan閱讀 725評(píng)論 0 0
  • 本書大陸各職業(yè)的分布是這樣的: 靈系師分為:靈士,靈師,靈王,靈帝,靈尊,每個(gè)階級(jí)分為十級(jí)。(系別有,金,木,水,...
    可沅甜閱讀 1,003評(píng)論 0 1

友情鏈接更多精彩內(nèi)容