语音播报文字

首先,项目要导入AVFoundation框架,然后常用到的类:

AVSpeechSynthesisVoice:用来配置发音,支持的发音非常多,这个会放在附录中展示出来

AVSpeechSynthesizer:实例一个播放器

AVSpeechUtterance:这个类的作用是将字符串合成为语音对象提供给AVSpeechSynthesizer来播放,语音播放的速度、音量、音调等都是在这设置的

 

****简单介绍完毕,上代码****

在.h文件中:

//  Copyright © 2016年 danson. All rights reserved.

 

#import <Foundation/Foundation.h>

#import <AVFoundation/AVFoundation.h>

 

 @interface SDSoundPlayer : NSObject

 

@property(nonatomic,assign)float rate;   //语速

@property(nonatomic,assign)float volume; //音量

@property(nonatomic,assign)float pitchMultiplier;  //音调

@property(nonatomic,assign)BOOL  autoPlay;  //自动播放

 

 //类方法实例出对象

+(SDSoundPlayer *)SDSoundPlayerInit;

//基础设置,如果有别的设置,也很好实现

-(void)setDefaultWithVolume:(float)aVolume rate:(CGFloat)aRate pitchMultiplier:(CGFloat)aPitchMultiplier;

 //播放并给出文字

-(void)play:(NSString *)string;

 

@end

 

在.m文件中:

//  Copyright © 2016年 danson. All rights reserved.

 

#import "SDSoundPlayer.h"

 static SDSoundPlayer *soundplayer = nil;

@implementation SDSoundPlayer

 

+(SDSoundPlayer *)SDSoundPlayerInit

{

    if(soundplayer == nil)

    {

        soundplayer = [[SDSoundPlayer alloc]init];

        [soundplayer setDefaultWithVolume:-1.0 rate:-1.0 pitchMultiplier:-1.0];

    }

    return soundplayer;

}

 

//播放声音

-(void)play:(NSString *)string

{

    if(string && string.length > 0){

        

        AVSpeechSynthesizer *player  = [[AVSpeechSynthesizer alloc]init];

        AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:string];//设置语音内容

        utterance.voice  = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];//设置语言

        utterance.rate   = self.rate;  //设置语速

        utterance.volume = self.volume;  //设置音量(0.0~1.0)默认为1.0

        utterance.pitchMultiplier    = self.pitchMultiplier;  //设置语调 (0.5-2.0)

        utterance.postUtteranceDelay = 1; //目的是让语音合成器播放下一语句前有短暂的暂停

        [player speakUtterance:utterance];

    }

}

 

//初始化配置

/**

 *  设置播放的声音参数 如果选择默认请传入 -1.0

 *

 *  @param aVolume          音量(0.0~1.0)默认为1.0

 *  @param aRate            语速(0.0~1.0)

 *  @param aPitchMultiplier 语调 (0.5-2.0)

 */

-(void)setDefaultWithVolume:(float)aVolume rate:(CGFloat)aRate pitchMultiplier:(CGFloat)aPitchMultiplier

{

    self.rate   = aRate;

    self.volume = aVolume;

    self.pitchMultiplier = aPitchMultiplier;

 

    if (aRate == -1.0) {

        self.rate = 1;

    }

    if (aVolume == -1.0) {

        self.volume = 1;

    }

    if (aPitchMultiplier == -1.0) {

        self.pitchMultiplier = 1;

    }

}

 @end

 

使用的例子:

    SDSoundPlayer *player = [SDSoundPlayer SDSoundPlayerInit];

    [player setDefaultWithVolume:-1.0 rate:0.4 pitchMultiplier:-1.0];

    [player play:@"要播放的文字"];

就是这么随意 

 

 

附录:AVSpeech支持的语言种类

"[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", 
"[AVSpeechSynthesisVoice0x978b210]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" )

对照中文(不是一一对应关系)

中文(简体中文) 
阿拉伯语(沙特阿拉伯) 
中文(普通话) 
中文(香港) 
中文(台湾) 
捷克 
丹麦 
荷兰语(比利时) 
荷兰语 
英语(澳大利亚) 
英语(英国) 
英语(爱尔兰) 
英语(美国) 
英语(南非) 
芬兰语 
法语(加拿大) 
法语(标准) 
德语(标准) 
希腊语 
北印度语 
匈牙利语 
印尼语 
意大利语(标准) 
日语 
韩语 
挪威语 
波兰语 
葡萄牙语(巴西) 
葡萄牙语(葡萄牙) 
罗马尼亚语 
俄语 
斯洛伐克语 
西班牙语 
西班牙语(墨西哥) 
瑞典语 
泰语 
土耳其语


转自 http://www.cnblogs.com/danson23/p/5509256.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值