0

我正在使用 sfoster iPhone TTS。我想将文本转换为语音,还想将字符串拆分为字母,并且文本和每个字母都将发音。但它没有发音文本和字母。我的代码如下:

-(void)spellOut:(NSString *)text{

[fliteEngine speakText:text];   
[fliteEngine setPitch:100.0 variance:50.0 speed:2.5];
[fliteEngine setVoice:@"cmu_us_rms"];

/* split a string into an array */

NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[text length]];

for (int i=0; i < [text length]; i++) {
    NSString *ichar  = [NSString stringWithFormat:@"%c", [text characterAtIndex:i]];
    [characters addObject:ichar];
}

/* set letter from each index of the array */

for (int i=0; i<[characters count]; i++) {

    NSString *letter = [characters objectAtIndex:i];

    NSLog(@"%@\n",letter);

    [fliteEngine speakText:letter];
    [fliteEngine setPitch:100.0 variance:50.0 speed:2.5];
    [fliteEngine setVoice:@"cmu_us_rms"];   
  }

}

问题出在哪里?

提前致谢。

4

3 回答 3

1

首先,您似乎尚未启动 FliteTTS,因此请先执行此操作

fliteEngine = [[FliteTTS alloc] init];

然后,正如我通过您的代码了解到的那样,如果您使用此代码而不单独发音所有字符,它就可以工作。

-(void)spellOut:(NSString *)text{

[fliteEngine speakText:text];
[fliteEngine setPitch:100.0 variance:50.0 speed:2.5];
[fliteEngine setVoice:@"cmu_us_rms"];

/* split a string into an array */

NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[text length]];

for (int i=0; i < [text length]; i++) 
 {
    NSString *ichar  = [NSString stringWithFormat:@"%c", [text characterAtIndex:i]];
    [characters addObject:ichar];
}


}

希望它可以帮助你。

于 2013-04-30T09:18:56.590 回答
0
- (void)viewDidLoad
{


 @try {

    [super viewDidLoad];

    self.wordToSpeech = @"What do functional foods mean? According to the April 2009 position on functional foods by the American Dietetic Association (ADA), all foods are functional at some level, because they provide nutrients that furnish energy, sustain growth, or maintain and repair vital processes. While the functional food category, per se, is not officially recognized by the Food and Drug Administration, the ADA considers functional foods to be whole foods and fortified, enriched, or enhanced foods that have a potentially beneficial effect on health. Thus a list of functional foods might be as varied as nuts, calcium-fortified orange juice, energy bars, bottled teas and gluten-free foods. While many functional foods—from whole grain breads to wild salmon—provide obvious health benefits, other functional foods like acai berry or brain development foods may make overly optimistic promises. Thus, it’s important to evaluate each functional food on the basis of scientific evidence before you buy into their benefits";

    [self sentenceToSpeech];
}
@catch (NSException *exception)
{
    NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]);
}

}

- (void)sentenceToSpeech{

@try {

    NSString *sentenceToSpeech = @"";

    if (0 == self.totalCountPlayed) {

        sentenceToSpeech = [self.wordToSpeech substringToIndex:100];
        self.totalCountPlayed = 100;

    }
    else{

        NSString *tempString = [self.wordToSpeech substringFromIndex:self.totalCountPlayed];

        if (100 <= [tempString length]) {

            sentenceToSpeech = [tempString substringToIndex:100];
            self.totalCountPlayed += 100;
        }
        else
        {
            sentenceToSpeech = tempString;
            self.totalCountPlayed = 0;
        }
    }

    [self speechUsingGoogleTTS:sentenceToSpeech];
}
@catch (NSException *exception)
{
    NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]);
}

}

- (void)speechUsingGoogleTTS:(NSString *)sentenceToSpeeh{

@try {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"file.mp3"];

    NSString *urlString = [NSString stringWithFormat:@"http://www.translate.google.com/translate_tts?tl=en&q=%@",sentenceToSpeeh];
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url] ;
    [request setValue:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1" forHTTPHeaderField:@"User-Agent"];
    NSURLResponse* response = nil;
    NSError* error = nil;
    NSData* data = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:&response
                                                     error:&error];
    [data writeToFile:path atomically:YES];


    NSError        *err;
    if ([[NSFileManager defaultManager] fileExistsAtPath:path])
    {
        self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:
                       [NSURL fileURLWithPath:path] error:&err];

        [self.player prepareToPlay];
        [self.player setNumberOfLoops:0];
        [self.player setDelegate:self];
        [self.player play];

    }
}
@catch (NSException *exception)
{
    NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]);
}
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{

@try {

    if (0 != self.totalCountPlayed) {

        [self sentenceToSpeech];
    }
}
@catch (NSException *exception)
{
    NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]);
}
}

使用此代码将起作用。

于 2013-04-30T09:12:17.097 回答
0

您可以使用计时器拼写文本,

.h

NSString *speechText;
int tempVariable;

.m

-(void)spellOut:(NSString *)text{

    speechText=[NSString stringWithFormat:@"%@",text];

    tempVariable=0;

    [fliteEngine speakText:text];   
    [fliteEngine setPitch:100.0 variance:50.0 speed:2.5];
    [fliteEngine setVoice:@"cmu_us_rms"];

    NSTimer *popupTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    NSRunLoop *runner = [NSRunLoop currentRunLoop];
    [runner addTimer:popupTimer forMode: NSDefaultRunLoopMode];
}

-(void)timerAction:(NSTimer *)theTimer {

    if(tempVariable<speechText.length) {

        NSString *letter = [NSString stringWithFormat:@"%c",[speechText characterAtIndex:tempVariable]];

        NSLog(@"%@\n",letter);

        [fliteEngine speakText:letter];
        [fliteEngine setPitch:100.0 variance:50.0 speed:2.5];
        [fliteEngine setVoice:@"cmu_us_rms"];
        tempVariable++;
    } else {

        [theTimer invalidate];
        theTimer=nil;
    }

}
于 2013-04-30T09:29:17.493 回答