SpriteKit:播放前将声音文件预加载到内存中? [英] SpriteKit: Preload sound file into memory before playing?

查看:26
本文介绍了SpriteKit:播放前将声音文件预加载到内存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Just wondering if this is possible. Currently, the first time I play a sound file while the app is running, there is a noticeable delay before the sound actually plays (like it's caching it or something). After this it plays instantly without issue, but if I close the app completely and relaunch it, the delay will be back the first time the sound is played. Here is the code I'm using to play the sound:

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

推荐答案

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

One approach you could take is to load the sound in right at the beginning of the scene:

YourScene.h:

YourScene.h:

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

YourScene.m:

YourScene.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...
}

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

This should preload the sound, and you should be able to run it by calling the action on your scene:

[self runAction:_yourSoundAction];

我自己在一个有限的场景中尝试过这个,它似乎摆脱了延迟.

I've tried this myself in a limited scenario and it appears to get rid of the delay.

这篇关于SpriteKit:播放前将声音文件预加载到内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆