等待AVSpeechSynthesiser完成话语

时间:2015-04-14 19:07:15

标签: ios avspeechsynthesizer

我正在编写一个应用,允许用户在听到他们想要关注的网络链接时按下按钮。

问题在于我正在使用的for循环将所有文本添加到话语列表中,并且在继续之前不等待话语完成,这意味着我无法分辨他们想要遵循的链接。 / p>

我有一个名为

的班级
  

语音

这是AVSpeechSynthesiser的代表,并试图创建我自己的方式来确定话语何时结束:

-(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];
}

在课堂上

  

的viewController

-(void)readBookmarks
{
[[self speech]continueSpeech];
[[self speech]outputAsSpeech:@"Bookmarks,"];
for ([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]) {}
    }
}

这个想法是应用程序应该等到话语发生后再继续。但是目前它读出“书签”并停止,它甚至没有读出第一个书签,我也尝试将while循环放在for循环的开头。

任何人都可以帮助我,我真的很感激。

由于

1 个答案:

答案 0 :(得分:0)

因此,在撕掉我的头发寻找答案后,我意识到我没有将合成器的代表设置为“自我”。 (我恨自己!)

然而,这并没有解决我的问题,由于某些原因,这仍然无法解决。我发现speechSynthesiser:didFinishSpeakingUtterance:从未被调用过。

所以我给我的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];
for (int i = 0; i < [[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"]);
}