足够简单......我在游戏中播放了一首背景歌曲,我想交叉淡化一首曲目,而不是硬停。
//Prep Background Music
if (![[SimpleAudioEngine sharedEngine]isBackgroundMusicPlaying]) {
[[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:@"song.mp3"];
}
[[SimpleAudioEngine sharedEngine] setBackgroundMusicVolume:1.0];
//Play Background Music
if (![[SimpleAudioEngine sharedEngine]isBackgroundMusicPlaying]) {
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"song.mp3" loop:YES];
}
答案 0 :(得分:5)
我用这个简单的方法来取代当前的背景音乐:
-(void)replaceBackgroundMusic:(NSString *)filePath volume:(float)volume
{
// no music's playing right now
if (![[SimpleAudioEngine sharedEngine] isBackgroundMusicPlaying])
return [self playBackgroundMusic:filePath volume:volume];
// already playing requested track
if ([filePath isEqualToString:[[[CDAudioManager sharedManager] backgroundMusic] audioSourceFilePath]])
return;
// replace current track with fade out effect
float currentVolume = [SimpleAudioEngine sharedEngine].backgroundMusicVolume;
id fadeOut = [CCActionTween actionWithDuration:1 key:@"backgroundMusicVolume" from:currentVolume to:0.0f];
id playNew =
[CCCallBlock actionWithBlock:^
{
[[SimpleAudioEngine sharedEngine] setBackgroundMusicVolume:volume];
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:filePath];
}];
[[[CCDirector sharedDirector] actionManager] addAction:[CCSequence actions:fadeOut, playNew, nil] target:[SimpleAudioEngine sharedEngine] paused:NO];
}
希望有所帮助!
答案 1 :(得分:4)
使用SimpleAudioEngine
无法做到这一点。我是这样做的:
创建一个扩展CDAudioManager
在您调用以开始播放新背景音乐的方法中,首先检查ivar backgroundMusic
(继承自CDAudioManager
)是否为零且正在播放。如果是,则使用CDLongAudioSourceFader
将其淡出。
将新的背景音乐加载到backgroundMusic
(它是CDLongAudioSource
的实例 - 查找它)。使用CDLongAudioSourceFader
淡化它。
以下是步骤2中方法的片段(抱歉无法向您展示其余部分,因为它是我自己的专有库的一部分)
- (void)audioBackgroundPlay:(NSString *)bgmfile crossfade:(float)fade {
CDLongAudioSource *audioSource = [self audioBackgroundLoad:bgmfile];
if (audioSource != backgroundMusic) {
if (backgroundMusic) {
[self audioBackgroundControl:AUDIOCTRL_STOP fade:fade];
backgroundMusic.audioSourcePlayer.meteringEnabled = NO;
[backgroundMusic release];
}
backgroundMusic = [audioSource retain];
if (meteringEnabled_) {
backgroundMusic.audioSourcePlayer.meteringEnabled = YES;
}
}
if (![backgroundMusic isPlaying]) {
[self audioBackgroundControl:AUDIOCTRL_PLAY fade:fade];
}
}
答案 2 :(得分:0)
感谢@adam.artajew的回答!我将其修改为与Cocos2D v3
一起使用,并在音频引擎上创建了一个类别Crossfade
,以便在游戏中的任何位置使用它。现在它执行以下操作:
在OALSimpleAudio+Crossfade.m
中包含这些导入:
#import "CCDirector_Private.h"
#import "CCActionManager.h"
并实施方法:
- (void)playBg:(NSString *)name crossfade:(BOOL)crossfade {
// Skip if already playing requested track
if (self.bgPlaying &&
[self.backgroundTrack.currentlyLoadedUrl.lastPathComponent isEqualToString:name]) {
return;
}
// Play right now if no crossfade needed
if (!crossfade) {
[self playBg:name loop:true];
}
// Fade out just if music's playing right now
NSMutableArray *actions = [NSMutableArray array];
if (self.bgPlaying) {
id fadeOut = [CCActionTween actionWithDuration:0.5 key:@"bgVolume" from:self.bgVolume to:0.0f];
[actions addObject:fadeOut];
}
// Replace current track with fade in effect
id playNew = [CCActionCallBlock actionWithBlock:^{
[self playBg:name loop:true];
}];
id fadeIn = [CCActionTween actionWithDuration:0.5 key:@"bgVolume" from:0.0f to:1.0f];
// Combime final action
[actions addObjectsFromArray:@[playNew, fadeIn]];
id sequence = [CCActionSequence actionWithArray:actions.copy];
// Run action
[[[CCDirector sharedDirector] actionManager] addAction:sequence target:self paused:NO];
}
用法:包含OALSimpleAudio+Crossfade.h
并致电
[[OALSimpleAudio sharedInstance] playBg:@"MainBgMusic.mp3" crossfade:YES];