在iOS的基礎(chǔ)類庫中, 提供了AVFoundation FrameWork,即Audio/Video基礎(chǔ)類庫,通過使用這個類庫,可以非常容易的在應(yīng)用程序錄制,播放視頻,音頻等。
成都創(chuàng)新互聯(lián)公司是一家專注于網(wǎng)站設(shè)計制作、成都網(wǎng)站設(shè)計與策劃設(shè)計,蕉嶺網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)10年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:蕉嶺等地區(qū)。蕉嶺做網(wǎng)站價格咨詢:13518219792
在錄制前要初始化 AVAudioRecorder*recorder;
//錄音設(shè)置
NSMutableDictionary *recordSetting = [[[NSMutableDictionaryalloc]init] autorelease];
//設(shè)置錄音格式 AVFormatIDKey==kAudioFormatLinearPCM
[recordSetting setValue:[NSNumbernumberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
//設(shè)置錄音采樣率(Hz)如:AVSampleRateKey==8000/44100/96000(影響音頻的質(zhì)量)
[recordSetting setValue:[NSNumbernumberWithFloat:44100] forKey:AVSampleRateKey];
//錄音通道數(shù) 1或 2
[recordSetting setValue:[NSNumbernumberWithInt:1] forKey:AVNumberOfChannelsKey];
//線性采樣位數(shù) 8、16、24、32
[recordSetting setValue:[NSNumbernumberWithInt:16] forKey:AVLinearPCMBitDepthKey];
//錄音的質(zhì)量
[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不發(fā)送
NSLog(@"發(fā)出去");
}else {
//刪除記錄的文件
[recorderdeleteRecording];
//刪除存儲的
}
[recorderstop];
[timerinvalidate];
}