- (void)viewDidLoad {
[super viewDidLoad];
NSArray *dirPath=NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *docPath=[dirPath objectAtIndex:0];
NSString *filePath=[docPath stringByAppendingPathComponent:@"test1.wav"];
_url=[NSURL URLWithString:filePath];
NSDictionary *recordSettings=[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt:2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil];
NSError *error;
//AVAudioRecorder *recorder;
//AVAudioPlayer *player;
_player=[[AVAudioPlayer alloc] initWithContentsOfURL:_url error:&error];
_recorder=[[AVAudioRecorder alloc] initWithURL:_url settings:recordSettings error:nil];
if (error) {
NSLog(@"%@",[error localizedDescription]);
}else{
[_recorder prepareToRecord];
}
}
调试的时候显示_player的值为nil,还有报错: The operation couldn’t be completed. (OSStatus error 2003334207.)我没在网上找到答案。
_player的值為nil,這個應該是Xcode的BUG導致的,你如果用NSLog輸出,_player應該是有值的。
AVAudioRecorder
設定代理,判斷方法:
樓主,解決了嘛?我也遇到同樣問題,估計是轉wav格式出錯了