将录音文件地址存储在数组中并在以后使用时遇到问题。这就是我获取 URL 地址、将其转换为字符串并将其添加到数组中的方式。这似乎有效。
测试视图控制器.h
@interface record_audio_testViewController : UIViewController <AVAudioRecorderDelegate> {
NSURL *audioFile;
AVAudioRecorder *recorder;
...
}
@property (nonatomic, retain) NSURL *audioFile;
测试视图控制器.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)
有谁知道如何度过这种情况?此外,是否有更简单和/或更安全的方法来做到这一点?