AVFoundation汉子转语音
2015-11-26 16:31
148 查看
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
@interface TXSoundPlayer :
NSObject
{
NSMutableDictionary* soundSet;
//声音设置
NSString* path; //配置文件路径
}
@property(nonatomic,assign)float rate;
//语速
@property(nonatomic,assign)float volume;
//音量
@property(nonatomic,assign)float pitchMultiplier;
//音调
@property(nonatomic,assign)BOOL autoPlay;
//自动播放
+(TXSoundPlayer*)soundPlayerInstance;
-(void)play:(NSString*)text;
-(void)setDefault;
-(void)writeSoundSet;
@end
static
TXSoundPlayer* soundplayer=nil;
@implementation TXSoundPlayer
+(TXSoundPlayer*)soundPlayerInstance
{
if(soundplayer==nil)
{
soundplayer=[[TXSoundPlayer
alloc]init];
[soundplayer
initSoundSet];
}
return
soundplayer;
}
//播放声音
-(void)play:(NSString*)text
{
if(![text isEqualToString:NULL])
{
AVSpeechSynthesizer* player=[[AVSpeechSynthesizer
alloc]init];
AVSpeechUtterance* u=[[AVSpeechUtterance
alloc]initWithString:text];//设置要朗读的字符串
u.voice=[AVSpeechSynthesisVoice
voiceWithLanguage:@"zh-CN"];//设置语言
u.volume=self.volume;
//设置音量(0.0~1.0)默认为1.0
u.rate=self.rate;
//设置语速
u.pitchMultiplier=self.pitchMultiplier;
//设置语调
[player
speakUtterance:u];
}
}
//初始化配置
-(void)initSoundSet
{
path=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
lastObject] stringByAppendingPathComponent:@"SoundSet.plist"];
soundSet=[NSMutableDictionary
dictionaryWithContentsOfFile:path];
if(soundSet==nil)
{
soundSet=[NSMutableDictionary
dictionary];
[soundplayer
setDefault];
[soundplayer
writeSoundSet];
}
else
{
self.autoPlay=[[soundSet
valueForKeyPath:@"autoPlay"]
boolValue];
self.volume=[[soundSet
valueForKeyPath:@"volume"]
floatValue];
self.rate=[[soundSet
valueForKeyPath:@"rate"]
floatValue];
self.pitchMultiplier=[[soundSet
valueForKeyPath:@"pitchMultiplier"]
floatValue];
}
}
//恢复默认设置
-(void)setDefault
{
self.volume=0.7;
self.rate=0.166;
self.pitchMultiplier=2.0;
}
//将设置写入配置文件
-(void)writeSoundSet
{
[soundSet
setValue:[NSNumber
numberWithBool:self.autoPlay]
forKey:@"autoPlay"];
[soundSet
setValue:[NSNumber
numberWithFloat:self.volume]
forKey:@"volume"];
[soundSet
setValue:[NSNumber
numberWithFloat:self.rate]
forKey:@"rate"];
[soundSet
setValue:[NSNumber
numberWithFloat:self.pitchMultiplier]
forKey:@"pitchMultiplier"];
[soundSet
writeToFile:path
atomically:YES];
}
@end
#import <AVFoundation/AVFoundation.h>
@interface TXSoundPlayer :
NSObject
{
NSMutableDictionary* soundSet;
//声音设置
NSString* path; //配置文件路径
}
@property(nonatomic,assign)float rate;
//语速
@property(nonatomic,assign)float volume;
//音量
@property(nonatomic,assign)float pitchMultiplier;
//音调
@property(nonatomic,assign)BOOL autoPlay;
//自动播放
+(TXSoundPlayer*)soundPlayerInstance;
-(void)play:(NSString*)text;
-(void)setDefault;
-(void)writeSoundSet;
@end
static
TXSoundPlayer* soundplayer=nil;
@implementation TXSoundPlayer
+(TXSoundPlayer*)soundPlayerInstance
{
if(soundplayer==nil)
{
soundplayer=[[TXSoundPlayer
alloc]init];
[soundplayer
initSoundSet];
}
return
soundplayer;
}
//播放声音
-(void)play:(NSString*)text
{
if(![text isEqualToString:NULL])
{
AVSpeechSynthesizer* player=[[AVSpeechSynthesizer
alloc]init];
AVSpeechUtterance* u=[[AVSpeechUtterance
alloc]initWithString:text];//设置要朗读的字符串
u.voice=[AVSpeechSynthesisVoice
voiceWithLanguage:@"zh-CN"];//设置语言
u.volume=self.volume;
//设置音量(0.0~1.0)默认为1.0
u.rate=self.rate;
//设置语速
u.pitchMultiplier=self.pitchMultiplier;
//设置语调
[player
speakUtterance:u];
}
}
//初始化配置
-(void)initSoundSet
{
path=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
lastObject] stringByAppendingPathComponent:@"SoundSet.plist"];
soundSet=[NSMutableDictionary
dictionaryWithContentsOfFile:path];
if(soundSet==nil)
{
soundSet=[NSMutableDictionary
dictionary];
[soundplayer
setDefault];
[soundplayer
writeSoundSet];
}
else
{
self.autoPlay=[[soundSet
valueForKeyPath:@"autoPlay"]
boolValue];
self.volume=[[soundSet
valueForKeyPath:@"volume"]
floatValue];
self.rate=[[soundSet
valueForKeyPath:@"rate"]
floatValue];
self.pitchMultiplier=[[soundSet
valueForKeyPath:@"pitchMultiplier"]
floatValue];
}
}
//恢复默认设置
-(void)setDefault
{
self.volume=0.7;
self.rate=0.166;
self.pitchMultiplier=2.0;
}
//将设置写入配置文件
-(void)writeSoundSet
{
[soundSet
setValue:[NSNumber
numberWithBool:self.autoPlay]
forKey:@"autoPlay"];
[soundSet
setValue:[NSNumber
numberWithFloat:self.volume]
forKey:@"volume"];
[soundSet
setValue:[NSNumber
numberWithFloat:self.rate]
forKey:@"rate"];
[soundSet
setValue:[NSNumber
numberWithFloat:self.pitchMultiplier]
forKey:@"pitchMultiplier"];
[soundSet
writeToFile:path
atomically:YES];
}
@end
相关文章推荐
- 网页设计中常用的20个Web安全字体
- 有关get_global/local_id/size
- playframework--控制器
- Node.js—Node模块机制
- AlertDialog(警示/消息对话框)
- 表的cell的高度自适应
- android 客户端 RSA加密 要注意的问题
- Nginx-openresty+mysql+redis使用篇(一)
- 编码转换函数
- 代码插入测试
- Sping Security-授权
- 字符串比较问题
- codelity刷题---MinAvgTwoSlice
- <11>Android HAL 处理硬件设备访问权限问题
- 【Android】获取手机中已安装apk文件信息(PackageInfo、ResolveInfo)(应用图片、应用名、包名等)
- js知识点 (转载 学习中)
- oracle 存储过程详细介绍(创建,删除存储过程,参数传递等)1
- 取出数据库表里面同一类型最新的一条数据
- centos网卡错误Device eth0 does not seem to be present
- Swift语言迈出侵蚀Androidd第一步