0

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

这是源代码。

-(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 格式),并提供在其他类中播放音频的信息。

提前致谢

4

1 回答 1

0

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

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

}

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

于 2012-05-11T08:06:46.230 回答