iOS如何在Sprite Kit游戏中创建音频淡入/淡出效果?

时间:2023-01-23 07:49:21

I'm interested in how I can manage background music in my Sprite Kit game to achieve fade in/out.

我对如何在Sprite Kit游戏中管理背景音乐以实现淡入/淡出感兴趣。

I noticed that Sprite Kit has a built-in sound player, but it seems to be more useful for very short effects, like "on hit" sounds:

我注意到Sprite Kit有一个内置的声音播放器,但它似乎对非常短的效果更有用,比如“on hit”声音:

[self runAction:[SKAction playSoundFileNamed:@"music.mp3" waitForCompletion:NO]];

It does not seem like there's a way to stop this sound.

似乎没有办法阻止这种声音。

I'm using Kobold Kit, and it comes with OALSimpleAudio library that can play sounds:

我正在使用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];

There's a bgVolume property in OALSimpleAudio, but no real fade.

OALSimpleAudio中有一个bgVolume属性,但没有真正的淡入淡出。

Should try to write my own fade in/out code of if there's something out there I can use to control volume over time of a generic music player, like OALSimpleAudio.

应该尝试编写我自己的淡入/淡出代码,如果有什么东西我可以用来控制通用音乐播放器的音量,如OALSimpleAudio。

2 个解决方案

#1


4  

You can also just use the build in AVAudioPlayer or of course adapt the function to your player:

您也可以使用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];

And then you add the function from this 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;
    }
}

#2


3  

ObjectAL has an AVAudioPlayer wrapper built in for music playback. It's called OALAudioTrack.

ObjectAL内置了一个用于音乐播放的AVAudioPlayer包装器。它叫做OALAudioTrack。

OALAudioTrack has a method fadeTo:duration:target:selector: that you can use to do fading. You already have an instance of OALAudioTrack available to you as the simple audio interface's backgroundTrack property:

OALAudioTrack有一个方法fadeTo:duration:target:selector:你可以用来做淡化。您已经有一个OALAudioTrack实例可用作简单音频接口的backgroundTrack属性:

[[OALSimpleAudio sharedInstance].backgroundTrack fadeTo:.. duration:.. ..];

#1


4  

You can also just use the build in AVAudioPlayer or of course adapt the function to your player:

您也可以使用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];

And then you add the function from this 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;
    }
}

#2


3  

ObjectAL has an AVAudioPlayer wrapper built in for music playback. It's called OALAudioTrack.

ObjectAL内置了一个用于音乐播放的AVAudioPlayer包装器。它叫做OALAudioTrack。

OALAudioTrack has a method fadeTo:duration:target:selector: that you can use to do fading. You already have an instance of OALAudioTrack available to you as the simple audio interface's backgroundTrack property:

OALAudioTrack有一个方法fadeTo:duration:target:selector:你可以用来做淡化。您已经有一个OALAudioTrack实例可用作简单音频接口的backgroundTrack属性:

[[OALSimpleAudio sharedInstance].backgroundTrack fadeTo:.. duration:.. ..];