在iOS的基础类库中, 提供了AVFoundation FrameWork,即Audio/Video基础类库,通过使用这个类库,可以非常容易的在应用程序录制,播放视频,音频等。
在录制前要初始化 AVAudioRecorder *recorder;
//录音设置
NSMutableDictionary *recordSetting = [[[NSMutableDictionaryalloc]init] autorelease];
//设置录音格式 AVFormatIDKey==kAudioFormatLinearPCM
[recordSetting setValue:[NSNumbernumberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
//设置录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量)
[recordSetting setValue:[NSNumbernumberWithFloat:44100] forKey:AVSampleRateKey];
//录音通道数 1 或 2
[recordSetting setValue:[NSNumbernumberWithInt:1] forKey:AVNumberOfChannelsKey];
//线性采样位数 8、16、24、32
[recordSetting setValue:[NSNumbernumberWithInt:16] forKey:AVLinearPCMBitDepthKey];
//录音的质量
[recordSetting setValue:[NSNumbernumberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
NSString *strUrl = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSURL *url = [NSURLfileURLWithPath:[NSStringstringWithFormat:@"%@/lll.aac", strUrl]];
urlPlay = url;
NSError *error;
//初始化
recorder = [[AVAudioRecorderalloc]initWithURL:url settings:recordSetting error:&error];
//开启音量检测
recorder.meteringEnabled = YES;
recorder.delegate = self;
录制完后保存
- (IBAction)btnUp:(id)sender
{
double cTime = recorder.currentTime;
if (cTime > 2) {//如果录制时间<2 不发送
NSLog(@"发出去");
}else {
//删除记录的文件
[recorderdeleteRecording];
//删除存储的
}
[recorderstop];
[timerinvalidate];
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。