Text to speech: word by word highlighting for iphone

I am making text to speech on UITextViewusing flite-1.4-iphone. When reading text, I want to automatically select text by word.

  • How to synchronize voice with text highlighting while reading?
  • How can I highlight text in a text view?

Here is my current code:

-(IBAction)btnClick:(id)sender
{
    [indicator startAnimating]; 
    textToSpeech = [[TextToSpeech alloc] init]; 
    [textToSpeech setVoice:@"cmu_us_awb"];
    [textToSpeech speakText:txtview.text];
    if ([txtview.text isEqualToString:@""]) 
    {
        [textToSpeech stopTalking];
        [self animate];
    }
}
+5
source share
1 answer

The fleet does not have the opportunity to tie up to find out what the word says. It uses text to create an audio file and simply plays it. You can create your own class for processing information transmitted by TextToSpeech. The class would split the string into separate words and then pass them on to flirt.

fliteTTS.m, talkText: , wav , AVPlayer wav . , , URL- wav ( ).

, , , .

, talkText:

-(NSString *)urlForSpeech:(NSString *)text
 {
    NSMutableString *cleanString;
    cleanString = [NSMutableString stringWithString:@""];
    if([text length] > 1)
    {
            int x = 0;
            while (x < [text length])
            {
                    unichar ch = [text characterAtIndex:x];
                    [cleanString appendFormat:@"%c", ch];
                    x++;
            }
    }
    if(cleanString == nil)
    {       // string is empty
            cleanString = [NSMutableString stringWithString:@""];
    }
    sound = flite_text_to_wave([cleanString UTF8String], voice);

    NSArray *filePaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *recordingDirectory = [filePaths objectAtIndex: 0];
    // Pick a file name
    NSString *tempFilePath = [NSString stringWithFormat: @"%@/%s", recordingDirectory, "temp.wav"];
    // save wave to disk
    char *path;     
    path = (char*)[tempFilePath UTF8String];
    cst_wave_save_riff(sound, path);

    return tempFilePath;      
}

, AVPlayer , :

[textView select:self];
textView.selectedRange = aSelectedRange; 

aSelectedRange - , .

AVPlayer, , Apple . , :

, .

+2

All Articles