Sprite 套件检测声音完成

Sprite kit detect sound completion

我使用了一系列动作,其中一个动作为我播放了声音。

        SKAction *wait = [SKAction waitForDuration:1];

        NSString* completion_sound = self.gameData[@"level_data"][@"completion_sound"];
        SKAction *playSound = [SKAction playSoundFileNamed:completion_sound waitForCompletion:NO];

        SKAction *completion = [SKAction runBlock:^{

            // do some code here.

        }];

        SKAction *sequence = [SKAction sequence:@[wait, playSound, completion]];

        [self runAction:sequence];

但我想检测声音的完成。似乎在块调用时声音仍在播放。

你的顺序错了。正确的顺序应该是:

SKAction *sequence = [SKAction sequence:@[playSound, wait, completion]];

看来您正在为 waitForCompletion: 参数传递 NO,它应该是 YES.

If YES, the duration of this action is the same as the length of the audio playback. If NO, the action is considered to have completed immediately.

所以在这行代码中改为YES...

SKAction *playSound = [SKAction playSoundFileNamed:completion_sound waitForCompletion:YES];

...而playSound动作持续时间将是声音的长度,因此'completion block' SKAction *completion直到声音结束才会执行。