【问题标题】:Sound effect stops midway when multiple sounds are being played?播放多个声音时音效中途停止?
【发布时间】:2012-04-20 12:19:51
【问题描述】:

我有一个 8 秒长的充电音效,只有在我的游戏中每个关卡结束时才会触发该音效。

问题是,当同时播放其他声音(激光、爆炸等)时,音效会在中途停止。我在其他地方读到,您可以同时播放多达 32 个声音,但游戏最多可能同时播放多达 7 或 8 个声音效果。但是充电的音效还是会中断。

如果我在游戏中不进行任何射击,只播放充电效果,它就会一直播放。

我在启动时像这样预加载我的声音:

-(void)setupSound
{
    [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"Kickshock.mp3" loop:YES];
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"explosion_large.caf"];
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"explosion_small.caf"];
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"laser_ship.caf"];
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"powerup.mp3"];
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"railgun.mp3"];
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"metal.mp3"];
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"count.mp3"];
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"charge_up.mp3"];
}

-(id)init
{
    if( (self = [super init]) )
    {
        [self setupSound];
        // Rest of other code..
    }
    return self;
}

我会在需要时播放它们(发射激光、关卡结束、爆炸等):

[[SimpleAudioEngine sharedEngine] playEffect:@"charge_up.mp3" pitch:1.0 pan:0.0 gain:0.4];

我猜充电声音正在失去其“插槽”,因为正在播放其他声音?就像上面提到的,当充电音效关闭时,我最多只能同时播放 7 或 8 个声音。

如果我的声音正在失去它的“槽”,有没有办法可以锁定一个特定的声音效果,这样它就不会失去它的“槽”?

知道我做错了什么或者我能做些什么来纠正这个问题吗?任何帮助是极大的赞赏! :D 感谢您的关注。

【问题讨论】:

    标签: cocos2d-iphone audio simpleaudioengine


    【解决方案1】:

    好的,看完教程后我明白了:

    http://bobueland.com/cocos2d/?p=200

    我没有意识到 SimpleAudioEngine 是为短音效设计的。

    对于那些被困在这一点上的人,这就是我所做的:

    在界面中:

    CDLongAudioSource *rightChannel;
    

    在我的 init 或 setupSound 方法中:

    rightChannel = [[CDAudioManager sharedManager] audioSourceForChannel:kASC_Right];
    [rightChannel load:@"charge_up.mp3"];
    

    当我想玩的时候:

    [rightChannel play];
    

    我相信这只有在你只有一个长音效时才有效。如果您有多个长音效,我相信您必须以不同的方式处理它。除非我只是创建更多 CDLongAudioSource 实例?有没有人有任何链接/教程?谢谢!

    【讨论】:

      【解决方案2】:

      要播放多种声音,我会使用 CDAudioManager 和 CDSoundEngine 而不是 SimpleAudioEngine。

      CDSoundEngine 允许多个通道(最多 32 个),可以同时播放声音。

      例子:

      // create engine
      CDSoundEngine * engine = [CDAudioManager sharedManager].soundEngine;
      
      // assign groups
      NSArray * groups = [NSArray arrayWithObjects:
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1],
          [NSNumber numberWithInt:1], nil];
      
      [engine defineSourceGroups:groups];
      [CDAudioManager initAsynchronously:kAMM_FxPlusMusicIfNoOtherAudio];
      
      // load requests
      NSMutableArray * requests = [[[NSMutableArray alloc] init] autorelease];
      
      NSString * plist_sounds = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"Sounds.plist"]];
      if (![[NSFileManager defaultManager] fileExistsAtPath:plist_sounds]) {
          plist_sounds = [[NSBundle mainBundle] pathForResource:@"Sounds" ofType:@"plist"];
      }
      NSDictionary * dictionary_sounds = [[NSDictionary dictionaryWithContentsOfFile:plist_sounds] objectForKey:@"Sounds"];   
      if (dictionary_sounds != nil) {
          for (id key in dictionary_sounds) {
              [requests addObject:[[[CDBufferLoadRequest alloc] init:[key intValue] filePath:[dictionary_sounds objectForKey:key]] autorelease]];
          }
      }
      
      [engine loadBuffersAsynchronously:requests];
      

      然后播放声音:

      - (ALuint)play:(NSInteger)sound group:(NSInteger)group loop:(BOOL)forever volume:(float)volume {
           [[CDAudioManager sharedManager].soundEngine playSound:sound sourceGroupId:group pitch:1.0f pan:0.0f gain:volume loop:forever];
      }
      

      【讨论】:

        【解决方案3】:

        对不起我简单的英语......
        简单引擎目前最多有 32 个音频通道

        if(SoundId == 0)
        {
            // when you say playEffect engine capture one channel for effect
            // do playEffect once and save result SoundID for Stop or Resume effect
            SoundId = SimpleAudioEngine::sharedEngine()->playEffect("audio.mp3");
        }
        // effect loaded, channel captured, say resume for play effect again, nothing more 
        else
        {
            SimpleAudioEngine::sharedEngine()->resumeEffect(SoundId);
        }
        
        // if you say effect nothing will happen with channel try
        SimpleAudioEngine::sharedEngine()->stopEffect(SoundID);
        

        在这种情况下,第一个效果的通道不会被新的效果替换, 如果运行次数超过 32

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多