我遇到这个问题,AVAudioRecorder不能为我工作,至少从我能看到的(或听不到的)。
我的目标是使用ARC定位iOS 5.
我确实设置了错误对象,但没有一个被解雇,所以我想我在这里做错了。
以下是我设置AVAudioRecorder的部分:
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir stringByAppendingPathComponent:currentTrack.trackFileName];
currentTrack.trackFileURL = [NSURL fileURLWithPath:soundFilePath];
NSLog(@"Chemin : %@", currentTrack.trackFileURL.path);
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
[recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
currentTrack.trackRecordSettings = recordSetting;
NSError *err = nil;
//[audioSession setDelegate:self];
if(err){
NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
return;
}
[[AVAudioSession sharedInstance] setActive:YES error:&err];
err = nil;
if(err){
NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
return;
}
err = nil;
recorder = [[AVAudioRecorder alloc]
initWithURL:currentTrack.trackFileURL
settings:currentTrack.trackRecordSettings
error:&error];
if (err)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Warning"
message: [err localizedDescription]
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
NSLog(@"error: %@", [error localizedDescription]);
} else {
[recorder setDelegate:self];
[recorder prepareToRecord];
BOOL audioHWAvailable = [[AVAudioSession sharedInstance]inputIsAvailable ];
if (!audioHWAvailable) {
UIAlertView *cantRecordAlert =
[[UIAlertView alloc] initWithTitle: @"Warning"
message: @"Audio input hardware not available"
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[cantRecordAlert show];
return;
}
}
此处没有错误
然后是时候开始录音了:
-(void)startRecordingAudio{
NSLog(@"Start recording");
[[AVAudioSession sharedInstance] setCategory :AVAudioSessionCategoryRecord error:nil];
[recorder record];
}
然后是时候停止录制了:
-(void)stopRecordingAudio{
NSLog(@"Stop recording");
[recorder stop];
NSError *err;
NSData *audioData = [NSData dataWithContentsOfFile:currentTrack.trackFileURL.path options: 0 error:&err];
if(!audioData)
NSLog(@"audio data error: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
NSLog(@"%d", audioData.length);
}
audioData长度始终为4096是否正常?如果我理解正确,那就是大约93毫秒的声音...
最后,当播放录制的声音时:
-(void)startPlayingAudio{
[[AVAudioSession sharedInstance] setCategory :AVAudioSessionCategoryPlayback error:nil];
NSLog(@"Start playing");
NSError *error;
if(player == nil){
player = [[AVAudioPlayer alloc] initWithContentsOfURL:currentTrack.trackFileURL
error:&error];
}
player.delegate = self;
if (error)
NSLog(@"Error: %@",
[error localizedDescription]);
else
[player play];
}
我确实设置了从不开火的委托方法:
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSLog (@"audioRecorderDidFinishRecording:successfully:");
}
-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
NSLog(@"Decode Error occurred");
}
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully: (BOOL)flag
{
NSLog (@"audioRecorderDidFinishRecording:successfully: %d", flag);
}
-(void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error
{
NSLog(@"Encode Error occurred");
}
-(void)beginInterruption{
NSLog(@"INTERRUPT");
}
另外,我确实看到.caf文件是在我的应用程序文档文件夹中正确创建的。
感谢您提供的任何帮助。至于现在,我听不到录制的声音,我使用没有耳机的iPhone 4设备进行测试。
答案 0 :(得分:0)
发现问题: 记录在prepareRecord之前调用。由于我使用viewDidAppear设置录制,因此在视图可见之前进行了开始录制的调用...
我确实使用了viewDidAppear,因为我没有调用viewDidLoad,因为我不使用initWithNib。我试图覆盖loadView,但我找到的所有示例都不清楚主题,并且从不调用loadView。也许有人可以指出我正确的方向?尽管如此,我可以在iphone上听到我的声音!
感谢您的时间。