Mac OS X 文本转语音性别
我正在制作一个应用程序,在其中我试图获取 Mac OS X 中的所有不同声音,然后按性别对它们进行排序。我创建了三个可变数组来放入每种性别(男性、女性、新奇)的声音,并且我使用枚举来遍历每个数组并将其放入正确的数组中。不幸的是,它不起作用。除了新奇阵列之外,所有的东西都是空的,而新奇阵列只有一个声音,Zarvox。有人看到我做错了什么吗?我发布了下面的代码:
NSArray* voices = [NSSpeechSynthesizer availableVoices];
for(NSString* x in voices){
NSDictionary* voiceInfo = [NSSpeechSynthesizer attributesForVoice:x];
NSString* voiceName = [voiceInfo objectForKey:NSVoiceName];
NSString* voiceGender = [voiceInfo objectForKey:NSVoiceGender];
maleVoices = [[NSMutableArray alloc] init];
femaleVoices = [[NSMutableArray alloc] init];
noveltyVoices = [[NSMutableArray alloc] init];
if (voiceGender == NSVoiceGenderMale){
[maleVoices addObject:voiceName];
} else if (voiceGender == NSVoiceGenderFemale) {
[femaleVoices addObject:voiceName];
} else {
[noveltyVoices addObject:voiceName];
}
}
I'm making an application, in which I am trying to get all the different voices in Mac OS X, and then sort them by gender. I've created three mutable arrays to put the voices of each gender (Male, Female, Novelty) in, and I'm using enumeration to go through each one and put it in the correct array. Unfortunately, It's not working. All but the novelty arrays come up empty, and the novelty array only has one voice, Zarvox. Does anybody see what I'm doing wrong? I've posted the code below:
NSArray* voices = [NSSpeechSynthesizer availableVoices];
for(NSString* x in voices){
NSDictionary* voiceInfo = [NSSpeechSynthesizer attributesForVoice:x];
NSString* voiceName = [voiceInfo objectForKey:NSVoiceName];
NSString* voiceGender = [voiceInfo objectForKey:NSVoiceGender];
maleVoices = [[NSMutableArray alloc] init];
femaleVoices = [[NSMutableArray alloc] init];
noveltyVoices = [[NSMutableArray alloc] init];
if (voiceGender == NSVoiceGenderMale){
[maleVoices addObject:voiceName];
} else if (voiceGender == NSVoiceGenderFemale) {
[femaleVoices addObject:voiceName];
} else {
[noveltyVoices addObject:voiceName];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
for
循环外部分配maleVoices
、femaleVoices
和noveltyVoices
。您只是在循环的每次迭代中创建一个新的空数组。Allocate
maleVoices
,femaleVoices
, andnoveltyVoices
outside of thefor
loop. You're just creating a new, empty array with each iteration of the loop.与字符串直接进行相等比较通常是不可靠的。使用
-isEqualToString:
方法:Direct equality comparison with strings is usually unreliable. Use the
-isEqualToString:
method: