SKAction playSoundFileNamed 在 500 mp3 时失败 [英] SKAction playSoundFileNamed failed at 500 mp3

查看:19
本文介绍了SKAction playSoundFileNamed 在 500 mp3 时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我需要使用很多不同的短mp3(一个一个大约500个项目)

In my application I need to use a lot of short different mp3s (about 500 items one by one)

所以我使用 SKAction playSoundFileNamed

大约 200 次声音后,它因加载资源失败 - 无法加载资源 s234.mp3"而崩溃.内存增加到 70mb.

After ~200 sounds it crashed with 'Failed to load resource - Resource s234.mp3 can not be loaded'. Memory rises to 70mb.

如何避免这种情况?

我尝试了什么:

  1. 在每次迭代中重新创建声音

  1. recreate sound in every iteration

SKAction *mySound=[SKAction playSoundFileNamed:aa waitForCompletion:YES];

  • 在.m开头创建一个变量

  • create the one variable in the beggining of .m

    SKAction *mySound;
    

  • 并在迭代中重用它

        mySound=[SKAction playSoundFileNamed:aa waitForCompletion:YES];
    

    2.在开始时将所有声音加载到数组中

    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]];
        }
    

    ...但从未改变 - 它崩溃并且无法加载 mp3.

    ...but never changed - it crashes and can't load mp3.

    如何清理这种内存泄漏?

    已编辑我还尝试关闭 ARC 并每次手动解除分配.没有任何改变.

    EDITED I also tried to turn off ARC and manually dealloc it every time. Nothing changed.

    推荐答案

    这里的这个小玩意将允许您使用常规的旧 SKAction,但可以在代码中自定义播放.

    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/主/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屋!

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