iphone - iOS6下AVAudioPlayer内存泄露
PHPz
PHPz 2017-04-17 11:01:01
[iOS讨论组]

我的视图控制器里放了一个按钮。main bundle里放了一个1.mp3,按钮每按一次就停止上次的音乐播放,开始下次音乐播放;

此代码在iOS4/5工作正常,iOS6中有严重的内存泄露。如果1.mp3有5M大,iOS6下每次播放就会有5M的内存不能释放。

我在iOS6下的使用方法错了吗?求指导

代码如下(开启ARC):

@implementation ViewController
{
    AVAudioPlayer *_player;
}

- (void)play
{
    if (_player)
    {
        [_player stop];
        _player = nil;
    }
    
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"test.app/1.mp3"];
    NSData *musicData = [[NSData alloc] initWithContentsOfFile:path];
    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:musicData error:nil];
    player.volume = 1;
    if (player)
    {
        _player = player;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(100, 100, 100, 100);
    [button setTitle:@"play" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

@end
PHPz
PHPz

学习是最好的投资!

全部回复(1)
PHP中文网

苹果iOS6运行时的bug,已反馈给苹果:https://bugreport.apple.com/cgi-bin/W...

相同问题:https://devforums.apple.com/thread/16...

苹果修复之前替代方法:将data写入缓存文件,用initWithContentsOfURL:替代initWithData:

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号