2012-05-29 31 views
0

我使用的JavaScript程序http://zaldzbugz.posterous.com/how-to-search-a-string-inside-uiwebview來解析HTML頁面,按照下述方式是否可以創建html文本作爲文本到語音文本字段的字符串輸入?

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


    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"UIWebViewSearch" ofType:@"js" inDirectory:@""]; 
    NSData *fileData = [NSData dataWithContentsOfFile:filePath]; 
    NSString *jsString = [[NSMutableString alloc] initWithData:fileData encoding:NSUTF8StringEncoding]; 
    text =jsString; 
    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); 
    // Play the sound back. 
    NSError *err; 
    [audioPlayer stop]; 
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:tempFilePath] error:&err]; 
    [audioPlayer setDelegate:self]; 
    //[audioPlayer prepareToPlay]; 
    [audioPlayer play]; 
    // Remove file 
    [[NSFileManager defaultManager] removeItemAtPath:tempFilePath error:nil]; 
    delete_wave(sound); 

} 

將文本轉換爲語音的字符串輸入。,我聽到的文本字段,而不是HTML文本的一些編碼格式(和當我做NSLog(jsstring)時,我得到了整個javascript代碼,我猜它在語音輸出中是一樣的)。 因爲我有很短的時間來解決,請幫助我... 在此先感謝..

回答

1

您的代碼將JavaScript文件轉換爲語音,在您的代碼中沒有任何一點加載HTML文件,讓獨立過濾文本。我也懷疑你所指的JavaScript程序實際上可以幫助你實現這一目標,對我而言,它看起來只適合查找和突出顯示已知頁面上的文本部分。

我建議你看看NSScannerApple docs),你可以使用它來過濾相關位的HTML文件,而無需使用外部JavaScript程序。雖然這將需要一些擺弄。如果您有完美的HTML,NSXMLParser將是一個更簡單的解決方案,但我不確定您是否可以依賴該解決方案。可能有第三方框架的現成解決方案,但不幸的是,我不能就此提供建議。 Stack Exchange上的這個問題可能也有幫助:extract the text only from html content in objective C

不管HTML解析如何,代碼中都會出現一些奇怪的現象。例如,爲什麼當您在以後四行覆蓋text而沒有觸及它時,爲什麼要將(NSString *) text作爲speakText:方法的參數?爲什麼繁瑣的text字符複製到cleanString

+0

謝謝你斯蒂芬。我沒有加載HTML頁面,並沒有必要在必要的JavaScript。 – iMeMyself

相關問題