我已经在我的项目中实现了 textToSpeech 并希望在说出文本时显示警报视图。这里我调用 textToSpeech 的方法:
//-----before TTS starts i try to display alertView with Cancelbutton
//[self performSelectorOnMainThread:@selector(alertWhileTTS) withObject:nil waitUntilDone:YES]; //gray view and no alertview
//[self performSelector:@selector(alertWhileTTS)]; //gray view and no alertview
//[self alertWhileTTS]; //gray view and no alertview
//this part here is blocking, no gray screen,
//after TTS is ready, the alertView is displayed
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI if you have to
[self alertWhileTTS];
});
[[self view] setNeedsDisplay];
[self synthesizeInBackground];
[queue waitUntilAllOperationsAreFinished];
[self setIsSpeaking: false];
[[self view] setNeedsDisplay];
这里的 synthesizeInBackground 方法(in 方法 synthesize
启动 TTS):
- (void) synthesizeInBackground {
queue = [[NSOperationQueue alloc] init];
operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(synthesize) object:nil];
[queue addOperation: operation];
}
虽然 TTS 我想显示一个带有cancel
按钮的 alertView。但在我的情况下,我只得到一个没有 alertView 的灰屏。
如何正确调用 alertWhileTTS,以便显示 alertView?
这里是 alertWhileTTS 的内容:
- (void) alertWhileTTS {
UIAlertView *inboxRead = [[[UIAlertView alloc] initWithTitle:@"Inbox tts..."
message:nil
delegate:self
cancelButtonTitle:@"Abbrechen"
otherButtonTitles:nil] autorelease];
inboxRead.tag = 997;
[inboxRead show];
}
更新查看我的解决方案,该解决方案有效:
[self performSelectorOnMainThread:@selector(alertWhileTTS) withObject:nil waitUntilDone:YES];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, (unsigned long)NULL), ^(void) {
[[self view] setNeedsDisplay];
[self synthesizeInBackground];
[queue waitUntilAllOperationsAreFinished];
[self setIsSpeaking: false];
[[self view] setNeedsDisplay];
});