首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVAudioPlayer泄露?

AVAudioPlayer泄露?
EN

Stack Overflow用户
提问于 2011-08-28 20:39:51
回答 2查看 1.2K关注 0票数 0

我有个问题,不知道该怎么处理。Tyring来构建一个具有声音和动画的应用程序。它会给我一个级别1,然后是级别2内存警告。我试着构建和分析,我得到了所有这些潜在的泄漏。如果我释放theAudio,声音就不会播放。有什么提示吗?

这是我得到的代码的一部分和leakes

代码语言:javascript
复制
    - (IBAction)playsound2
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"cat" ofType:@"mp3"];
        AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
        *- **Method returns an Objective-C object with a +1 retain count (owning reference)***
        theAudio.delegate = self;
        [theAudio play];

        ***-  Object allocated on line 302 and stored into 'theAudio' is no longer referenced after this point and has a retain count of +1 (object leaked)***


        cat1.hidden = 0;
        cat.hidden = 1;
        [cat1 startAnimating];
        cat.center = cat1.center;
        [self performSelector:@selector(loadAnimations) withObject:nil afterDelay:1.0];
        catLabel.hidden = 0;
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-28 20:58:38

到目前为止,对于同样的问题,是解决方案。

代码语言:javascript
复制
    - (AVAudioPlayer *)audioPlayerWithContentsOfFile:(NSString *)path {
        NSData *audioData = [NSData dataWithContentsOfFile:path];
        AVAudioPlayer *player = [AVAudioPlayer alloc];
        if([player initWithData:audioData error:NULL]) {
            [player autorelease];
        } else {
            [player release];
            player = nil;
        }
        return player;
    }

为了获得更好的想法,你可以关注

票数 6
EN

Stack Overflow用户

发布于 2012-05-10 09:44:59

您必须为AVAudioPlayer类实例声明一个实例变量。

代码语言:javascript
复制
//Instance variable:
{
    AVAudioPlayer* theAudio;
}

- (IBAction)playsound2
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"cat" ofType:@"mp3"];
    if (!theAudio ) {
        theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
        if (theAudio ) {
            theAudio.delegate = self;
            [theAudio play];
        }
    }

    cat1.hidden = 0;
    cat.hidden = 1;
    [cat1 startAnimating];
    cat.center = cat1.center;
    [self performSelector:@selector(loadAnimations) withObject:nil afterDelay:1.0];
    catLabel.hidden = 0;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7221024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档