如何在传递下一个字符串之前等待iSpeech完成发言

时间:2013-09-19 15:10:26

标签: ios nsoperation nsoperationqueue ispeech

我正在使用iSpeech API。我已经在他们的论坛上发布了,但那里没有太多的活动。我可以传递一个字符串并让应用程序说出来。但现在我想传入一个字符串数组,比如说5.我希望引擎一个接一个地说出这些字符串,但它只说第一个字符串。我决定使用NSOperationQueue,所以在我的viewDidLoad中我做了:

//Create Queue
    queue = [[NSOperationQueue alloc] init];
    [queue setMaxConcurrentOperationCount:1];

然后我这样做:

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    //Set loop for 5 strings
    [self fetchStringsToSpeak:indexPath];
    

    }

    - (无效)fetchStringsToSpeak:(NSIndexPath *)indexPath {

    int stringsToSpeak = 5;
    
    for (int counter = 0; counter < stringsToSpeak; counter++) {
    
        //Get the string
        NSDictionary *currentString = [self.stringsArray objectAtIndex:indexPath.row+counter];
    
        //3. Fill im text and image for cell
        NSString *string = [currentString objectForKey:@"text"];
    
        //Clean the string from URLs
        NSError *error;
        NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error];
        NSArray *matches = [linkDetector matchesInString:string options:0 range:NSMakeRange(0, [string length])];
        NSLog(@"matches %@", matches);
    
        if ([matches count] > 0) {
            for (NSTextCheckingResult *match in matches) {
                if ([match resultType] == NSTextCheckingTypeLink) {
                    NSString *linkToRemove = [[match URL] absoluteString];
                    self.cleanString = [string stringByReplacingOccurrencesOfString:linkToRemove withString:@""];
                    NSLog(@"Clean string: %@", self.cleanString);
    
                }
            }
        } else {
            NSLog(@"NO LINK");
            self.cleanString = string;
        } //END IF/ELSE
    
    NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(speakAString:) object:self.cleanString];
    [queue addOperation:operation];
    
    //Message back to the main thread
    [self performSelectorOnMainThread:@selector(taDah) withObject:nil waitUntilDone:YES];
    
    } //END FOR LOOP
    

    }

    • (void)speakAString:(NSString *)stringToSpeak { ISSpeechSynthesis * synthesis = [[ISSpeechSynthesis alloc] initWithText:stringToSpeak]; NSError *错误; if(![综合说话:&amp; err]){     NSLog(@“ERROR:%@”,错误); } self.cleanString = nil; }

通过以下方法传递值:

它说的是第一个,但后来我明白了:

  

Domain = com.ispeech.ispeechsdk.ErrorDomain Code = 303“SDK已经存在   进行综合或识别。等待之前完成   开始另一个请求。“UserInfo = 0xb54f2b0   {NSLocalizedDescription = SDK已在执行综合或   承认。在开始另一个请求之前等待它完成。}

为什么操作队列不等待?

0 个答案:

没有答案