2012-03-14 21 views
5

Đủ đơn giản ... Tôi có một bài hát nền chơi trên trò chơi của mình và tôi muốn đi qua một bản nhạc, thay vì một điểm dừng chân khó khăn.Làm cách nào để Crossfade nhạc trong cocos2d?

//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]; 
     } 

Trả lời

4

Bạn không thể thực hiện điều đó bằng cách sử dụng SimpleAudioEngine. Đây là cách tôi đã làm nó:

  1. Tạo một lớp mà kéo dài CDAudioManager

  2. Trong phương pháp mà bạn gọi để bắt đầu chơi một nhạc nền mới, kiểm tra đầu tiên nếu Ivar backgroundMusic (thừa hưởng từ CDAudioManager) không phải là không và đang chơi. Nếu có, hãy làm mờ nó bằng cách sử dụng CDLongAudioSourceFader.

  3. Tải nhạc nền mới thành backgroundMusic (đây là phiên bản CDLongAudioSource - tra cứu). Làm mờ nó bằng cách sử dụng CDLongAudioSourceFader.

Dưới đây là một đoạn của phương pháp trong bước 2 (xin lỗi không thể hiển thị cho bạn những phần còn lại vì nó là một phần của thư viện độc quyền của riêng tôi)

- (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]; 
    } 
} 
+0

Được rồi, cảm ơn bạn. –

5

tôi sử dụng phương pháp đơn giản này để thay thế hiện tại nhạc nền:

-(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]; 
} 

Hy vọng điều đó sẽ hữu ích!

0

Cảm ơn câu trả lời của @ adam.artajew! Tôi đã sửa đổi nó để sử dụng với Cocos2D v3 và tạo một danh mục Crossfade trên công cụ âm thanh để sử dụng nó từ bất kỳ nơi nào trong trò chơi. Bây giờ nó thực hiện những hành động sau đây:

  • Fade ra
  • Thay đổi theo dõi
  • Fade in

Trong OALSimpleAudio+Crossfade.m bao gồm các hàng nhập khẩu:

#import "CCDirector_Private.h" 
#import "CCActionManager.h" 

Và thực hiện phương pháp:

- (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]; 
} 

Cách sử dụng: Bao gồmOALSimpleAudio+Crossfade.h và gọi

[[OALSimpleAudio sharedInstance] playBg:@"MainBgMusic.mp3" crossfade:YES]; 
Các vấn đề liên quan