如何将iphone音频文件保存在文档目录中

时间:2012-05-11 07:34:13

标签: iphone

我已经在我的应用程序中实现了录音,但我需要知道如何将录音文件保存在文档目录中,并在其他类中检索相同的文件以播放音频。

这是源代码。

-(void)startPushed
{
        NSMutableDictionary *rs = [[NSMutableDictionary alloc] init];
        [rs setValue:[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
        [rs setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
        [rs setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
        recordedTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"caf"]]];

        recorder = [[AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:rs error:&error];
        [recorder setDelegate:self];
        [recorder prepareToRecord];
        [recorder record];


        NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
        NSString *docDir = [docPath objectAtIndex:0];
        NSString *fullPath = [docDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", recordedTmpFile]];
        NSLog(@"%@", fullPath);
}

 -(void)playbackPushed
    {
    AVAudioPlayer *avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error];
        NSLog(@"Play Path:%@", recordedTmpFile);
        [avPlayer prepareToPlay];
        [avPlayer play];
    }

请告诉我如何在文档目录中读取和写入音频文件(.caf格式) 并提供其他课程中音频播放的信息。

提前致谢

1 个答案:

答案 0 :(得分:0)

使用此代码访问iOS中的文档目录

-(NSURL *)documentDirectory{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];

}

并使用NSFileManager将文件复制到路径。

相关问题