开发者

Mac OS X Text-To-Speech Genders

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];
    开发者_C百科noveltyVoices = [[NSMutableArray alloc] init];

    if (voiceGender == NSVoiceGenderMale){
        [maleVoices addObject:voiceName];

    } else if (voiceGender == NSVoiceGenderFemale) {
        [femaleVoices addObject:voiceName];
    } else {

        [noveltyVoices addObject:voiceName];
    }
}


Allocate maleVoices, femaleVoices, and noveltyVoices outside of the for loop. You're just creating a new, empty array with each iteration of the loop.


Direct equality comparison with strings is usually unreliable. Use the -isEqualToString: method:

if([voiceGender isEqualToString:NSVoiceGenderMale]){
    // etc.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜