【发布时间】:2014-02-12 01:11:24
【问题描述】:
我想将录制的音频保存在本地文档目录 之后我想播放保存的音频文件
音频文件没有在文档目录中创建?
我尝试过使用以下代码我做错了什么?
NSString *temp1=[NSString stringWithFormat:@"%@.m4a",temp];
NSLog(@"=====>>%@",temp1);
NSArray *pathComponents=[NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)lastObject],temp1, nil];
NSURL *outputFileURL=[NSURL fileURLWithPathComponents:pathComponents];
if (isRecording==NO) {
isRecording=YES;
NSLog(@"recording started");
session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
// Define the recorder setting
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
// Initiate and prepare the recorder
recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];
[session setActive:YES error:nil];
// Start recording
[recorder record];
}
else
{
NSLog(@"recording stopped");
[recorder stop];
NSData *audioData=[NSData dataWithContentsOfURL:outputFileURL];
[audioData writeToURL:outputFileURL atomically:YES];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:outputFileURL error:nil];
[player prepareToPlay];
[player play];
[player setVolume:1.0];
isRecording=NO;
}
【问题讨论】:
标签: objective-c ios7 avfoundation nsdocumentdirectory