在NSUserDefaults中使用url保存NSMutableArray时遇到问题

时间:2014-02-18 05:36:44

标签: avfoundation nsuserdefaults avaudiorecorder avaudiosession

我已经创建了一个基于我阅读的内容的应用程序并且我录制了音频,然后尝试将所有音频文件通过其唯一的url路径永久保存到mutablearray并从那里到NSUserDefaults但我正在尝试设置非属性列表对象错误。这是我的代码崩溃的部分:

-(void)audioRecorderDidFinishRecording: (AVAudioRecorder *)recorder successfully:(BOOL)flag{
if(flag==true){

    [self.recordings addObject:_audioRecorder.url];
     NSLog(@"prwti einai edw %@", self.recordings);

    [self.userDefaults setObject:self.recordings forKey:FTS];
    [self.userDefaults synchronize];
    NSLog(@"%@", self.recordings);
}}

有什么建议吗?请现在我是iOS编程的新手,并试图解决问题。谢谢。

- (void)viewDidLoad{
[super viewDidLoad];

// get the NSUserDefaults object for the app
self.userDefaults = [NSUserDefaults standardUserDefaults];

// get NSDictionary of the app's tag-query pairs
NSArray *dictionary = [self.userDefaults arrayForKey:FTS];

// if dictionary is nil, create empty NSMutableDictionary;
// otherwise, create NSMutableCopy of dictionary
if (dictionary == nil)
    self.recordings = [[NSMutableArray alloc] init];
else
    self.recordings = [dictionary mutableCopy];


//na allaxoume to onoma tou arxeio se monadiko gia to kathena
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"ddMMMYY_hhmmssa";
NSString *date=[[formatter stringFromDate:[NSDate date]] stringByAppendingString:@".caf"];

_playButton.enabled = NO;
_stopButton.enabled = NO;

NSArray *dirPaths;
NSString *docsDir;

dirPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];

NSString *soundFilePath = [docsDir stringByAppendingPathComponent:date];

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey,
                                [NSNumber numberWithInt:16], AVEncoderBitRateKey,
                                [NSNumber numberWithInt: 2], AVNumberOfChannelsKey,
                                [NSNumber numberWithFloat:44100.0], AVSampleRateKey,
                                nil];
NSError *error = nil;

AVAudioSession *audioSession = [AVAudioSession sharedInstance];

[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

_audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL
                                             settings:recordSettings
                                                error:&error];
_audioRecorder.delegate=self;
if (error) {
    NSLog(@"error: %@", [error localizedDescription]); }

else {
    [_audioRecorder prepareToRecord];
}}

1 个答案:

答案 0 :(得分:0)

尝试使用您的网址,例如NSData

[self.recordings addObject:_audioRecorder.url];

NSMutableArray *archiveArray = [NSMutableArray arrayWithCapacity:self.recordings.count];
for (NSString *audioPath in self.recordings) { 
    NSData *pathAudioData = [NSKeyedArchiver archivedDataWithRootObject:audioPath];
    [archiveArray addObject:pathAudioData];
}

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:archiveArray forKey:FTP];

请注意,您只能在NSArray, NSDictionary, NSString, NSData, NSNumber, and NSDate中存储NSUserDefaults之类的内容。