PRN*_*ios 5 iphone objective-c cocos2d-iphone ios
很简单......我在我的游戏中播放了一首背景歌曲,我想交叉淡化一首曲目,而不是硬停.
//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];
}
Run Code Online (Sandbox Code Playgroud)
小智 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];
}
Run Code Online (Sandbox Code Playgroud)
希望有所帮助!
你不能使用 来做到这一点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];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2718 次 |
| 最近记录: |