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];
任何帮助非常赞赏。
通过调试器,它看起来问题是与NSURL实例。要么他们在被使用之前被释放,要么一个与另一个发生冲突? – MightyMeta 2012-02-05 00:51:48