下面的代码获取方向列表并使用 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];
}
}