1

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

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

我有一堂课叫

演讲

这是 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];
}

而在课堂上

视图控制器

-(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 循环的开头。

任何人都可以帮助我,我将非常感激。

谢谢

4

1 回答 1

1

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

但是,这并没有解决我的问题,由于某种原因,这仍然不起作用。我发现 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];
}

视图控制器

-(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"]);
}
于 2015-04-20T16:21:06.693 回答