ios - SpriteKit : Preload sound file into memory before playing?

标签 ios objective-c ios7 sprite-kit

只是想知道这是否可能。目前,我第一次在应用程序运行时播放声音文件时,在声音实际播放之前会有明显的延迟(比如它正在缓存它或其他东西)。在此之后它立即播放没有问题,但如果我完全关闭应用程序并重新启动它,延迟将在第一次播放声音时恢复。这是我用来播放声音的代码:

[self runAction:[SKAction playSoundFileNamed:@"mySound.caf" waitForCompletion:NO]];

最佳答案

您可以采用的一种方法是在场景开始时加载声音:

你的场景.h:

@interface YourScene : SKScene
@property (strong, nonatomic) SKAction *yourSoundAction;
@end

你的场景.m:

- (void)didMoveToView: (SKView *) yourView
{
    _yourSoundAction = [SKAction playSoundFileNamed:@"yourSoundFile" waitForCompletion:NO];
    // the rest of your init code
    // possibly wrap this in a check to make sure the scene's only initiated once...
}

这应该会预加载声音,您应该能够通过在场景中调用操作来运行它:

[self runAction:_yourSoundAction];

我自己在有限的场景中尝试过,它似乎消除了延迟。

关于ios - SpriteKit : Preload sound file into memory before playing?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22826675/

相关文章:

objective-c - 在 if 语句中组合 contentOffset? iOS

ios - 如何映射自定义 NSManagedObject 类中的关系

ios - 根据速度更改快速的SKSprite节点动画

ios - MPMoviePlayerController 播放/暂停切换问题

ios 这是将数据从一个 View Controller 传递到另一个 View Controller 的最佳方式。通过单例类或 *.h 文件?

ios - 使用 iOS 发送请求 - 使用 304 HTTPCode 快速回答

ios - NSUserDefaults 事务性

ios - 如何在不同的 UIViewController 之间传递数据?

ios - 不明白为什么我的 NSString 数据为空

ios - 检测 iOS 7 中的静音开关问题