iOS6中的AudioToolBox泄漏? [英] AudioToolBox leak in iOS6?
问题描述
当我使用AudioToolBox播放音乐时,内存会大量泄漏。
When I use AudioToolBox for playing music, memory leaks heavily.
AVAudioPlayer *newMusicPlayer = [[AVAudioPlayer alloc] initWithData:data error:&error];
我使用此代码播放音乐。在iOS5和iOS4中,它可以正常工作。但在iOS6中,如果数据大小为5M,则所有5M都泄露。我无法在仪器中看到泄漏信息。
I use this code to play music. In iOS5 and iOS4, it works properly. But in iOS6, if data's size is 5M, all of the 5M leaked. And I can't see leak info in Instruments.
有没有人有同样的问题?任何建议都将不胜感激。
Is there anybody have the same problem? Any suggestion will be grateful.
此处的所有音频代码(使用ARC):
All my audio 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
推荐答案
我正在使用 AVAudioPlayer
从文件播放音频,并发现iOS模拟器始终泄漏内存,但设备没有。这是使用仪器验证的。这是ARC代码。
I'm playing audio from a file using AVAudioPlayer
, and found that the iOS simulator consistently leaks memory, but the device doesn't. This was verified using Instruments. This is ARC code.
我的播放器声明如下:
@property (nonatomic, retain) AVAudioPlayer *numberPlayer;
并合成如下:
@synthesize numberPlayer = _numberPlayer;
因为我需要播放几种不同的声音, AVAudioPlayer
创建后无法重置为播放不同的音频文件,我每次都会创建一个新的播放器,如下所示:
Since I need to play several different sounds, and AVAudioPlayer
cannot be reset to play a different audio file after its creation, I'm creating a new player every time, like this:
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];
在我的委托中,我将播放器设置为 nil
当它完成播放时:
In my delegate I set the player to nil
when it has finished playing:
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
if (player == self.numberPlayer) {
_numberPlayer = nil;
}
}
在模拟器中,两者都是 AudioSessionDevice
和 UISoundNewDevice
泄漏内存。然而,最大的泄密者实际上是 NSURL
。设备上不会发生这些泄漏。这种行为在iOS 6中没有改变,但我确实将我的项目部署设置为5.0,这应该有所不同。
In the simulator, both AudioSessionDevice
and UISoundNewDevice
leak memory. However, the biggest leaker is actually NSURL
. None of these leaks occur on the device. This behavior has not changed in iOS 6, but I do have my project deployment set to 5.0, should that make any difference.
另请参阅https://stackoverflow.com/questions/3433487/avaudioplayer-leak-in-simulator 和 AudioToolbox库AVAudioPlayer中的内存泄漏。
这篇关于iOS6中的AudioToolBox泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!