在ARC下实现多个AVAudioPlayers时App崩溃

时间:2012-02-05 00:28:31

标签: objective-c ios5 avfoundation avaudioplayer automatic-ref-counting

我计划使用多个AVAudioPlayers同时播放不同的音频文件。它们由用户启动的事件触发。我是第一次使用ARC,并且没有太多关于ARC如何应用于设置AVAudioPlayers的文档(Apple的示例代码是预ARC,我可以找到大多数教程)。

该应用程序适用于一个AVAudioPlayer实例,但每秒添加一次SIGABRT。我的猜测是它与ARC过早发布的播放器实例有关,但我不知道如何防止这种情况发生。

代码如下:

@interface LocationTracker : UIViewController <AVAudioPlayerDelegate> {

AVAudioPlayer *mainLoopPlayer;
AVAudioPlayer *sea1Player;

}

@property (strong, nonatomic) AVAudioPlayer *mainLoopPlayer;
@property (strong, nonatomic) AVAudioPlayer *sea1Player;

@end

实现(在viewDidLoad中):

NSURL *mainLoopURL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:@"wind" ofType:@"aiff"]];   
self.mainLoopPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:mainLoopURL error:nil];  
mainLoopPlayer.numberOfLoops = -1;
mainLoopPlayer.delegate = self;
[mainLoopPlayer prepareToPlay];

NSURL *sea1URL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:@"sea1" ofType:@"aiff"]];   
self.sea1Player = [[AVAudioPlayer alloc] initWithContentsOfURL:sea1URL error:nil];  
sea1Player.numberOfLoops = -1;
sea1Player.delegate = self;
[mainLoopPlayer prepareToPlay];

任何帮助都非常感激。

1 个答案:

答案 0 :(得分:0)

好的,发现了问题,这与内存管理无关,而是使用引用的文件夹链接到音频文件。

我以前使用引用的文件夹来存储图像而没有任何问题,但显然处理多个音频文件时这是不可取的。

相关问题