【发布时间】: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