问题描述
只是想知道这是否可行。目前,我第一次在应用程序运行时播放声音文件时,在声音实际播放之前会有明显的延迟(比如它正在缓存它或其他东西)。在此之后它立即播放没有问题,但如果我完全关闭应用程序并重新启动它,延迟将在第一次播放声音时返回。以下是我用来播放声音的代码:
[self runAction:[SKAction playSoundFileNamed:@mySound.caf waitForCompletion:NO]];
您可以采取的一种方法是加载声音就在场景的开头:
YourScene.h:
@interface YourScene:SKScene
@property(强大,非原子)SKAction * yourSoundAction;
@end
YourScene.m:
'pre>
- (无效)didMoveToView:(SKView *)yourView
{
_yourSoundAction = [SKAction playSoundFileNamed:@ yourSoundFile waitForCompletion:NO];
//你的初始代码的剩余部分
//可能将它包装在支票中,以确保场景只发起一次......
}
这应该预加载声音,你应该可以通过调用场景中的动作来运行声音:
[self runAction:_yourSoundAction];
我自己在有限的情况下尝试了这个,它似乎摆脱了延迟。 / p>
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:
@interface YourScene : SKScene
@property (strong, nonatomic) SKAction *yourSoundAction;
@end
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:在播放之前将声音文件预加载到内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!