Javascript 是否可以创建html文本作为文本到语音文本字段的字符串输入?

Javascript 是否可以创建html文本作为文本到语音文本字段的字符串输入?,javascript,ios5,xcode4,Javascript,Ios5,Xcode4,我在中使用javascript程序来解析html页面和以下方法 -(void)speakText:(NSString *)text { NSString *filePath = [[NSBundle mainBundle] pathForResource:@"UIWebViewSearch" ofType:@"js" inDirectory:@""]; NSData *fileData = [NSData dataWithContentsOfFile:filePath

我在中使用javascript程序来解析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);

}
-(void)speakText:(NSString*)文本
{
NSString*文件路径=[[NSBundle mainBundle]路径用于资源:@“UIWebViewSearch”,其类型为:@“js”目录:@”“;
NSData*fileData=[NSData dataWithContentsOfFile:filePath];
NSString*jsString=[[NSMutableString alloc]initWithData:fileData编码:NSUTF8StringEncoding];
text=jsString;
NSMutableString*cleanString;
cleanString=[NSMutableString stringWithString:@”“];
如果([文本长度]>1)
{
int x=0;
而(x<[文本长度])
{
unichar ch=[文本字符索引:x];
[cleanString appendFormat:@“%c”,ch];
x++;
}
}
if(cleanString==nil)
{//字符串为空
cleanString=[NSMutableString stringWithString:@”“];
}
声音=从文本到波浪([cleanString UTF8String],声音);
NSArray*FilePath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString*recordingDirectory=[FilePath对象索引:0];
//选择一个文件名
NSString*tempFilePath=[NSString stringWithFormat:@“%@/%s”,recordingDirectory,“temp.wav”];
//将wave保存到磁盘
字符*路径;
path=(char*)[tempFilePath UTF8String];
cst_wave_save_riff(声音、路径);
//把声音放回去。
n错误*错误;
[音频播放器停止];
audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:tempFilePath]错误:&err];
[音频播放器设置代表:自我];
//[音频播放器准备播放];
[音频播放器播放];
//删除文件
[[NSFileManager defaultManager]removeItemAtPath:tempFilePath错误:nil];
删除_波(声音);
}
为了将文本转换为语音字符串输入,我在文本字段中听到了一些编码格式,而不是html文本(当我使用NSLog(jsstring)时,我得到了整个javascript代码,我猜在语音输出中也是如此)。 由于我有很短的时间来解决,请帮助我。。。
提前谢谢

您的代码将JavaScript文件转换为语音,在代码中没有加载HTML文件,更不用说从中过滤文本了。我也怀疑你提到的JavaScript程序是否真的能帮助你实现这一点,在我看来,它只适合在页面上查找并突出显示你已经知道的部分文本

我建议您查看
NSScanner
(),您可以使用它过滤HTML文件中的相关位,而无需使用外部JavaScript程序。不过这需要一些摆弄。如果您有完美的HTML,
NSXMLParser
将是一个更简单的解决方案,但我不确定您是否可以依赖它。第三方框架可能有现成的解决方案,但不幸的是,我不能就此向您提供建议。有关堆栈交换的此问题也可能有帮助:


不管HTML解析如何,代码中总会出现一些奇怪的事情。例如,当您以后覆盖
文本
四行而不触摸它时,为什么要将
(NSString*)text
作为
speakText:
方法的参数?为什么要将
文本
逐字符复制到
干净字符串

谢谢Stefan。我并没有加载html页面,javascript根本不需要。