Sprite kit 当AVAudioPlayer ist反复触发时,大量fps下降(精灵套件)

Sprite kit 当AVAudioPlayer ist反复触发时,大量fps下降(精灵套件),sprite-kit,avaudioplayer,frame-rate,Sprite Kit,Avaudioplayer,Frame Rate,我正在用精灵工具包制作一个iOS游戏,你可以在其中发射水枪。只要水枪一开,就会发出一声划伤声。由于我实现了这个功能,当使用触动开始方法反复触发音效时,我遇到了大量的fps问题 有没有可能解决这个问题 @property (nonatomic) AVAudioPlayer *splashSound; -(id)initWithSize:(CGSize)size { NSError *error3; NSURL *splashURL = [[NSBundle mainBundle

我正在用精灵工具包制作一个iOS游戏,你可以在其中发射水枪。只要水枪一开,就会发出一声划伤声。由于我实现了这个功能,当使用触动开始方法反复触发音效时,我遇到了大量的fps问题

有没有可能解决这个问题

@property (nonatomic) AVAudioPlayer *splashSound;

-(id)initWithSize:(CGSize)size {

    NSError *error3;
    NSURL *splashURL = [[NSBundle mainBundle] URLForResource:@"splash" withExtension:@"caf"];
    self.splashSound = [[AVAudioPlayer alloc]initWithContentsOfURL:splashURL error:&error3];
    self.splashSound.numberOfLoops = 1;
    [self.splashSound prepareToPlay];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */
    startGamePlay = YES;
    if (self.waterCapacity != 0){
    waterSprayed = YES;
    [self.splashSound play]; // sound starts when screen is touched
    [self.currentWasser sprayWater];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.currentWasser removeActionForKey:@"water"];
    [self.splashSound pause]; // sound is paused when touches ended
    waterSprayed = NO;
}

使用UILongPressGestureRecognitor修复了此问题:工作正常。太高兴了

- (void)didMoveToView:(SKView *)view
{
    UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(delayedSplashSound:)];
    longGesture.minimumPressDuration = 0.15;
    [view addGestureRecognizer:longGesture];
}

- (void)delayedSplashSound:(UITapGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        [self.splashSound play];
    }else if (recognizer.state == UIGestureRecognizerStateEnded){
        [self.splashSound pause];
        [self.currentWater removeActionForKey:@"water"];
        waterSprayed = NO;
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    startGamePlay = YES;
    if (self.waterCapacity != 0){
    waterSprayed = YES;
    [self.currentWater sprayWater];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.currentWater removeActionForKey:@"water"];
    waterSprayed = NO;
}

使用UILongPressGestureRecognitor修复了此问题:工作正常。太高兴了

- (void)didMoveToView:(SKView *)view
{
    UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(delayedSplashSound:)];
    longGesture.minimumPressDuration = 0.15;
    [view addGestureRecognizer:longGesture];
}

- (void)delayedSplashSound:(UITapGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        [self.splashSound play];
    }else if (recognizer.state == UIGestureRecognizerStateEnded){
        [self.splashSound pause];
        [self.currentWater removeActionForKey:@"water"];
        waterSprayed = NO;
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    startGamePlay = YES;
    if (self.waterCapacity != 0){
    waterSprayed = YES;
    [self.currentWater sprayWater];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.currentWater removeActionForKey:@"water"];
    waterSprayed = NO;
}

在使用无限循环的
AVAudioPlayer.play()
AVAudioPlayer.stop()
时,我还有另一个关于帧下降的解决方案。不要调用
play()
stop()
,只需调用
play()
一次,然后设置
yourplayernistance.volume=0.0
yourplayernistance.volume=1.0
。这解决了帧故障/-drops for me-perfect 60 fps,而不是下降到~40 fps。

我有另一个解决方案,可以在使用无限循环的
AVAudioPlayer.play()
AVAudioPlayer.stop()
时进行帧下降。不要调用
play()
stop()
,只需调用
play()
一次,然后设置
yourplayernistance.volume=0.0
yourplayernistance.volume=1.0
。这解决了帧故障/-对我来说下降-完美的60 fps,而不是下降到~40 fps。

当声音效果应该继续播放时,为什么循环数为1?如果是一个长的音频文件,你应该把它改成一个小的(1-5秒),然后简单地循环它。PS:如果您不使用error对象,您可以在init方法中传入error:nil。这在实际设备上测试过吗?模拟器太糟糕了。谢谢你的回复。不幸的是,这一变化没有任何区别。我想我将使用UILongPressGestureRecognitor在触摸屏幕1秒钟后播放声音文件。我想我可以通过这种方式防止低fps为什么循环数是1,而音效应该继续播放?如果是一个长的音频文件,你应该把它改成一个小的(1-5秒),然后简单地循环它。PS:如果您不使用error对象,您可以在init方法中传入error:nil。这在实际设备上测试过吗?模拟器太糟糕了。谢谢你的回复。不幸的是,这一变化没有任何区别。我想我将使用UILongPressGestureRecognitor在触摸屏幕1秒钟后播放声音文件。我想我可以通过这种方式防止fps过低