【问题标题】:Text to speech: highlighting word by word for iphone文本到语音:逐字突出显示 iphone
【发布时间】:2012-08-13 00:56:06
【问题描述】:

我正在使用 flite-1.4-iphone 在UITextView 上进行文字转语音。在阅读文本时,我想自动逐字突出显示文本。

  1. 如何在阅读时将语音与文本突出显示同步?
  2. 如何在文本视图中突出显示文本?

这是我当前的代码:

-(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];
    }
}

【问题讨论】:

    标签: ios uitextview text-to-speech flite


    【解决方案1】:

    Flite 无法绑定来查看何时说出了什么单词。它所做的是使用文本生成音频文件并播放它。 您可以创建一个自定义类来处理传递给 TextToSpeech 的信息。该类会将字符串分成单独的单词,然后将它们传递给 flite。

    如果你查看 fliteTTS.m,在 speakText: 方法中,你可以看到它创建了一个 wav 文件,然后让 AVPlayer 播放 wav 文件。您可以做的是更改该方法,以便将 wav 文件的 URL 返回到您的自定义类(可以将它们保存在数组中),而不是播放文件。

    然后让自定义类按顺序播放声音,每次播放下一个剪辑时,突出显示新的文本部分。

    所以代替 speakText:

    -(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 不熟悉,因此无法真正帮助您进行设置,但在苹果的开发者网站上有一些非常好的示例。这是你应该看的:link

    完成后不要忘记删除音频文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-24
      • 2020-04-16
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多