【问题标题】:NSURL not working after retrieved from NSMutableArray从 NSMutableArray 检索后 NSURL 不起作用
【发布时间】:2012-09-29 03:09:22
【问题描述】:

无法将录音文件地址存储在数组中并在以后使用。 这就是我获取 URL 地址、将其转换为字符串并将其添加到数组中的方式。这似乎有效。

testViewController.h

@interface record_audio_testViewController : UIViewController <AVAudioRecorderDelegate> {       
NSURL *audioFile;
AVAudioRecorder *recorder;
...
}
@property (nonatomic, retain) NSURL *audioFile;

testViewController.m

#import "record_audio_testViewController.h"
@implementation record_audio_testViewController
@synthesize audioFile

- (void)viewDidLoad {
NSMutableArray *urlArray = [[NSMutableArray alloc] init];
    audioFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent: [NSString stringWithFormat: @"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"caf"]]];
    //  convert NSURL to NSString
    NSString *fileLoc = [audioFile absoluteString];
    [urlArray addObject: fileLoc ];
    [[NSUserDefaults standardUserDefaults] setObject:urlArray forKey:@"urlArray"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [urlArray release];
    //  Create an AVAudioSession object.
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];
    //Activate the session
    [audioSession setActive:YES error: &error];
}

- (void)buttonClicked:(UIButton*)button {
    NSMutableArray *urlArray;
    urlArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"urlArray"];
    audioFile = [NSURL URLWithString: [urlArray objectAtIndex:btnN] ];
    //  at this point, the URL looks exactly as created
}

- (IBAction)  rec_button_pressed {
    NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
    [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
    [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];        
    recorder = [[ AVAudioRecorder alloc] initWithURL: audioFile settings: recordSetting error:&error ]; //  this is where it CRASHES

    [recorder setDelegate:self];
    [recorder prepareToRecord];
    [recorder record];
}

当我使用 NSLog 查看和比较这些地址时,它们看起来都一样。但是程序抛出异常:

    Thread 1: EXC_BAD_ACCESS (code=2 address=0x9)

有人知道如何度过这种情况吗? 另外,是否有更简单和/或更安全的方法来做到这一点?

【问题讨论】:

  • 错误访问发生在哪里?另外,您使用ARC吗?当您存储 URL 时,您实际上应该获取书签数据并存储它,然后稍后读取书签数据并从中创建一个 URL。另外,音频日期真的还在吗?临时目录随时可能被清空,您的文件实际上可能不再存在,尤其是在它很大的情况下。
  • 它发生在记录器上 = 在这个项目中没有使用 ARC。不熟悉书签数据。实际上还没有发现我的音频数据是否仍然存在。我想录制大约 50 到 200 个短音频文件并随意访问它们。我只是不知道为什么 URL 会崩溃,如果它看起来和之前完全一样。
  • 我假设 initWithURL: audioFile 是问题所在。可能是错的。
  • 例如使用NSLog,下面是audioFile的BEFORE和AFTER值:file://localhost/Users/apple/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/99B96531- 3BB0-4440-ABC6-916F794244CA/tmp/3705780443938.caf 文件://localhost/Users/apple/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/99B96531-3BB0-4440-ABC6-916F794244CA/tmp/3705780443938 .caf
  • audioFile 存储在哪里?那是局部变量还是实例变量?您可以编辑您的问题以包含导致错误的方法的整个方法主体吗?这对我们有很大帮助。

标签: ios xcode nsmutablearray nsurl audio-recording


【解决方案1】:

您显示的代码可能完全无关紧要,因为这里并没有引发异常。该异常可能是由于内存管理错误,并且可能与 NSURL 无关。你只是在猜测——你不应该这样。

您的代码中没有内存管理,而且由于您没有使用 ARC,这表明您没有意识到自己的内存管理职责。我的书向您解释了它们,并解释了为什么应该尽可能使用 ARC:

http://www.apeth.com/iOSBook/ch12.html

另外,使用静态分析器;它会发现一些内存管理不善的危险迹象。

【讨论】:

  • 好吧,毫无疑问,我有很多东西要学习负责任的编码。这现在是原型,当我认为这个项目真的值得做时,我打算重新开始并使用 ARC。回到这个问题,如果我这样说一切都很好:audioFile = [NSURL fileURLWithPath:. . . .紧接在:recorder = [[ . . .线。但是,再次使用 NSLog,它们看起来完全一样。
  • 即使在进行原型设计时,您也不能忽略内存管理。 Objective-C 不是那种语言。到目前为止,没有理由认为 NSURL 只是一个红鲱鱼。
  • 好的,我运行了 ANALYZE 并得到了这个: .../record_audio_testViewController.m:292:9: 对象泄漏:分配并存储到“recordSetting”中的对象在此执行路径的后面没有被引用,并且有保留次数为 +1
  • 我做了一个 [recordSetting release] 来摆脱它。
猜你喜欢
  • 2013-11-26
  • 1970-01-01
  • 2012-04-21
  • 1970-01-01
  • 1970-01-01
  • 2010-12-22
  • 1970-01-01
  • 2013-09-07
  • 1970-01-01
相关资源
最近更新 更多