SKAction playSoundFileNamed在500 mp3时失败了 [英] SKAction playSoundFileNamed failed at 500 mp3
问题描述
在我的应用程序中,我需要使用很多不同的短mp3(大约500个项目一个接一个)
所以我使用 SKAction playSoundFileNamed
在大约200声之后它崩溃了'无法加载资源 - 无法加载资源s234.mp3'。内存增加到70mb。
如何避免这种情况?
我尝试过:
-
在每次迭代中重新创建声音
SKAction * mySound = [SKAction playSoundFileNamed:aa waitForCompletion:YES];
-
在.m
的开头创建一个变量
SKAction * mySound;
并在迭代中重复使用
mySound = [SKAction playSoundFileNamed:aa waitForCompletion:YES];
2。
在开始时将所有声音加载到数组
for(int j = 0; j< 500; j ++){
NSString * aa = [NSString stringWithFormat:@s%d.mp3,j];
[item.sounds addObject:[SKAction playSoundFileNamed:aa waitForCompletion:YES]];
}
...但从未改变 - 它崩溃并且无法加载mp3。
如何清除此内存泄漏?
已编辑
我也试图关闭ARC并每次手动解除它。没有任何改变。
这里的小小调将允许您使用常规的旧SKAction,但可以自定义代码中的播放。 / p>
https:// github .com / pepelkod / iOS- Examples / tree / master / PlaySoundWithVolume
+(SKAction *)playSoundFileNamed :( NSString *)fileName atVolume:(CGFloat)volume waitForCompletion:(BOOL)wait {
// setup audio
NSString * nameOnly = [fileName stringByDeletingPathExtension];
NSString * extension = [fileName pathExtension];
NSURL * soundPath = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:nameOnly ofType:extension]];
AVAudioPlayer * player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundPath error:NULL];
[player setVolume:volume];
[player prepareToPlay];
SKAction * playAction = [SKAction runBlock:^ {
[player play];
}];
if(wait == YES){
SKAction * waitAction = [SKAction waitForDuration:player.duration];
SKAction * groupActions = [SKAction组:@ [playAction,waitAction]];
返回groupActions;
}
返回playAction;
}
In my application I need to use a lot of short different mp3s (about 500 items one by one)
So I use SKAction playSoundFileNamed
After ~200 sounds it crashed with 'Failed to load resource - Resource s234.mp3 can not be loaded'. Memory rises to 70mb.
How to avoid this?
What I tried:
recreate sound in every iteration
SKAction *mySound=[SKAction playSoundFileNamed:aa waitForCompletion:YES];
create the one variable in the beggining of .m
SKAction *mySound;
and reuse it in iterations
mySound=[SKAction playSoundFileNamed:aa waitForCompletion:YES];
2. load all sounds to array once at start
for (int j=0;j<500;j++){
NSString *aa=[NSString stringWithFormat:@"s%d.mp3", j];
[item.sounds addObject:[SKAction playSoundFileNamed:aa waitForCompletion:YES]];
}
...but never changed - it crashes and can't load mp3.
How to clean this memory leaks?
EDITED I also tried to turn off ARC and manually dealloc it every time. Nothing changed.
This little ditty right here will allow you to use a regular old SKAction but customize the playback in code.
https://github.com/pepelkod/iOS-Examples/tree/master/PlaySoundWithVolume
+(SKAction*)playSoundFileNamed:(NSString*)fileName atVolume:(CGFloat)volume waitForCompletion:(BOOL)wait{
// setup audio
NSString* nameOnly = [fileName stringByDeletingPathExtension];
NSString* extension = [fileName pathExtension];
NSURL *soundPath = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:nameOnly ofType:extension]];
AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:soundPath error:NULL];
[player setVolume:volume];
[player prepareToPlay];
SKAction* playAction = [SKAction runBlock:^{
[player play];
}];
if(wait == YES){
SKAction* waitAction = [SKAction waitForDuration:player.duration];
SKAction* groupActions = [SKAction group:@[playAction, waitAction]];
return groupActions;
}
return playAction;
}
这篇关于SKAction playSoundFileNamed在500 mp3时失败了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!