2012-03-14 17 views
5

काफी सरल ... मेरे पास मेरे गेम पर एक पृष्ठभूमि गीत है, और मैं एक हार्ड स्टॉप के बजाय एक ट्रैक को पार करना चाहता हूं।मैं 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]; 
     } 

उत्तर

4

आप SimpleAudioEngine का उपयोग करके ऐसा नहीं कर सकते हैं। यहाँ कैसे मैंने किया है:

  1. एक वर्ग है कि CDAudioManager

  2. फैली विधि कि आप एक नया पृष्ठभूमि संगीत चलाना प्रारंभ करने के लिए कॉल में बनाएं, पहले जांच अगर इवर backgroundMusic (CDAudioManager से विरासत में मिली) शून्य नहीं है और खेल रहा है। यदि ऐसा है, तो CDLongAudioSourceFader का उपयोग कर इसे बाहर निकाल दें।

  3. नया पृष्ठभूमि संगीत 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]; 
    } 
} 
+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]; 
} 

आशा है कि मदद करता है!

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]; 
संबंधित मुद्दे