【发布时间】:2023-03-24 10:31:01
【问题描述】:
我打算使用多个 AVAudioPlayer 同时播放不同的音频文件。它们由用户启动的事件触发。我是第一次使用 ARC,关于 ARC 如何应用于设置 AVAudioPlayers 的文档并不多(Apple 的示例代码是 pre-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];
非常感谢任何帮助。
【问题讨论】:
-
通过调试器,看起来问题出在 NSURL 实例上。要么它们在使用之前就被释放了,要么它们正在与另一个发生冲突?
标签: objective-c ios5 avfoundation avaudioplayer automatic-ref-counting