i am using javascript program in http://zaldzbugz.posterous.com/how-to-search-a-string-inside-uiwebview to parse the html page and the following method
-(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);
}
to convert text to speech the string input., i am hearing some encoded format in text field instead of html text (and when i did NSLog(jsstring) i got the whole javascript code and i guess its same in speech output ) .
as i am having very short time to solve , please help me out…
Thanks in advance..
Your code converts the JavaScript file to speech, at no point in your code you load an HTML file, let alone filter text out of it. I also doubt that the JavaScript program you refer to can actually help you achieve that, to me it looks only suitable to find and highlight parts of the text on the page that you already know.
I would recommend you to look into
NSScanner(Apple docs), you can use it to filter the HTML file for the relevant bits without using an external JavaScript program. It will take some fiddling though. If you have perfect HTML, anNSXMLParserwould be a simpler solution, but I am not sure if you can rely on that. There might be ready-made solutions with third-party frameworks, but unfortunately I can’t advise you on this. This question on Stack Exchange might also be helpful: extract the text only from html content in objective CRegardless of the HTML parsing, there are some strange things going on in your code anyway. For example, why do you take
(NSString *) textas a parameter for yourspeakText:method, when you overwritetextfour lines later without ever touching it? And why the cumbersome character-by-character copying oftextintocleanString?