【问题标题】:App Crashes When Implementing Multiple AVAudioPlayers under ARC在 ARC 下实现多个 AVAudioPlayers 时应用程序崩溃
【发布时间】: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


【解决方案1】:

好的,找到问题了,不是内存管理问题,而是使用引用的文件夹链接到音频文件。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多