我已经在我的应用程序中实现了录音,但我需要知道如何将录音文件保存在文档目录中,并在其他类中检索相同的文件以播放音频。
这是源代码。
-(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 格式)并提供其他类音频播放的信息。
提前致谢
使用此
代码访问iOS中的文档目录
-(NSURL *)documentDirectory{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
,然后使用 NSFileManager 将文件复制到路径。