在 ARC 下实现多个 AVAudioPlayer 时应用程序崩溃
我计划使用多个 AVAudioPlayer 同时播放不同的音频文件。它们由用户发起的事件触发。我是第一次使用 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];
非常感谢任何帮助。
I plan to use multiple AVAudioPlayers to play different audio files simultaneously. They are triggered by user-initiated events. I am using ARC for the first time, and there is not much documentation out there as to how ARC might apply to setting up AVAudioPlayers (Apple's sample code is pre-ARC, as are most tutorials I can find).
The app works fine with one AVAudioPlayer instance, but throws a SIGABRT once a second is added. My guess is that it has something to do with the player instances being released prematurely by ARC, but am not sure how to prevent this.
Code is as follows:
@interface LocationTracker : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *mainLoopPlayer;
AVAudioPlayer *sea1Player;
}
@property (strong, nonatomic) AVAudioPlayer *mainLoopPlayer;
@property (strong, nonatomic) AVAudioPlayer *sea1Player;
@end
implementation (in 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];
Any help much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,发现问题了,这与内存管理无关,而是使用引用的文件夹来链接到音频文件。
我以前曾使用引用的文件夹来存储图像,没有任何问题,但显然,在处理多个音频文件时这是不行的。
Okay, found the problem, and it wasn't to do with memory management, but instead the use of a referenced folder to link to the audio files.
I have previously used referenced folders to store images in without any problems, but this is a no-go when dealing with multiple audio files, apparently.