1

我正在使用 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];
    

    }

    -(void)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(![synthesis speak:&err]) { NSLog(@"ERROR: %@", err); } self.cleanString = nil; }

它通过以下方法传递值:

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

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

为什么操作队列不等待?

4

0 回答 0