等待 AVSpeechSynthesiser 完成发音
Wait for AVSpeechSynthesiser to finish utterance
我正在编写一个应用程序,允许用户在听到他们想要关注的网络 link 时按下按钮。
问题是我正在使用的 for 循环将所有文本添加到话语列表中,并且在继续之前不等待话语完成,这意味着我无法分辨是什么 link他们想跟随。
我有一个 class 叫
Speech
它是 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];
}
并且在 class
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]) {}
}
}
我们的想法是应用程序应该等到话语发生后再继续。但是此刻它读出"Bookmarks,"就停止了,它甚至没有读出第一个书签,我也试过把while循环放在for循环的开头。
谁能帮帮我,我将不胜感激。
谢谢
因此,在竭尽全力寻找答案后,我意识到我没有将合成器的委托设置为 'self'。 (我讨厌我自己!)
但是,这并没有解决我的问题,出于某种原因,这仍然不起作用。我发现 speechSynthesiser:didFinishSpeakingUtterance: 从未被调用过。
因此,我向 Speech 对象发送了一个我希望它说出的字符串数组,并在该对象中跟踪它们,然后我添加了一个方法 return 文本数组中的位置合成器当前正在说的是:
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"]);
}
我正在编写一个应用程序,允许用户在听到他们想要关注的网络 link 时按下按钮。
问题是我正在使用的 for 循环将所有文本添加到话语列表中,并且在继续之前不等待话语完成,这意味着我无法分辨是什么 link他们想跟随。
我有一个 class 叫
Speech
它是 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];
}
并且在 class
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]) {}
}
}
我们的想法是应用程序应该等到话语发生后再继续。但是此刻它读出"Bookmarks,"就停止了,它甚至没有读出第一个书签,我也试过把while循环放在for循环的开头。
谁能帮帮我,我将不胜感激。
谢谢
因此,在竭尽全力寻找答案后,我意识到我没有将合成器的委托设置为 'self'。 (我讨厌我自己!)
但是,这并没有解决我的问题,出于某种原因,这仍然不起作用。我发现 speechSynthesiser:didFinishSpeakingUtterance: 从未被调用过。
因此,我向 Speech 对象发送了一个我希望它说出的字符串数组,并在该对象中跟踪它们,然后我添加了一个方法 return 文本数组中的位置合成器当前正在说的是:
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"]);
}