ios - Wait for AVSpeechSynthesiser to finish utterance -
i'm writing app allows users press button when hear web link want follow.
the problem loop using adds text list of utterances , not wait utterance complete before continuing, meaning can't tell link want follow.
i have class called
speech
which delegate of avspeechsynthesiser , have tried create own way of determining when utterance has concluded:
-(id)init { self = [super init]; if (self) { _synthesiser = [[avspeechsynthesizer alloc]init]; [self setspeaking:no]; } return self; } -(void)outputasspeech:(nsstring *)text { [self setspeaking:yes]; [[self synthesiser]speakutterance:[[avspeechutterance alloc]initwithstring:text]]; } -(bool)isspeaking { return [self speaking]; } -(void)speechsynthesizer:(avspeechsynthesizer *)synthesizer didfinishspeechutterance:(avspeechutterance *)utterance { [self setspeaking:no]; }
and in class
viewcontroller
-(void)readbookmarks { [[self speech]continuespeech]; [[self speech]outputasspeech:@"bookmarks,"]; ([self bookmarkspointer]; [self bookmarkspointer] < [[self bookmarks]count]; _bookmarkspointer++) { nsdictionary* dictionary = [[self bookmarks]objectatindex:[self bookmarkspointer]]; [[self speech]outputasspeech:[dictionary objectforkey:@"title"]]; while ([[self speech]isspeaking]) {} } }
the idea app should wait until utterance has occurred , continue. @ moment reads out "bookmarks," , stops, doesn't read out first bookmark, have tried putting while loop @ beginning of loop.
can me please, appreciate it.
thanks
so after tearing hair out looking answer realised hadn't set synthesizer's delegate 'self'. (i hate myself!)
however, didn't solve problem, reason still didn't work. discovered speechsynthesiser:didfinishspeakingutterance: never being called.
so instead sent speech object array of strings wanted , kept track of them in object, added method return position in array of text being spoken synthesiser:
speech
-(id)init { self = [super init]; if (self) { _synthesiser = [[avspeechsynthesizer alloc]init]; [[self synthesiser]setdelegate:self]; [self setspeaking:no]; } return self; } -(void)outputasspeech:(nsarray*)textarray { [self settexttobespoken:textarray]; [self setarraypointer:0]; [[self synthesiser]speakutterance:[[avspeechutterance alloc]initwithstring:[[self texttobespoken]objectatindex:[self arraypointer]]]]; } -(void)speechsynthesizer:(avspeechsynthesizer *)synthesizer didfinishspeechutterance:(avspeechutterance *)utterance { _arraypointer++; if ([self arraypointer] < [[self texttobespoken]count]) { [[self synthesiser]speakutterance:[[avspeechutterance alloc]initwithstring:[[self texttobespoken]objectatindex:[self arraypointer]]]]; } } -(int)stringbeingspoken { return [self arraypointer]; }
viewcontroller
-(void)readbookmarks { [[self speech]continuespeech]; nsmutablearray* texttospeak = [[nsmutablearray alloc]init]; (int = 0; < [[self bookmarks]count]; i++) { nsdictionary* dictionary = [[self bookmarks]objectatindex:i]; nsstring* texttoread = [dictionary objectforkey:@"title"]; [texttospeak addobject:texttoread]; } [[self speech]outputasspeech:texttospeak]; } -(void)currentlybeingspoken { nsdictionary* dictionary = [[self bookmarks]objectatindex:[[self speech]stringbeingspoken]]; nslog([dictionary objectforkey:@"title"]); }
Comments
Post a Comment