AudioToolBox Error in iOS6?

When I use AudioToolBox to play music, memory leaks a lot.

AVAudioPlayer *newMusicPlayer = [[AVAudioPlayer alloc] initWithData:data error:&error]; 

I use this code to play music. On iOS5 and iOS4, it works correctly. But in iOS6, if the data size is 5 million, all 5M leaked. And I do not see information about the leak in the Tools.

Does anyone have the same problem? Any suggestion would be appreciated.

All my sound code here (using 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 
+4
source share
2 answers

I play audio from a file using AVAudioPlayer and find that the iOS simulator loses memory sequentially, but the device does not. This has been verified using tools. This is the ARC code.

My player is declared as follows:

 @property (nonatomic, retain) AVAudioPlayer *numberPlayer; 

and is synthesized as follows:

 @synthesize numberPlayer = _numberPlayer; 

Since I need to play several different sounds, and AVAudioPlayer cannot be reset to play another audio file after creating it, I create a new player each time, for example:

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

In my delegate, I installed nil player when it finished playing:

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

In the simulator, both the AudioSessionDevice memory and the UISoundNewDevice . However, the biggest level is actually NSURL . None of these leaks occur on the device. This behavior has not changed in iOS 6, but I have found that my project deployment is set to 5.0, this should matter.

See also https://stackoverflow.com/questions/3433487/avaudioplayer-leak-in-simulator and a memory leak in the AudioToolbox AVAudioPlayer library .

+5
source

Maybe you did better than me, and I don't have Xcode in my front end, but:

created object, link created:

 AVAudioPlayer *newMusicPlayer = [[AVAudioPlayer alloc] initWithData:data error:&error]; 

this assignment, not distribution, do not save:

 _musicPlayer = newMusicPlayer; 

from the playMusicWithMusicData function, it will release newMusicPlayer , and this will cause my code to crash in another function when I use _musicPlayer . The analyzer tool was so rooted that I saved it, maybe released it too.

if you are using ios4 and not ARC this project.

in if branch:

  [_musicPlayer stop]; _musicPlayer = nil; 

I would check _musicPlayer so that it is not null, but that will not solve your problems.

I don’t know where it can be, if not in ios, and probably there will be:

Decided if you change the delegate instead of yourself.

  _musicPlayer.delegate = self; 

for any other class?

+1
source

Source: https://habr.com/ru/post/1435757/


All Articles