Ios 等待AVSpeechSynthesiser完成讲话

Ios 等待AVSpeechSynthesiser完成讲话,ios,avspeechsynthesizer,Ios,Avspeechsynthesizer,我正在编写一个应用程序,允许用户在听到他们想要关注的web链接时按下按钮 问题是,我使用的for循环将所有文本添加到话语列表中,而不是等到话语完成后再继续,这意味着我无法说出他们想要遵循的链接 我有一门课叫 讲话 作为AvSpeechSynthesizer的代表,我尝试创建自己的方式来确定话语何时结束: -(id)init { self = [super init]; if (self) { _synthesiser = [[AVSpeechSynthesizer alloc]init]

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

问题是,我使用的for循环将所有文本添加到话语列表中,而不是等到话语完成后再继续,这意味着我无法说出他们想要遵循的链接

我有一门课叫

讲话

作为AvSpeechSynthesizer的代表,我尝试创建自己的方式来确定话语何时结束:

-(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];
}
在课堂上

视图控制器

-(无效)readBookmarks
{
[[自言自语]继续思考];
[[self speech]输出语音:@“书签,”;
对于([self bookmarksPointer];[self bookmarksPointer]<[self bookmarks]count];[u bookmarksPointer++){
NSDictionary*dictionary=[[self bookmarks]objectAtIndex:[self bookmarksPointer]];
[[self speech]outputAsSpeech:[dictionary objectForKey:@“title”];
而([[自言自语]在说话]{}
}
}
当时的想法是,应用程序应该等到发出声音后再继续。但现在它读出“Bookmarks”,然后停止,它甚至没有读出第一个书签,我还尝试将while循环放在for循环的开头

谁能帮帮我,我会非常感激的


谢谢

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

然而,这并没有解决我的问题,出于某种原因,这仍然不起作用。我发现语音合成器:didFinishSpeakingUtterance:从未被调用

因此,我给我的语音对象发送了一个字符串数组,我想让它说出并在该对象中跟踪它们,然后我添加了一个方法来返回合成器当前所说文本数组中的位置:

讲话

-(id)init
{
self=[super init];
如果(自我){
_合成器=[[AVSpeechSynthesizer alloc]init];
[[self synthesiser]setDelegate:self];
[自设置峰值:否];
}
回归自我;
}
-(void)outputAsSpeech:(NSArray*)textary
{
[self-setTextToBeSpoken:textary];
[自设置阵列指针:0];
[[self-Synthesizer]SpeakOutstance:[[AvSpeechOutstance alloc]initWithString:[[self-TextToSpoken]objectAtIndex:[self-arrayPointer]]];
}
-(void)speechSynthesizer:(AVSpeechSynthesizer*)合成器完成speechoutrance:(avspeechoutrance*)outrance
{
_arrayPointer++;
如果([self arrayPointer]<[self textToBeSpoken]计数]){
[[self-Synthesizer]SpeakOutstance:[[AvSpeechOutstance alloc]initWithString:[[self-TextToSpoken]objectAtIndex:[self-arrayPointer]]];
}
}
-(int)正在讲话的字符串
{
返回[自阵列指针];
}
视图控制器

-(无效)readBookmarks
{
[[自言自语]继续思考];
NSMutableArray*textToSpeak=[[NSMutableArray alloc]init];
对于(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];
}
-(无效)目前正在讲话
{
NSDictionary*dictionary=[[self bookmarks]objectAtIndex:[[self speech]stringbeingspeaked]];
NSLog([dictionary objectForKey:@“title”]);
}

当您单步执行for循环时会发生什么?NSDictionary值是多少?objectForKey值呢?它们都得到了正确的结果,我知道它从每个NSDictionary中得到了正确的文本。
-(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]) {}
    }
}
-(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"]);
}