文本到语音不起作用

时间:2013-04-30 08:45:43

标签: iphone ios text-to-speech

我正在使用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"];   
  }

}

问题出在哪里?

提前致谢。

3 个答案:

答案 0 :(得分: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];
}


}

希望它对你有所帮助。

答案 1 :(得分: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]);
}
}

使用此代码即可。

答案 2 :(得分: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;
    }

}
相关问题