Audio6脚本在iOS6中泄漏?

Ope*_*ead 4 iphone avaudioplayer ios6

当我使用AudioToolBox播放音乐时,内存泄漏严重.

AVAudioPlayer *newMusicPlayer = [[AVAudioPlayer alloc] initWithData:data error:&error];
Run Code Online (Sandbox Code Playgroud)

我用这段代码来播放音乐.在iOS5和iOS4中,它可以正常工作.但是在iOS6中,如果数据大小为5M,则所有5M泄露.我无法在仪器中看到泄漏信息.

有没有人有同样的问题?任何建议都将不胜感激.

这里的所有音频代码(使用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
Run Code Online (Sandbox Code Playgroud)

Jer*_*aho 5

我正在使用文件播放音频AVAudioPlayer,并发现iOS模拟器一直泄漏内存,但设备没有.这是使用仪器验证的.这是ARC代码.

我的播放器声明如下:

@property (nonatomic, retain) AVAudioPlayer *numberPlayer;
Run Code Online (Sandbox Code Playgroud)

和这样合成:

@synthesize numberPlayer = _numberPlayer;
Run Code Online (Sandbox Code Playgroud)

由于我需要播放几种不同的声音,并且AVAudioPlayer在创建后无法重置播放不同的音频文件,我每次都会创建一个新的播放器,如下所示:

NSString *audioFilePathName = [NSString stringWithFormat:@"Voices/%@/%03i.m4a", self.voiceName, self.theNumber];
NSURL *url = [NSURL fileURLWithPath:BUNDLE_FULL_PATH(audioFilePathName)];
self.numberPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
self.numberPlayer.numberOfLoops = 0;
[self.numberPlayer setCurrentTime:0.0];
[self.numberPlayer setDelegate:self];
[self.numberPlayer prepareToPlay];
[self.numberPlayer play];
Run Code Online (Sandbox Code Playgroud)

在我的代表中,我将播放器设置为nil完成播放时:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    if (player == self.numberPlayer) {
        _numberPlayer = nil;
    }
}
Run Code Online (Sandbox Code Playgroud)

在模拟器,既AudioSessionDeviceUISoundNewDevice泄漏内存.然而,实际上最大的泄密者NSURL.这些泄漏都不会发生在设备上.这种行为在iOS 6中没有改变,但我确实将我的项目部署设置为5.0,这应该有所不同.

另请参阅/sf/ask/240344121/AudioToolbox库AVAudioPlayer中的内存泄漏.

  • initWithContentsOfURL:节省兆字节的内存,只有initWithData:泄漏兆字节.我将在我的项目中用initWithContentOfURL替换initWithData.谢谢 :) (3认同)