我对如何在Sprite Kit游戏中管理背景音乐以实现淡入/淡出感兴趣. 我注意到Sprite Kit有一个内置的声音播放器,但它似乎对非常短的效果更有用,比如“on hit”声音: [self runAction:[SKAction pl
我注意到Sprite Kit有一个内置的声音播放器,但它似乎对非常短的效果更有用,比如“on hit”声音:
[self runAction:[SKAction playSoundFileNamed:@"music.mp3" waitForCompletion:NO]];
似乎没有办法阻止这种声音.
我正在使用Kobold Kit,它附带可以播放声音的OALSimpleAudio库:
[[OALSimpleAudio sharedInstance] preloadEffect:@"die.wav"]; [[OALSimpleAudio sharedInstance] playEffect:@"die.wav"]; [[OALSimpleAudio sharedInstance]preloadBg:@"battle.mp3"]; [[OALSimpleAudio sharedInstance] playBg:@"battle.mp3" loop:YES];
OALSimpleAudio中有一个bgVolume属性,但没有真正的淡入淡出.
应该尝试编写我自己的淡入/淡出代码,如果有什么东西我可以用来控制通用音乐播放器的音量,如OALSimpleAudio.
您也可以在 AVAudioPlayer中使用构建版本,或者当然还可以将功能调整到您的播放器://play background sound NSError *error; NSURL * backgroundMusicURL = [[NSBundle mainBundle] URLForResource:@"SpaceLife" withExtension:@"mp3"]; self.backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error]; self.backgroundMusicPlayer.numberOfLoops = -1; [self.backgroundMusicPlayer prepareToPlay]; [self.backgroundMusicPlayer play];
然后你添加这个post的功能:
- (void)doVolumeFade { if (self.backgroundMusicPlayer.volume > 0.1) { self.backgroundMusicPlayer.volume = self.player.volume - 0.1; [self performSelector:@selector(doVolumeFade) withObject:nil afterDelay:0.1]; } else { // Stop and get the sound ready for playing again [self.backgroundMusicPlayer stop]; self.backgroundMusicPlayer.currentTime = 0; [self.backgroundMusicPlayer prepareToPlay]; self.backgroundMusicPlayer.volume = 1.0; } }