【问题标题】:stop effect in cocos2d?在 cocos2d 中停止效果?
【发布时间】:2011-08-28 15:29:08
【问题描述】:

经过这么多搜索,我找不到任何解决 cocos2d 停止效果的方法。

我的效果是播放从数据库中获取的声音,所以要停止那个特定的声音,我必须这样做:

[[SimpleAudioEngine sharedEngine] stopEffect:[NSString stringWithFormat:@"%@.wav",sound]];

但我收到警告:stopEffect 从指针生成整数而不进行强制转换..

这是为什么呢? 我怎样才能停止一次播放的所有声音???或者不是一个特定的?还有什么办法吗?

非常感谢。

【问题讨论】:

  • 好的,我明白了:ALuint soundEffectID; soundEffectID=[[SimpleAudioEngine sharedEngine] playEffect:play]; [[SimpleAudioEngine sharedEngine] stopEffect:soundEffectID];
  • 您应该将此作为答案提交并接受您的答案以将问题标记为已解决。

标签: objective-c cocos2d-iphone


【解决方案1】:

好的,你这样做:

ALuint soundEffectID;

//to start
soundEffectID=[[SimpleAudioEngine sharedEngine] playEffect:@"my sound"];
//to stop
[[SimpleAudioEngine sharedEngine] stopEffect:soundEffectID];

【讨论】:

    【解决方案2】:

    如果您没有 soundEffectID,则可以进行下一步。它帮助我解决了我的问题。

    static NSMutableArray *soundsIdArr;
    
    @implementation MusicAndSound
    
    //It must be run before using sound
    +(void)initSound
    {
        NSLog(@"initSound");
    
        soundsIdArr = [NSMutableArray arrayWithCapacity:0];
        [soundsIdArr retain];
    }
    
    +(void)playSound:(NSString *)fileName
    {
        [[SimpleAudioEngine sharedEngine] setEffectsVolume:1.0];
    
        soundEffectID = [[SimpleAudioEngine sharedEngine] playEffect:[[NSBundle mainBundle] pathForResource:fileName ofType:nil]];
    
        [soundsIdArr addObject:[NSString stringWithFormat:@"%i", soundEffectID]];
    }
    
    +(void)stopAllSounds
    {
        [[SimpleAudioEngine sharedEngine] setEffectsVolume:0.0];
    
        for (int i=0; i<[soundsIdArr count]; i++)
        {
            [[SimpleAudioEngine sharedEngine] stopEffect:[[soundsIdArr objectAtIndex:i] intValue]];
        }
    
        [soundsIdArr removeAllObjects];
    }
    
    - (void)dealloc
    {
        [soundsIdArr release];
    
        [super dealloc];
    }
    
    @end
    

    【讨论】:

      【解决方案3】:

      还有一些...

      如果你想停止所有运行的声音,那么

      [SimpleAudioEngine end];
      

      但这也会释放sharedEngine,所以你需要调用“SharedEngine”以防想再次播放声音:)

      【讨论】:

        猜你喜欢
        • 2012-08-07
        • 1970-01-01
        • 1970-01-01
        • 2017-05-16
        • 1970-01-01
        • 2010-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多