1

I am developing a game where an SKAudioNode plays the game music. When the player starts a new game, I want the music to fade in so I wrote the following code:

SKAudioNode *SFXNode = [[SKAudioNode alloc] initWithfile:@"GameMusic.mp3"];
[SFXNode runAction:[SKAction changeVolumeTo:0 duration:0]];
SFXNode.positional = NO;
[self addChild:SFXNode];
[SFXNode runAction:[SKAction changeVolumeTo:1 duration:1]];

However, when the scene initiates, the music plays at full volume for a split second, then it mutes and fades back in as it is supposed to. Does anyone have any idea why this is happening? (The music is only full volume after the user has triggered the modal segue to the scene, but before it has been displayed on the screen. As soon as the scene is displayed the music fades in normally). Also, the music does seem to be positional: as the player moves about in the scene the volume changes which I do not want happening. Solutions top either of these problems are much appreciated. Thank you!

4

1 回答 1

2

这是因为您的音量变化是在音频发送到音频缓冲区之后发生的。您需要在更新发生之前重置音量。现在您不能在创建音频后立即执行此操作,您需要在将其添加到 AudioEngine 后执行此操作,因此我建议您在场景移动到视图(在didMoveToView函数中)时执行此操作。

实际更改音量的代码应该类似于

((AVAudioMixerNode *)SFXNode.avAudioNode).volume = 0 但我建议avAudioNode先检查是否存在并且它确实符合AVAudioMixerNode协议

于 2018-01-22T16:42:15.727 回答