实现播报汉语文字,用系统AVSpeechSynthesizer类来实现
来源:互联网 发布:h3c交换机端口加vlan 编辑:程序博客网 时间:2024/06/14 03:02
一、简单说明
(1)
在iOS7之前,想要实现语音播报文字内容,可能需要第三方资源库来实现。现在在iOS7上,系统为我们提供了语音播报文字的功能,我们不仅可以播报英语内容,也可以播报汉语文字
实现TTS主要依赖AVSpeechSynthesizer,AVSpeechUtterance,AVSpeechSynthesisVoice,要使用这些类必须先加入
AVFoundation框架:
AVSpeechSynthesisVoice:用来配置发音,支持的发音非常多.个人感觉台湾发音最好听~通过调用
[AVSpeechSynthesisVoicespeechVoices]类方法可用看到支持的发音种类;
AVSpeechUtterance:这个类就是用来将字符串合成为语音对象提供给AVSpeechSynthesizer来播放,这个类还有一些
实例方法用来控制语速,音调等等。。
实现代码:
TXSoundPlayer.h文件代码:
1 // Created by 鑫 on 14/12/23. 2 // Copyright (c) 2014年 梁镋鑫. All rights reserved. 3 // 4 5 #import <Foundation/Foundation.h> 6 #import <AVFoundation/AVFoundation.h> 7 8 @interface TXSoundPlayer : NSObject 9 {10 NSMutableDictionary* soundSet; //声音设置11 NSString* path; //配置文件路径12 }13 14 @property(nonatomic,assign)float rate; //语速15 @property(nonatomic,assign)float volume; //音量16 @property(nonatomic,assign)float pitchMultiplier; //音调17 @property(nonatomic,assign)BOOL autoPlay; //自动播放18 19 20 +(TXSoundPlayer*)soundPlayerInstance;21 22 -(void)play:(NSString*)text;23 24 -(void)setDefault;25 26 -(void)writeSoundSet;27 28 @end
TXSoundPlayer.m文件代码:
1 // 2 // Created by 鑫 on 14/12/23. 3 // Copyright (c) 2014年 梁镋鑫. All rights reserved. 4 // 5 6 #import "TXSoundPlayer.h" 7 8 static TXSoundPlayer* soundplayer=nil; 9 10 @implementation TXSoundPlayer11 12 +(TXSoundPlayer*)soundPlayerInstance13 {14 if(soundplayer==nil)15 {16 soundplayer=[[TXSoundPlayer alloc]init];17 [soundplayer initSoundSet];18 }19 return soundplayer;20 }21 22 //播放声音23 -(void)play:(NSString*)text24 {25 if(![text isEqualToString:NULL])26 {27 AVSpeechSynthesizer* player=[[AVSpeechSynthesizer alloc]init];28 AVSpeechUtterance* u=[[AVSpeechUtterance alloc]initWithString:text];//设置要朗读的字符串29 u.voice=[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];//设置语言30 u.volume=self.volume; //设置音量(0.0~1.0)默认为1.031 u.rate=self.rate; //设置语速32 u.pitchMultiplier=self.pitchMultiplier; //设置语调33 [player speakUtterance:u];34 }35 }36 37 //初始化配置38 -(void)initSoundSet39 {40 path=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"SoundSet.plist"];41 soundSet=[NSMutableDictionary dictionaryWithContentsOfFile:path];42 if(soundSet==nil)43 {44 soundSet=[NSMutableDictionary dictionary];45 [soundplayer setDefault];46 [soundplayer writeSoundSet];47 }48 else49 {50 self.autoPlay=[[soundSet valueForKeyPath:@"autoPlay"] boolValue];51 self.volume=[[soundSet valueForKeyPath:@"volume"] floatValue];52 self.rate=[[soundSet valueForKeyPath:@"rate"] floatValue];53 self.pitchMultiplier=[[soundSet valueForKeyPath:@"pitchMultiplier"] floatValue];54 }55 }56 57 //恢复默认设置58 -(void)setDefault59 {60 self.volume=0.7;61 self.rate=0.166;62 self.pitchMultiplier=1.0;63 }64 65 //将设置写入配置文件66 -(void)writeSoundSet67 {68 [soundSet setValue:[NSNumber numberWithBool:self.autoPlay] forKey:@"autoPlay"];69 [soundSet setValue:[NSNumber numberWithFloat:self.volume] forKey:@"volume"];70 [soundSet setValue:[NSNumber numberWithFloat:self.rate] forKey:@"rate"];71 [soundSet setValue:[NSNumber numberWithFloat:self.pitchMultiplier] forKey:@"pitchMultiplier"];72 [soundSet writeToFile:path atomically:YES];73 }74 75 @end
(2)
上面的代码节选自我项目中的部分代码,功能已经封装好工具类了,所以想要实现该功能,例如warnmsg = @"今天天气真好,工作加油";来测试一下。
NSString * dayweather =@"据警方了解经济,复苏减肥了就散了就是垃圾手机发了睡觉了时间就是犯贱手机发了就是就是,哦就哦时间就是就是凤凰山风景时间飞逝减肥接送"; TXSoundPlayer* sound=[TXSoundPlayer soundPlayerInstance]; [sound play:dayweather];
注意:在iOS7上运行能正常播放声音,而在iOS8上由于模拟器问题不能整成播放,在真机上才能正常播报,坑爹的模拟器,害得我看了那没文档。
原文链接:http://www.cnblogs.com/asd5551680/p/4180556.html
0 0
- 实现播报汉语文字,用系统AVSpeechSynthesizer类来实现
- iOS系统语音播报文字
- 用css来实现自动截取文字
- CSS来实现用图片替换文字
- Android 语音播报实现
- 使用AVSpeechSynthesizer朗读文字
- 文字转语音AVSpeechSynthesizer
- ios 文本转语音 播报 AVSpeechSynthesizer
- 汉语著者号自动生成系统的设计与实现
- java实现语音播报功能
- 使用java实现语音播报
- 用css来实现文字描边效果
- 用C#winform来实现文字的滚动
- 用jQuery写扩展插件实现语音播报
- 借助BMFont来实现图片变文字
- 系统播报
- 前端页面实现语音播报功能
- android 滚动播报效果的实现
- JSON 数据格式详解
- 关联式容器map/multimap
- iOS OC KVC KVO 实现原理详解
- java深拷贝与浅拷贝
- 几个跳转语句的介绍:break语句,continue语句,return语句。
- 实现播报汉语文字,用系统AVSpeechSynthesizer类来实现
- 委托模式
- Servlet3.0(二)--使用注解标注过滤器(Filter)@WebFilter
- Failed to read artifact descriptor for org.apache.maven.plugins:maven-jar-plugin:jar:2.5
- golang继承,和多态
- 基于H5的移动端贪吃蛇游戏开发
- 探究CSS box-shadow属性
- 关联式容器set和map原理
- mysql对表的操作