iOS 语音播放文字内容--制作简易听书软件(AVSpeechSynthesizer)

来源:互联网 发布:酒驾软件 浙江工业大学 编辑:程序博客网 时间:2024/05/17 03:22

iOS 语音播放文字内容--制作简易听书软件(AVSpeechSynthesizer)

字数46 阅读731评论8 喜欢50

Collection/Bookmark/Share for width under 768px


听书.png

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

下面先来一张UI布局图


Snip20160730_3.png

下面附上代码(代码中有详细的注释)

//

//  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) IBOutletUITextView *textView;


/** 语言数组 */

@property (nonatomic,strong) NSArray<AVSpeechSynthesisVoice *> *laungeVoices;


/** 语音合成器 */

@property (nonatomic,strong) AVSpeechSynthesizer *synthesizer;


@end


@implementation FFVoicePlayTextController


- (void)viewDidLoad {

    [super viewDidLoad];


    self.navigationItem.title =@"语音播放文字内容";


}


/***********************************懒加载***********************************/

#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 {


    //创建一个会话

    AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:self.textView.text];


    //选择语言发音的类别,如果有中文,一定要选择中文,要不然无法播放语音

    utterance.voice =self.laungeVoices[2];


    //播放语言的速率,值越大,播放速率越快

    utterance.rate =0.4f;


    //音调  --  为语句指定pitchMultiplier ,可以在播放特定语句时改变声音的音调、pitchMultiplier的允许值一般介于0.5(低音调)和2.0(高音调)之间

    utterance.pitchMultiplier =0.8f;


    //让语音合成器在播放下一句之前有短暂时间的暂停,也可以类似的设置preUtteranceDelay

    utterance.postUtteranceDelay =0.1f;


    //播放语言

    [self.synthesizer speakUtterance:utterance];


}


/** 暂停语音播放/回复语音播放 */

- (IBAction)playAndPauseBtnClick:(UIButton *)sender {


    if (self.synthesizer.isPaused == YES) {//暂停状态

        //继续播放

        [self.synthesizer continueSpeaking];

    }


    else {//现在在播放


        //立即暂停播放

        [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(@"开始播放语音的时候调用");

}


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

    NSLog(@"语音播放结束的时候调用");

}


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

    NSLog(@"暂停语音播放的时候调用");

}


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

    NSLog(@"继续播放语音的时候调用");

}


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

    NSLog(@"取消语音播放的时候调用");

}


- (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",  中文(台湾)  

"[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" )

further readings

0 0
原创粉丝点击