今天开始做录音的功能。点击recordbtn开始录音,再次点击,则停止录音,点击play键播放录音
然后就遇到问题了。
点击play键后,[self.audioPlayer prepareToPlay] 总是为no,即使把这个去掉,强制[self.audioPlayer play];也没有效果,请问是怎么回事?
另:用player播放其他音频是可以的,作为对比测试,播放了一首歌曲,没问题,就是播放自己录制的音频出问题,是不是录制的时候就出错了?
附代码:
-(void)record{
NSLog(@"recording");
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];
[audioSession setActive:YES error:nil];
NSString *tripReportAudioPath = [self audioRecordingPath];
NSError *err = nil;
NSURL *tripReportAudioURL = [NSURL URLWithString:tripReportAudioPath];
AVAudioRecorder *tripReportRecorder = [[AVAudioRecorder alloc]initWithURL:tripReportAudioURL settings:[self audioRecordingSetting] error:&err];
if (tripReportRecorder != nil) {
tripReportRecorder.delegate = self;
if ([tripReportRecorder prepareToRecord] && self.recordBtn.selected == NO) {
self.recordBtn.selected = YES;
[tripReportRecorder record];
}else if (self.recordBtn.selected == YES){
self.recordBtn.selected = NO;
[tripReportRecorder stop ];
}
}else{
NSLog(@"record err:%@",[err description]);
}
}
//生成录音文件地址
-(NSString *)audioRecordingPath{
NSString *result = nil;
NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docdir = [folders objectAtIndex:0];
result = [docdir stringByAppendingPathComponent:@"tripReport.caf"];
return result;
}
//录音器的相关设置
-(NSDictionary *)audioRecordingSetting{
NSDictionary *result = nil;
NSMutableDictionary *settings = [[NSMutableDictionary alloc]init ];
//录制的音频格式
[settings setValue:[NSNumber numberWithInteger:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
//采样率
[settings setValue:[NSNumber numberWithFloat:44100.0f] forKey:AVSampleRateKey];
//信道数
[settings setValue:[NSNumber numberWithInteger:1] forKey:AVNumberOfChannelsKey];
//录音的质量
[settings setValue:[NSNumber numberWithInteger:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
[settings setValue:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey];
[settings setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
result = [NSDictionary dictionaryWithDictionary:settings];
return result;
}
-(void)play{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive:YES error:nil];
NSError *playBackError = nil;
NSError *readingError = nil;
NSString *audioPath = [self audioRecordingPath];
// NSString *audioPath = [[NSBundle mainBundle]pathForResource:@"Love" ofType:@"mp3"];
NSLog(@"path:%@",audioPath);
NSURL *url = [NSURL URLWithString:audioPath];
NSData *fileData = [NSData dataWithContentsOfFile:audioPath options:NSDataReadingMapped error:&readingError];
self.audioPlayer = [[AVAudioPlayer alloc]initWithData:fileData error:&playBackError];
if (self.audioPlayer != nil) {
NSLog(@"finishi3");
self.audioPlayer.delegate = self;
self.audioPlayer.volume = 50.0;
if ([self.audioPlayer prepareToPlay]) {
NSLog(@"finishi4");
UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);
[self.audioPlayer play];
}
}else{
NSLog(@"error:%@",[playBackError description]);
}
}