0

我打算使用多個AVAudioPlayers同時播放不同的音頻文件。它們由用戶發起的事件觸發。我首次使用ARC,並且沒有太多的文檔介紹ARC如何應用於設置AVAudioPlayers(Apple的示例代碼是ARC之前的,因爲我可以找到大多數教程)。在ARC下實現多個AVAudioPlayers時,應用程序崩潰

該應用程序可以正常使用一個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]; 

任何幫助非常讚賞。

+0

通過調試器,它看起來問題是與NSURL實例。要麼他們在被使用之前被釋放,要麼一個與另一個發生衝突? – MightyMeta 2012-02-05 00:51:48

回答

0

好的,發現問題了,它不是與內存管理有關,而是使用引用文件夾來鏈接到音頻文件。

我以前使用引用的文件夾存儲圖像沒有任何問題,但顯然,這是一個處理多個音頻文件時不去。