下面的代码采用方向列表并使用AVSpeechSynthesizer将其读出。完成后,用户将能够选择可变的时间量,应用程序将读出符合时间跨度的说明。
问题在于,当我按下播放按钮时,方向之间的延迟明显长于应有的延迟。而不是我用硬编码的两分钟,它需要超过三分钟。我已经记录了所有postUtteranceDelays的值,并且它们正确加起来。这也不是由于处理时间,因为当postUtteranceDelay设置为0时,方向之间没有暂停。我不确定发生了什么。
- (IBAction)play:(UIButton *)sender {
[sender setTitle:@"Showering" forState:UIControlStateNormal];
Shower *shower = [[SpecificShower alloc] init];
NSUInteger totalRatio = [shower calculateTotalRatio:shower];
NSNumber *offset = @18.0; // estimated time to speak instructions combined
NSNumber *seconds = @120.0; // hard coded but just for testing
int totalSeconds = seconds.intValue - offset.intValue;
self.synthesizer = [[AVSpeechSynthesizer alloc] init];
for (NSDictionary* direction in shower.directions) {
AVSpeechUtterance *aDirection = [[AVSpeechUtterance alloc] initWithString:direction[@"text"]];
NSNumber *directionLength = direction[@"length"];
aDirection.rate = .3;
aDirection.preUtteranceDelay = 0;
// totalRatio is calculated by adding all the lengths together
// then the individual direction length is divided by totalRatio
// and that fraction is multiplied by total number of seconds
// to come up with the postUtteranceDelay for each direction
aDirection.postUtteranceDelay = totalSeconds * [directionLength floatValue]/totalRatio;
NSLog(@"%f", aDirection.postUtteranceDelay);
[self.synthesizer speakUtterance:aDirection];
}
}