更改目录时AVAudioRecorder无法录制

时间:2012-07-02 17:50:37

标签: iphone objective-c ios avaudiorecorder

我正在录制一些音频,当我使用此代码选择存储音频数据的目录时,这是正常的:

NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                           NSUserDomainMask, YES);
NSString *documentsFolder = [folders objectAtIndex:0];
result = [documentsFolder stringByAppendingPathComponent:@"Recording.m4a"];

但是我需要将音频文件存储在另一个目录中,然后我尝试了这段代码:

NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                           NSUserDomainMask, YES);
NSString *documentsFolder = [folders objectAtIndex:0];
result = [documentsFolder stringByAppendingPathComponent:@"Sounds/Recording.m4a"];

使用新路径初始化AVAudioRecorder实例,但是当我呼叫[myAudioRecorer prepareToRecord][myAudioRecorder record]时,它会失败,并且会记录任何声音。

如何正确更改目录?

PS:我也尝试使用NSFileManager创建一个新目录,但它不起作用。我是这样做的:

[[NSFileManager defaultManager] createDirectoryAtPath:pathAsString withIntermediateDirectories:YES attributes:nil error:&error]

编辑:

这是我录制音频的代码:

//here is the method that returns the path i want to save the files    
- (NSString *) audioRecordingPath{ 
        NSString *result = nil;
        NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                           NSUserDomainMask, YES);
        NSString *documentsFolder = [folders objectAtIndex:0];
        result = [documentsFolder stringByAppendingPathComponent:@"Sounds/Recording.m4a"];
        return result;
    }

//just the record settings
    - (NSDictionary *) audioRecordingSettings{ 
        NSDictionary *result = nil;

        NSMutableDictionary *settings = [[NSMutableDictionary alloc] init];
        [settings setValue:[NSNumber numberWithInteger:kAudioFormatAppleLossless] forKey:AVFormatIDKey];
        [settings setValue:[NSNumber numberWithFloat:44100.0f] forKey:AVSampleRateKey];

        [settings setValue:[NSNumber numberWithInteger:1] forKey:AVNumberOfChannelsKey];
        [settings setValue:[NSNumber numberWithInteger:AVAudioQualityLow] forKey:AVEncoderAudioQualityKey];
        result = [NSDictionary dictionaryWithDictionary:settings];
        return result; 

    }

//here is where i record the audio
    - (IBAction)recordAudio{

        NSError *error = nil;

        NSString *pathAsString = [self audioRecordingPath];

        NSURL *audioRecordingURL = [NSURL fileURLWithPath:pathAsString];
        self.audioRecorder = [[AVAudioRecorder alloc] initWithURL:audioRecordingURL settings:[self audioRecordingSettings] error:&error];

        if (self.audioRecorder != nil){
            self.audioRecorder.delegate = self;
            if ([self.audioRecorder prepareToRecord] && [self.audioRecorder record]){ 
                NSLog(@"Successfully started to record.");

            } else {
                NSLog(@"Failed to record.");// the error returned.
                self.audioRecorder = nil; 
            }
        } else {
            NSLog(@"Failed to create an instance of the audio recorder.");
        } 

    }

0 个答案:

没有答案